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

@ -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,10 +310,14 @@ 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);
try {
if (theStatusCode > 299) {
theServletResponse.setStatus(theStatusCode);
}
theServletResponse.setContentType(Constants.CT_HTML_WITH_UTF8); theServletResponse.setContentType(Constants.CT_HTML_WITH_UTF8);
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
@ -386,7 +392,6 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
b.append("ms."); b.append("ms.");
} }
b.append("</p>"); b.append("</p>");
b.append("\n"); b.append("\n");
@ -410,7 +415,6 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
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."));