Send correct status code from ResponseHighliterInterceptor

This commit is contained in:
James 2017-01-21 18:03:41 -05:00
parent d1542e98c5
commit d616d20511
2 changed files with 108 additions and 104 deletions

View File

@ -13,7 +13,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -69,6 +69,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
public static final String PARAM_RAW_TRUE = "true"; public static final String PARAM_RAW_TRUE = "true";
public static final String PARAM_TRUE = "true"; public static final String PARAM_TRUE = "true";
private String format(String theResultBody, EncodingEnum theEncodingEnum) { private String format(String theResultBody, EncodingEnum theEncodingEnum) {
String str = StringEscapeUtils.escapeHtml4(theResultBody); String str = StringEscapeUtils.escapeHtml4(theResultBody);
if (str == null || theEncodingEnum == null) { if (str == null || theEncodingEnum == null) {
@ -181,7 +182,8 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
} }
@Override @Override
public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws ServletException, IOException { public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws ServletException, IOException {
/* /*
* It's not a browser... * It's not a browser...
*/ */
@ -209,13 +211,14 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse);
} }
streamResponse(theRequestDetails, theServletResponse, theException.getOperationOutcome(), theServletRequest); streamResponse(theRequestDetails, theServletResponse, theException.getOperationOutcome(), theServletRequest, theException.getStatusCode());
return false; return false;
} }
@Override @Override
public boolean outgoingResponse(RequestDetails theRequestDetails, IBaseResource theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws AuthenticationException { public boolean outgoingResponse(RequestDetails theRequestDetails, IBaseResource theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws AuthenticationException {
/* /*
* Request for _raw * Request for _raw
@ -264,7 +267,6 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse);
} }
/* /*
* Not a GET * Not a GET
*/ */
@ -279,12 +281,12 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse);
} }
streamResponse(theRequestDetails, theServletResponse, theResponseObject, theServletRequest); streamResponse(theRequestDetails, theServletResponse, theResponseObject, theServletRequest, 200);
return false; return false;
} }
private void streamResponse(RequestDetails theRequestDetails, HttpServletResponse theServletResponse, IBaseResource resource, ServletRequest theServletRequest) { private void streamResponse(RequestDetails theRequestDetails, HttpServletResponse theServletResponse, IBaseResource resource, ServletRequest theServletRequest, int theStatusCode) {
IParser p; IParser p;
Map<String, String[]> parameters = theRequestDetails.getParameters(); Map<String, String[]> parameters = theRequestDetails.getParameters();
if (parameters.containsKey(Constants.PARAM_FORMAT)) { if (parameters.containsKey(Constants.PARAM_FORMAT)) {
@ -308,109 +310,111 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
p.setPrettyPrint(prettyPrintResponse); p.setPrettyPrint(prettyPrintResponse);
} }
EncodingEnum encoding = p.getEncoding(); EncodingEnum encoding = p.getEncoding();
String encoded = p.encodeResourceToString(resource); String encoded = p.encodeResourceToString(resource);
theServletResponse.setContentType(Constants.CT_HTML_WITH_UTF8); try {
StringBuilder b = new StringBuilder(); if (theStatusCode > 299) {
b.append("<html lang=\"en\">\n"); theServletResponse.setStatus(theStatusCode);
b.append(" <head>\n"); }
b.append(" <meta charset=\"utf-8\" />\n"); theServletResponse.setContentType(Constants.CT_HTML_WITH_UTF8);
b.append(" <style>\n");
b.append(".hlQuot {\n");
b.append(" color: #88F;\n");
b.append("}\n");
b.append(".hlAttr {\n");
b.append(" color: #888;\n");
b.append("}\n");
b.append(".hlTagName {\n");
b.append(" color: #006699;\n");
b.append("}\n");
b.append(".hlControl {\n");
b.append(" color: #660000;\n");
b.append("}\n");
b.append(".hlText {\n");
b.append(" color: #000000;\n");
b.append("}\n");
b.append(".hlUrlBase {\n");
b.append("}");
b.append(".headersDiv {\n");
b.append(" background: #EEE;");
b.append("}");
b.append(".headerName {\n");
b.append(" color: #888;\n");
b.append(" font-family: monospace;\n");
b.append("}");
b.append(".headerValue {\n");
b.append(" color: #88F;\n");
b.append(" font-family: monospace;\n");
b.append("}");
b.append("BODY {\n");
b.append(" font-family: Arial;\n");
b.append("}");
b.append(" </style>\n");
b.append(" </head>\n");
b.append("\n");
b.append(" <body>");
b.append("<p>"); StringBuilder b = new StringBuilder();
b.append("This result is being rendered in HTML for easy viewing. "); b.append("<html lang=\"en\">\n");
b.append("You may access this content as "); b.append(" <head>\n");
b.append(" <meta charset=\"utf-8\" />\n");
b.append(" <style>\n");
b.append(".hlQuot {\n");
b.append(" color: #88F;\n");
b.append("}\n");
b.append(".hlAttr {\n");
b.append(" color: #888;\n");
b.append("}\n");
b.append(".hlTagName {\n");
b.append(" color: #006699;\n");
b.append("}\n");
b.append(".hlControl {\n");
b.append(" color: #660000;\n");
b.append("}\n");
b.append(".hlText {\n");
b.append(" color: #000000;\n");
b.append("}\n");
b.append(".hlUrlBase {\n");
b.append("}");
b.append(".headersDiv {\n");
b.append(" background: #EEE;");
b.append("}");
b.append(".headerName {\n");
b.append(" color: #888;\n");
b.append(" font-family: monospace;\n");
b.append("}");
b.append(".headerValue {\n");
b.append(" color: #88F;\n");
b.append(" font-family: monospace;\n");
b.append("}");
b.append("BODY {\n");
b.append(" font-family: Arial;\n");
b.append("}");
b.append(" </style>\n");
b.append(" </head>\n");
b.append("\n");
b.append(" <body>");
b.append("<a href=\""); b.append("<p>");
b.append(createLinkHref(parameters, Constants.FORMAT_JSON)); b.append("This result is being rendered in HTML for easy viewing. ");
b.append("\">Raw JSON</a> or "); b.append("You may access this content as ");
b.append("<a href=\""); b.append("<a href=\"");
b.append(createLinkHref(parameters, Constants.FORMAT_XML)); b.append(createLinkHref(parameters, Constants.FORMAT_JSON));
b.append("\">Raw XML</a>, "); b.append("\">Raw JSON</a> or ");
b.append(" or view this content in "); b.append("<a href=\"");
b.append(createLinkHref(parameters, Constants.FORMAT_XML));
b.append("\">Raw XML</a>, ");
b.append("<a href=\""); b.append(" or view this content in ");
b.append(createLinkHref(parameters, Constants.FORMATS_HTML_JSON));
b.append("\">HTML JSON</a> ");
b.append("or "); b.append("<a href=\"");
b.append("<a href=\""); b.append(createLinkHref(parameters, Constants.FORMATS_HTML_JSON));
b.append(createLinkHref(parameters, Constants.FORMATS_HTML_XML)); b.append("\">HTML JSON</a> ");
b.append("\">HTML XML</a>.");
Date startTime = (Date) theServletRequest.getAttribute(RestfulServer.REQUEST_START_TIME); b.append("or ");
if (startTime != null) { b.append("<a href=\"");
long time = System.currentTimeMillis() - startTime.getTime(); b.append(createLinkHref(parameters, Constants.FORMATS_HTML_XML));
b.append(" Response generated in "); b.append("\">HTML XML</a>.");
b.append(time);
b.append("ms.");
}
Date startTime = (Date) theServletRequest.getAttribute(RestfulServer.REQUEST_START_TIME);
if (startTime != null) {
long time = System.currentTimeMillis() - startTime.getTime();
b.append(" Response generated in ");
b.append(time);
b.append("ms.");
}
b.append("</p>"); b.append("</p>");
b.append("\n"); b.append("\n");
// if (isEncodeHeaders()) { // if (isEncodeHeaders()) {
// b.append("<h1>Request Headers</h1>"); // b.append("<h1>Request Headers</h1>");
// b.append("<div class=\"headersDiv\">"); // b.append("<div class=\"headersDiv\">");
// for (int next : theRequestDetails.get) // for (int next : theRequestDetails.get)
// b.append("</div>"); // b.append("</div>");
// b.append("<h1>Response Headers</h1>"); // b.append("<h1>Response Headers</h1>");
// b.append("<div class=\"headersDiv\">"); // b.append("<div class=\"headersDiv\">");
// b.append("</div>"); // b.append("</div>");
// b.append("<h1>Response Body</h1>"); // b.append("<h1>Response Body</h1>");
// } // }
b.append("<pre>"); b.append("<pre>");
b.append(format(encoded, encoding)); b.append(format(encoded, encoding));
b.append("</pre>"); b.append("</pre>");
b.append(" </body>"); b.append(" </body>");
b.append("</html>"); b.append("</html>");
//@formatter:off //@formatter:off
String out = b.toString(); String out = b.toString();
//@formatter:on //@formatter:on
try {
theServletResponse.getWriter().append(out); theServletResponse.getWriter().append(out);
theServletResponse.getWriter().close(); theServletResponse.getWriter().close();
} catch (IOException e) { } catch (IOException e) {

View File

@ -157,7 +157,7 @@ public class ResponseHighlightingInterceptorTest {
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Resp: {}", responseContent); ourLog.info("Resp: {}", responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(400, status.getStatusLine().getStatusCode());
assertThat(responseContent, stringContainsInOrder("<span class='hlTagName'>OperationOutcome</span>", "Unknown resource type 'Foobar' - Server knows how to handle")); assertThat(responseContent, stringContainsInOrder("<span class='hlTagName'>OperationOutcome</span>", "Unknown resource type 'Foobar' - Server knows how to handle"));
@ -188,7 +188,7 @@ public class ResponseHighlightingInterceptorTest {
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Resp: {}", responseContent); ourLog.info("Resp: {}", responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(400, status.getStatusLine().getStatusCode());
assertThat(responseContent, stringContainsInOrder("<span class='hlTagName'>OperationOutcome</span>", "This is the base URL of FHIR server. Unable to handle this request, as it does not contain a resource type or operation name.")); assertThat(responseContent, stringContainsInOrder("<span class='hlTagName'>OperationOutcome</span>", "This is the base URL of FHIR server. Unable to handle this request, as it does not contain a resource type or operation name."));