Author: fmui
Date: Tue Aug 9 14:40:07 2016
New Revision: 1755607
URL: http://svn.apache.org/viewvc?rev=1755607&view=rev
Log:
Server: in case of an Error send the status code 500, not 200, and log the error
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractCmisHttpServlet.java
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java?rev=1755607&r1=1755606&r2=1755607&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
Tue Aug 9 14:40:07 2016
@@ -77,6 +77,7 @@ import org.apache.chemistry.opencmis.com
import org.apache.chemistry.opencmis.commons.server.CallContext;
import org.apache.chemistry.opencmis.commons.server.CmisService;
import org.apache.chemistry.opencmis.server.impl.ServerVersion;
+import
org.apache.chemistry.opencmis.server.impl.browser.AbstractBrowserServiceCall;
import org.apache.chemistry.opencmis.server.shared.AbstractCmisHttpServlet;
import org.apache.chemistry.opencmis.server.shared.Dispatcher;
import org.apache.chemistry.opencmis.server.shared.ExceptionHelper;
@@ -97,6 +98,11 @@ public class CmisAtomPubServlet extends
private static final Logger LOG =
LoggerFactory.getLogger(CmisAtomPubServlet.class);
+ private static final String OPENCMIS_CSS_STYLE = "<style>"
+ + "<!--H1
{font-size:24px;line-height:normal;font-weight:bold;background-color:#f0f0f0;color:#003366;border-bottom:1px
solid #3c78b5;padding:2px;} "
+ + "BODY
{font-family:Verdana,arial,sans-serif;color:black;font-size:14px;} "
+ + "HR {color:#3c78b5;height:1px;}--></style>";
+
private static final long serialVersionUID = 1L;
private final Dispatcher dispatcher = new Dispatcher();
@@ -212,6 +218,25 @@ public class CmisAtomPubServlet extends
} else {
printError(e, request, response);
}
+
+ } catch (Throwable t) {
+ LOG.error(createLogMessage(t, request), t);
+
+ try {
+ response.resetBuffer();
+
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+
response.setContentType(AbstractBrowserServiceCall.JSON_MIME_TYPE);
+ response.setCharacterEncoding(IOUtils.UTF8);
+
+ PrintWriter pw = response.getWriter();
+ writeHtmlErrorPage(pw, 500, "runtime", "An error occurred!",
t);
+ pw.flush();
+ } catch (Exception te) {
+ // we tried to send an error message but it failed.
+ // there is nothing we can do...
+ }
+
+ throw t;
} finally {
// we are done.
try {
@@ -354,39 +379,8 @@ public class CmisAtomPubServlet extends
response.setCharacterEncoding(IOUtils.UTF8);
PrintWriter pw = response.getWriter();
-
- pw.print("<html><head><title>Apache Chemistry OpenCMIS - "
- + exceptionName
- + " error</title>"
- + "<style><!--H1
{font-size:24px;line-height:normal;font-weight:bold;background-color:#f0f0f0;color:#003366;border-bottom:1px
solid #3c78b5;padding:2px;} "
- + "BODY
{font-family:Verdana,arial,sans-serif;color:black;font-size:14px;} "
- + "HR
{color:#3c78b5;height:1px;}--></style></head><body>");
- pw.print("<h1>HTTP Status " + statusCode + " - <!--exception-->" +
exceptionName + "<!--/exception--></h1>");
- pw.print("<p><!--message-->");
- StringEscapeUtils.ESCAPE_HTML4.translate(message, pw);
- pw.print("<!--/message--></p>");
-
- String st = ExceptionHelper.getStacktraceAsString(ex);
- if (st != null) {
- pw.print("<hr
noshade='noshade'/><!--stacktrace--><pre>\n<!--key-->stacktrace<!--/key><!--value-->"
- + st + "<!--/value-->\n</pre><!--/stacktrace--><hr
noshade='noshade'/>");
- }
-
- if (ex instanceof CmisBaseException) {
- Map<String, String> additionalData = ((CmisBaseException)
ex).getAdditionalData();
- if (additionalData != null && !additionalData.isEmpty()) {
- pw.print("<hr noshade='noshade'/>Additional
data:<br><br>");
- for (Map.Entry<String, String> e :
additionalData.entrySet()) {
- pw.print("<!--key-->");
- StringEscapeUtils.ESCAPE_HTML4.translate(e.getKey(),
pw);
- pw.print("<!--/key--> = <!--value-->");
- StringEscapeUtils.ESCAPE_HTML4.translate(e.getValue(),
pw);
- pw.print("<!--/value--><br>");
- }
- }
- }
-
- pw.print("</body></html>");
+ writeHtmlErrorPage(pw, statusCode, exceptionName, message, ex);
+ pw.flush();
} catch (Exception e) {
LOG.error(createLogMessage(ex, request), e);
try {
@@ -396,4 +390,36 @@ public class CmisAtomPubServlet extends
}
}
}
+
+ protected void writeHtmlErrorPage(PrintWriter pw, int statusCode, String
exceptionName, String message, Throwable t)
+ throws IOException {
+ pw.print("<html><head><title>Apache Chemistry OpenCMIS - " +
exceptionName + " error</title>"
+ + OPENCMIS_CSS_STYLE + "</head><body>");
+ pw.print("<h1>HTTP Status " + statusCode + " - <!--exception-->" +
exceptionName + "<!--/exception--></h1>");
+ pw.print("<p><!--message-->");
+ StringEscapeUtils.ESCAPE_HTML4.translate(message, pw);
+ pw.print("<!--/message--></p>");
+
+ String st = ExceptionHelper.getStacktraceAsString(t);
+ if (st != null) {
+ pw.print("<hr
noshade='noshade'/><!--stacktrace--><pre>\n<!--key-->stacktrace<!--/key><!--value-->"
+ st
+ + "<!--/value-->\n</pre><!--/stacktrace--><hr
noshade='noshade'/>");
+ }
+
+ if (t instanceof CmisBaseException) {
+ Map<String, String> additionalData = ((CmisBaseException)
t).getAdditionalData();
+ if (additionalData != null && !additionalData.isEmpty()) {
+ pw.print("<hr noshade='noshade'/>Additional data:<br><br>");
+ for (Map.Entry<String, String> e : additionalData.entrySet()) {
+ pw.print("<!--key-->");
+ StringEscapeUtils.ESCAPE_HTML4.translate(e.getKey(), pw);
+ pw.print("<!--/key--> = <!--value-->");
+ StringEscapeUtils.ESCAPE_HTML4.translate(e.getValue(), pw);
+ pw.print("<!--/value--><br>");
+ }
+ }
+ }
+
+ pw.print("</body></html>");
+ }
}
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java?rev=1755607&r1=1755606&r2=1755607&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
Tue Aug 9 14:40:07 2016
@@ -76,6 +76,7 @@ import static org.apache.chemistry.openc
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletConfig;
@@ -103,6 +104,7 @@ import org.apache.chemistry.opencmis.com
import
org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
import
org.apache.chemistry.opencmis.commons.exceptions.CmisVersioningException;
import org.apache.chemistry.opencmis.commons.impl.Constants;
+import org.apache.chemistry.opencmis.commons.impl.IOUtils;
import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
import org.apache.chemistry.opencmis.commons.server.CallContext;
import org.apache.chemistry.opencmis.commons.server.CmisService;
@@ -271,6 +273,24 @@ public class CmisBrowserBindingServlet e
} else {
printError(context, e, request, response);
}
+ } catch (Throwable t) {
+ LOG.error(createLogMessage(t, request), t);
+
+ try {
+ response.resetBuffer();
+
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+
response.setContentType(AbstractBrowserServiceCall.JSON_MIME_TYPE);
+ response.setCharacterEncoding(IOUtils.UTF8);
+
+ PrintWriter pw = response.getWriter();
+ pw.print("{\"exception\":\"runtime\",\"message\": \"An error
occurred!\"}");
+ pw.flush();
+ } catch (Exception te) {
+ // we tried to send an error message but it failed.
+ // there is nothing we can do...
+ }
+
+ throw t;
} finally {
// in any case close the content stream if one has been provided
if (request instanceof POSTHttpServletRequestWrapper) {
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractCmisHttpServlet.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractCmisHttpServlet.java?rev=1755607&r1=1755606&r2=1755607&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractCmisHttpServlet.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractCmisHttpServlet.java
Tue Aug 9 14:40:07 2016
@@ -165,10 +165,10 @@ public abstract class AbstractCmisHttpSe
return context;
}
- protected static String createLogMessage(Exception ex, HttpServletRequest
request) {
+ protected static String createLogMessage(Throwable t, HttpServletRequest
request) {
StringBuilder sb = new StringBuilder(256);
- sb.append(ex.getMessage());
+ sb.append(t.getMessage());
sb.append(" (");
sb.append(request.getMethod());