package azkaban.webapp.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import azkaban.utils.WebUtils;
import azkaban.utils.JSONUtils;
import azkaban.utils.Props;
import azkaban.webapp.AzkabanWebServer;
import azkaban.webapp.session.Session;
public abstract class AbstractAzkabanServlet extends HttpServlet {
private static final DateTimeFormatter ZONE_FORMATTER = DateTimeFormat.forPattern("z");
private static final String AZKABAN_SUCCESS_MESSAGE = "azkaban.success.message";
private static final String AZKABAN_FAILURE_MESSAGE = "azkaban.failure.message";
private static final long serialVersionUID = -1;
public static final String DEFAULT_LOG_URL_PREFIX = "predefined_log_url_prefix";
public static final String LOG_URL_PREFIX = "log_url_prefix";
public static final String HTML_TYPE = "text/html";
public static final String XML_MIME_TYPE = "application/xhtml+xml";
public static final String JSON_MIME_TYPE = "application/json";
private static final WebUtils utils = new WebUtils();
private AzkabanWebServer application;
private String name;
private String label;
private String color;
public AzkabanWebServer getApplication() {
return application;
}
@Override
public void init(ServletConfig config) throws ServletException {
application = (AzkabanWebServer) config.getServletContext().getAttribute(AzkabanServletContextListener.AZKABAN_SERVLET_CONTEXT_KEY);
if (application == null) {
throw new IllegalStateException(
"No batch application is defined in the servlet context!");
}
Props props = application.getAzkabanProps();
name = props.getString("azkaban.name", "");
label = props.getString("azkaban.label", "");
color = props.getString("azkaban.color", "#FF0000");
}
public boolean hasParam(HttpServletRequest request, String param) {
return request.getParameter(param) != null;
}
public String getParam(HttpServletRequest request, String name) throws ServletException {
String p = request.getParameter(name);
if (p == null) {
throw new ServletException("Missing required parameter '" + name + "'.");
}
else {
return p;
}
}
public int getIntParam(HttpServletRequest request, String name) throws ServletException {
String p = getParam(request, name);
return Integer.parseInt(p);
}
public int getIntParam(HttpServletRequest request, String name, int defaultVal) {
if (hasParam(request, name)) {
try {
return getIntParam(request, name);
} catch (Exception e) {
return defaultVal;
}
}
return defaultVal;
}
public Map<String, String> getParamGroup(HttpServletRequest request, String groupName) throws ServletException {
Enumeration<Object> enumerate = (Enumeration<Object>)request.getParameterNames();
String matchString = groupName + "[";
HashMap<String, String> groupParam = new HashMap<String, String>();
while( enumerate.hasMoreElements() ) {
String str = (String)enumerate.nextElement();
if (str.startsWith(matchString)) {
groupParam.put(str.substring(matchString.length(), str.length() - 1), request.getParameter(str));
}
}
return groupParam;
}
protected void setSessionValue(HttpServletRequest request, String key, Object value) {
request.getSession(true).setAttribute(key, value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void addSessionValue(HttpServletRequest request, String key, Object value) {
List l = (List) request.getSession(true).getAttribute(key);
if (l == null)
l = new ArrayList();
l.add(value);
request.getSession(true).setAttribute(key, l);
}
protected void setErrorMessageInCookie(HttpServletResponse response, String errorMsg) {
Cookie cookie = new Cookie(AZKABAN_FAILURE_MESSAGE, errorMsg);
response.addCookie(cookie);
}
protected void setSuccessMessageInCookie(HttpServletResponse response, String message) {
Cookie cookie = new Cookie(AZKABAN_SUCCESS_MESSAGE, message);
response.addCookie(cookie);
}
protected String getSuccessMessageFromCookie(HttpServletRequest request) {
Cookie cookie = getCookieByName(request, AZKABAN_SUCCESS_MESSAGE);
if (cookie == null) {
return null;
}
return cookie.getValue();
}
protected String getErrorMessageFromCookie(HttpServletRequest request) {
Cookie cookie = getCookieByName(request, AZKABAN_FAILURE_MESSAGE);
if (cookie == null) {
return null;
}
return cookie.getValue();
}
protected Cookie getCookieByName(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
}
return null;
}
protected Page newPage(HttpServletRequest req, HttpServletResponse resp, Session session, String template) {
Page page = new Page(req, resp, application.getVelocityEngine(), template);
page.add("azkaban_name", name);
page.add("azkaban_label", label);
page.add("azkaban_color", color);
page.add("utils", utils);
page.add("timezone", ZONE_FORMATTER.print(System.currentTimeMillis()));
page.add("currentTime", (new DateTime()).getMillis());
if (session != null && session.getUser() != null) {
page.add("user_id", session.getUser().getUserId());
}
page.add("context", req.getContextPath());
String errorMsg = getErrorMessageFromCookie(req);
page.add("error_message", errorMsg == null || errorMsg.isEmpty() ? "null" : errorMsg);
setErrorMessageInCookie(resp, null);
String successMsg = getSuccessMessageFromCookie(req);
page.add("success_message", successMsg == null || successMsg.isEmpty() ? "null" : successMsg);
setSuccessMessageInCookie(resp, null);
return page;
}
protected Page newPage(HttpServletRequest req, HttpServletResponse resp, String template) {
Page page = new Page(req, resp, application.getVelocityEngine(), template);
page.add("azkaban_name", name);
page.add("azkaban_label", label);
page.add("azkaban_color", color);
page.add("timezone", ZONE_FORMATTER.print(System.currentTimeMillis()));
page.add("currentTime", (new DateTime()).getMillis());
page.add("context", req.getContextPath());
return page;
}
protected void writeJSON(HttpServletResponse resp, Object obj) throws IOException {
resp.setContentType(JSON_MIME_TYPE);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(resp.getOutputStream(), obj);
}
public static AzkabanWebServer getApp(ServletConfig config) {
AzkabanWebServer app = (AzkabanWebServer) config.getServletContext().getAttribute(AzkabanServletContextListener.AZKABAN_SERVLET_CONTEXT_KEY);
if (app == null) {
throw new IllegalStateException("No batch application is defined in the servlet context!");
}
else {
return app;
}
}
public static String createJsonResponse(String status, String message, String action, Map<String, Object> params) {
HashMap<String, Object> response = new HashMap<String, Object>();
response.put("status", status);
if (message != null) {
response.put("message", message);
}
if (action != null) {
response.put("action", action);
}
if (params != null) {
response.putAll(params);
}
return JSONUtils.toJSON(response);
}
}