-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAdminServlet.java
113 lines (99 loc) · 3.99 KB
/
AdminServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.atlassian.labs.speakeasy.ui;
import com.atlassian.labs.speakeasy.external.SpeakeasyService;
import com.atlassian.labs.speakeasy.external.UnauthorizedAccessException;
import com.atlassian.labs.speakeasy.model.Permission;
import com.atlassian.labs.speakeasy.model.Settings;
import com.atlassian.plugin.webresource.UrlMode;
import com.atlassian.plugin.webresource.WebResourceManager;
import com.atlassian.plugin.webresource.WebResourceUrlProvider;
import com.atlassian.plugins.rest.common.json.JaxbJsonMarshaller;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.atlassian.templaterenderer.annotations.HtmlSafe;
import com.google.common.collect.ImmutableMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import static com.atlassian.labs.speakeasy.util.JavascriptEscaper.escape;
/**
*
*/
public class AdminServlet extends HttpServlet
{
private final SpeakeasyService speakeasyService;
private final TemplateRenderer templateRenderer;
private final UserManager userManager;
private final WebResourceManager webResourceManager;
private final JaxbJsonMarshaller jsonMarshaller;
private final WebResourceUrlProvider webResourceUrlProvider;
public AdminServlet(
SpeakeasyService speakeasyService,
UserManager userManager,
TemplateRenderer templateRenderer,
WebResourceManager webResourceManager,
JaxbJsonMarshaller jsonMarshaller,
WebResourceUrlProvider webResourceUrlProvider)
{
this.speakeasyService = speakeasyService;
this.userManager = userManager;
this.templateRenderer = templateRenderer;
this.webResourceManager = webResourceManager;
this.jsonMarshaller = jsonMarshaller;
this.webResourceUrlProvider = webResourceUrlProvider;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String user = userManager.getRemoteUsername();
if (!userManager.isAdmin(user))
{
resp.sendError(403, "Must be an administrator to configure Speakeasy");
return;
}
webResourceManager.requireResource("com.atlassian.auiplugin:ajs");
webResourceManager.requireResourcesForContext("speakeasy.admin");
try
{
final Settings settings = speakeasyService.getSettings(user);
resp.setContentType("text/html");
render("templates/admin.vm", ImmutableMap.<String,Object>builder().
put("user", user).
put("contextPath", req.getContextPath()).
put("staticResourcesPrefix", webResourceUrlProvider.getStaticResourcePrefix(UrlMode.RELATIVE)).
put("settings", new JsRenderer(jsonMarshaller.marshal(settings))).
put("permissionsJson", new JsRenderer(jsonMarshaller.marshal(Permission.ALL))).
put("permissions", Permission.ALL).
build(),
resp.getWriter());
}
catch (UnauthorizedAccessException e)
{
resp.sendError(403, e.getMessage());
}
resp.getWriter().close();
}
public static class JsRenderer
{
private final String value;
public JsRenderer(String value)
{
this.value = value;
}
@HtmlSafe
@com.atlassian.velocity.htmlsafe.HtmlSafe
public String render() throws IOException
{
return value;
}
}
protected void render(final String template, final Map<String, Object> renderContext,
final Writer output)
throws IOException
{
templateRenderer.render(template, renderContext, output);
}
}