* @throws IOException if an I/O error occurs while handling the request
*/
private void execute(HttpServletRequest request, HttpServletResponse response, String method)
throws ServletException, IOException {
RequestContext context = null;
try {
String msg = "HTTP "+method+", "+request.getRequestURL().toString();
if ((request.getQueryString() != null) && (request.getQueryString().length() > 0)) {
msg += "?"+request.getQueryString();
}
getLogger().fine(msg);
String sEncoding = request.getCharacterEncoding();
if ((sEncoding == null) || (sEncoding.trim().length() == 0)) {
request.setCharacterEncoding("UTF-8");
}
context = RequestContext.extract(request);
/// estabish the publisher
StringAttributeMap params = context.getCatalogConfiguration().getParameters();
String autoAuthenticate = Val.chkStr(params.getValue("BaseServlet.autoAuthenticate"));
if (!autoAuthenticate.equalsIgnoreCase("false")) {
Credentials credentials = getCredentials(request);
if (credentials != null) {
this.authenticate(context,credentials);
}
}
Publisher publisher = new Publisher(context);
// executeUpdate the appropriate action
if (method.equals("GET")) {
this.executeGet(request,response,context,publisher);
} else if (method.equals("POST")) {
this.executePost(request,response,context,publisher);
} else if (method.equals("PUT")) {
this.executePut(request,response,context,publisher);
} else if (method.equals("DELETE")) {
this.executeDelete(request,response,context,publisher);
}
} catch (CredentialsDeniedException e) {
String sRealm = this.getRealm(context);
response.setHeader("WWW-Authenticate","Basic realm=\""+sRealm+"\"");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (NotAuthorizedException e) {
String sRealm = this.getRealm(context);
response.setHeader("WWW-Authenticate","Basic realm=\""+sRealm+"\"");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (ValidationException e) {
String sMsg = e.toString();
if (sMsg.contains("XSD violation.")) {
sMsg = "XSD violation.";
} else if (sMsg.contains("Invalid metadata document.")) {
sMsg = "Invalid metadata document.";
} else {
sMsg = "Invalid metadata document.";
}
String json = Val.chkStr(request.getParameter("errorsAsJson"));
if (json.length()>0) {
json = Val.escapeXmlForBrowser(json);
FacesContextBroker fcb = new FacesContextBroker(request, response);
MessageBroker msgBroker = fcb.extractMessageBroker();
ArrayList<String> validationMessages = new ArrayList<String>();
e.getValidationErrors().buildMessages(msgBroker, validationMessages, true);
StringBuilder sb = new StringBuilder();
sb.append(json).append(" = {\r\n");
sb.append("message: \"").append(Val.escapeStrForJson(sMsg)).append("\",\r\n");
sb.append("code: 409,\r\n");
sb.append("errors: [\r\n");
for (int i=0; i<validationMessages.size(); i++) {
if (i>0) {
sb.append(",\r\n");
}
sb.append("\"").append(Val.escapeStrForJson(validationMessages.get(i))).append("\"");
}
if (validationMessages.size()>0) {
sb.append("\r\n");
}
sb.append("]}");
LOGGER.log(Level.SEVERE, sb.toString());
response.getWriter().print(sb.toString());
} else {
response.sendError(409,sMsg);
}
} catch (ServletException e) {
String sMsg = e.getMessage();
int nCode = Val.chkInt(sMsg.substring(0,3),500);
sMsg = Val.chkStr(sMsg.substring(4));
String json = Val.chkStr(request.getParameter("errorsAsJson"));
if (json.length()>0) {
json = Val.escapeXmlForBrowser(json);
StringBuilder sb = new StringBuilder();
sb.append(json).append(" = {\r\n");
sb.append("message: \"").append(Val.escapeStrForJson(sMsg)).append("\",\r\n");
sb.append("code: ").append(nCode).append(",\r\n");
sb.append("errors: [\r\n");
sb.append("\"").append(Val.escapeStrForJson(sMsg)).append("\"");
sb.append("]}");
LOGGER.log(Level.SEVERE, sb.toString());
response.getWriter().print(sb.toString());
} else {
response.sendError(nCode,sMsg);
}
} catch (Throwable t) {
String sMsg = t.toString();
String json = Val.chkStr(request.getParameter("errorsAsJson"));
if (json.length()>0) {
json = Val.escapeXmlForBrowser(json);
StringBuilder sb = new StringBuilder();
sb.append(json).append(" = {\r\n");
sb.append("message: \"").append(Val.escapeStrForJson(sMsg)).append("\",\r\n");
sb.append("code: ").append(500).append(",\r\n");
sb.append("errors: [\r\n");
sb.append("\"").append(Val.escapeStrForJson(sMsg)).append("\"");
sb.append("]}");
LOGGER.log(Level.SEVERE, sb.toString());
response.getWriter().print(sb.toString());
} else if (sMsg.contains("The document is owned by another user:")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN,"The document is owned by another user.");
} else {
String sErr = "Exception occured while processing servlet request.";
getLogger().log(Level.SEVERE,sErr,t);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
sMsg + sErr);
}
} finally {
if (context != null) context.onExecutionPhaseCompleted();
}
}