Skip to content

Instantly share code, notes, and snippets.

@wtfuii
Created March 27, 2017 06:31
Show Gist options
  • Save wtfuii/f7e81b6cd8857265f31e97402a7cc722 to your computer and use it in GitHub Desktop.
Save wtfuii/f7e81b6cd8857265f31e97402a7cc722 to your computer and use it in GitHub Desktop.
Skeleton for an easesolutions codestyle Servlet file
package com.easesolutions.jira.plugin.mailclient.servlet;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.plugin.webresource.UrlMode;
import com.atlassian.plugin.webresource.WebResourceUrlProvider;
import com.atlassian.sal.api.auth.LoginUriProvider;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.easesolutions.jira.plugin.mailclient.model.service.PermissionAccessService;
import com.easesolutions.jira.plugin.mailclient.util.LoggerHelper;
import com.easesolutions.jira.plugin.mailclient.util.ProjectPartsHelper;
import com.easesolutions.jira.plugin.mailclient.util.ServletHelper;
import com.easesolutions.jira.plugin.mailclient.util.UrlHelper;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
public class AdminRulesServlet extends BaseServlet
{
private static final long serialVersionUID = 0x0008;
private final LoggerHelper _loggerHelper;
private final PermissionAccessService _permissionAccess;
private final WebResourceUrlProvider _webResource;
private final Gson _gson;
//dependency injection ceremony
public AdminRulesServlet(
TemplateRenderer engine,
UserManager jiraUserManager,
com.atlassian.sal.api.user.UserManager userManager,
LoginUriProvider loginUriProvider,
UrlHelper urlHelper,
PermissionAccessService permissionAccessService,
WebResourceUrlProvider webResource
) {
super(engine, jiraUserManager, userManager, loginUriProvider, urlHelper);
this._permissionAccess = permissionAccessService;
this._webResource = webResource;
this._loggerHelper = new LoggerHelper(AdminRulesServlet.class);
this._gson = new Gson();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
_loggerHelper.start("doGet");
_loggerHelper.param(req, resp);
ServletHelper.CheckResult check;
//extract user from request
check = performInitialCheck(req, resp);
if(check==null) {
return;
}
//request mapping to specific functions
String op;
op = req.getParameter("op");
if (op != null) {
if (op.equals("mygetoperation")) {
handleMyGetOperation(check, resp);
}
} else {
handleDefaultContent(check, resp);
}
_loggerHelper.end("doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
_loggerHelper.start("doPost");
_loggerHelper.param(req, resp);
ServletHelper.CheckResult check;
//extract user from request
check = performInitialCheck(req, resp);
if(check==null) {
return;
}
//request mapping to specific functions
String op;
op = req.getParameter("op");
if (op != null) {
if (op.equals("mypostoperation")) {
handleMyPostOperation(check, resp, req);
}
} else {
ServletHelper.sendBadRequest(resp);
}
_loggerHelper.end("doPost");
}
private void handleDefaultContent(ServletHelper.CheckResult check, HttpServletResponse resp) throws IOException {
_loggerHelper.start("handleDefaultContent");
_loggerHelper.param(check, resp);
String rsrc = _webResource.getStaticPluginResourceUrl("com.easesolutions.jira.plugins.my-plugin:my.js.resources", "", UrlMode.ABSOLUTE);
HashMap<String, Object> model = new HashMap<String, Object>();
//if another HTML / CSS part should be included in the initial template, add it like this to the context.
model.put("myTemplatePart", ProjectPartsHelper.createPart(_engine, null, new HashMap<String, Object>(), rsrc, "/templates/templatePart.vm"));
model.put("rsrc_url", rsrc);
ServletHelper.addHtmlResponseType(resp);
//respond with the compiled .vm template
_engine.render("templates/template.vm", model, resp.getWriter());
_loggerHelper.end("handleDefaultContent");
}
private void handleMyGetOperation(ServletHelper.CheckResult check, HttpServletResponse resp) throws IOException {
_loggerHelper.start("handleMyGetOperation");
_loggerHelper.param(check, resp);
try {
if (!_permissionAccess.hasAccess(check.appUser, PermissionAccessService.Access.MY_PERMISSION_READ)) {
throw new ServletHelper.BadData();
}
//creating an object to return as json to the client
MyJsonData result = new MyJsonData();
result.myInteger = 42;
result.myString = "my new string";
//converting the java object to JSON and returning it
ServletHelper.writeJsonBody(resp, _gson.toJson(result));
} catch (Exception e) {
_loggerHelper.error(e);
ServletHelper.sendBadRequest(resp);
} finally {
_loggerHelper.end("handleMyGetOperation");
}
}
private void handleMyPostOperation(ServletHelper.CheckResult check, HttpServletResponse resp, HttpServletRequest req) throws IOException {
_loggerHelper.start("handleMyPostOperation");
_loggerHelper.param(check, resp);
try {
//permission check
if (!_permissionAccess.hasAccess(check.appUser, PermissionAccessService.Access.MY_PERMISSION_WRITE)) {
throw new ServletHelper.BadData();
}
//convert JSON to Java object
MyJsonData payload = _gson.fromJson(ServletHelper.readJsonBody(req).toString(), MyJsonData.class);
//do whatever you want with the posted data
System.out.println(payload);
ServletHelper.sendSuccess(resp);
} catch (Exception e) {
_loggerHelper.error(e);
ServletHelper.sendBadRequest(resp);
} finally {
_loggerHelper.end("handleMyPostOperation");
}
}
//payload java class
private class MyJsonData {
public Integer myInteger;
public String myString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment