Interceptor tweaks

This commit is contained in:
James Agnew 2019-04-24 21:19:47 -04:00
parent c8c81c2ebc
commit 5a9fa4e549
6 changed files with 62 additions and 22 deletions

View File

@ -202,7 +202,6 @@
<linksource>true</linksource> <linksource>true</linksource>
<verbose>false</verbose> <verbose>false</verbose>
<debug>false</debug> <debug>false</debug>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalJOption>-Xdoclint:none</additionalJOption> <additionalJOption>-Xdoclint:none</additionalJOption>
</configuration> </configuration>
<executions> <executions>

View File

@ -0,0 +1,33 @@
package ca.uhn.fhir.rest.server.interceptor;
/*-
* #%L
* HAPI FHIR - Server Framework
* %%
* Copyright (C) 2014 - 2019 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
public class InterceptorOrders {
public static final int SERVE_MEDIA_RESOURCE_RAW_INTERCEPTOR = 1000;
public static final int RESPONSE_HIGHLIGHTER_INTERCEPTOR = 10000;
/** Non instantiable */
private InterceptorOrders() {
// nothing
}
}

View File

@ -1,6 +1,9 @@
package ca.uhn.fhir.rest.server.interceptor; package ca.uhn.fhir.rest.server.interceptor;
import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.EncodingEnum;
@ -60,7 +63,8 @@ import static org.apache.commons.lang3.StringUtils.*;
* *
* @since 1.0 * @since 1.0
*/ */
public class ResponseHighlighterInterceptor extends InterceptorAdapter { @Interceptor
public class ResponseHighlighterInterceptor {
/** /**
* TODO: As of HAPI 1.6 (2016-06-10) this parameter has been replaced with simply * TODO: As of HAPI 1.6 (2016-06-10) this parameter has been replaced with simply
@ -230,7 +234,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
return lineCount; return lineCount;
} }
@Override @Hook(value = Pointcut.SERVER_HANDLE_EXCEPTION, order = InterceptorOrders.RESPONSE_HIGHLIGHTER_INTERCEPTOR)
public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws ServletException, IOException { throws ServletException, IOException {
/* /*
@ -238,7 +242,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
*/ */
Set<String> accept = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest); Set<String> accept = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest);
if (!accept.contains(Constants.CT_HTML)) { if (!accept.contains(Constants.CT_HTML)) {
return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); return true;
} }
/* /*
@ -246,18 +250,18 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
*/ */
String requestedWith = theServletRequest.getHeader("X-Requested-With"); String requestedWith = theServletRequest.getHeader("X-Requested-With");
if (requestedWith != null) { if (requestedWith != null) {
return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); return true;
} }
/* /*
* Not a GET * Not a GET
*/ */
if (theRequestDetails.getRequestType() != RequestTypeEnum.GET) { if (theRequestDetails.getRequestType() != RequestTypeEnum.GET) {
return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); return true;
} }
if (theException.getOperationOutcome() == null) { if (theException.getOperationOutcome() == null) {
return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); return true;
} }
streamResponse(theRequestDetails, theServletResponse, theException.getOperationOutcome(), theServletRequest, theException.getStatusCode()); streamResponse(theRequestDetails, theServletResponse, theException.getOperationOutcome(), theServletRequest, theException.getStatusCode());
@ -305,7 +309,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
return this; return this;
} }
@Override @Hook(value = Pointcut.SERVER_OUTGOING_RESPONSE, order = InterceptorOrders.RESPONSE_HIGHLIGHTER_INTERCEPTOR)
public boolean outgoingResponse(RequestDetails theRequestDetails, ResponseDetails theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) public boolean outgoingResponse(RequestDetails theRequestDetails, ResponseDetails theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws AuthenticationException { throws AuthenticationException {
@ -315,7 +319,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
String[] rawParamValues = theRequestDetails.getParameters().get(PARAM_RAW); String[] rawParamValues = theRequestDetails.getParameters().get(PARAM_RAW);
if (rawParamValues != null && rawParamValues.length > 0 && rawParamValues[0].equals(PARAM_RAW_TRUE)) { if (rawParamValues != null && rawParamValues.length > 0 && rawParamValues[0].equals(PARAM_RAW_TRUE)) {
ourLog.warn("Client is using non-standard/legacy _raw parameter - Use _format=json or _format=xml instead, as this parmameter will be removed at some point"); ourLog.warn("Client is using non-standard/legacy _raw parameter - Use _format=json or _format=xml instead, as this parmameter will be removed at some point");
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
boolean force = false; boolean force = false;
@ -337,7 +341,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
force = true; force = true;
theRequestDetails.addParameter(Constants.PARAM_FORMAT, PARAM_FORMAT_VALUE_JSON); theRequestDetails.addParameter(Constants.PARAM_FORMAT, PARAM_FORMAT_VALUE_JSON);
} else { } else {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
} }
@ -346,34 +350,34 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
*/ */
Set<String> highestRankedAcceptValues = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest); Set<String> highestRankedAcceptValues = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest);
if (!force && highestRankedAcceptValues.contains(Constants.CT_HTML) == false) { if (!force && highestRankedAcceptValues.contains(Constants.CT_HTML) == false) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
/* /*
* It's an AJAX request, so no HTML * It's an AJAX request, so no HTML
*/ */
if (!force && isNotBlank(theServletRequest.getHeader("X-Requested-With"))) { if (!force && isNotBlank(theServletRequest.getHeader("X-Requested-With"))) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
/* /*
* If the request has an Origin header, it is probably an AJAX request * If the request has an Origin header, it is probably an AJAX request
*/ */
if (!force && isNotBlank(theServletRequest.getHeader(Constants.HEADER_ORIGIN))) { if (!force && isNotBlank(theServletRequest.getHeader(Constants.HEADER_ORIGIN))) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
/* /*
* Not a GET * Not a GET
*/ */
if (!force && theRequestDetails.getRequestType() != RequestTypeEnum.GET) { if (!force && theRequestDetails.getRequestType() != RequestTypeEnum.GET) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
/* /*
* Not binary * Not binary
*/ */
if (!force && (theResponseObject.getResponseResource() instanceof IBaseBinary)) { if (!force && (theResponseObject.getResponseResource() instanceof IBaseBinary)) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); return true;
} }
streamResponse(theRequestDetails, theServletResponse, theResponseObject.getResponseResource(), theServletRequest, 200); streamResponse(theRequestDetails, theServletResponse, theResponseObject.getResponseResource(), theServletRequest, 200);
@ -665,7 +669,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
} }
private void writeLength(HttpServletResponse theServletResponse, int theLength) throws IOException { private void writeLength(HttpServletResponse theServletResponse, int theLength) throws IOException {
double kb = ((double)theLength) / FileUtils.ONE_KB; double kb = ((double) theLength) / FileUtils.ONE_KB;
if (kb <= 1000) { if (kb <= 1000) {
theServletResponse.getWriter().append(String.format("%.1f", kb)).append(" KB"); theServletResponse.getWriter().append(String.format("%.1f", kb)).append(" KB");
} else { } else {

View File

@ -21,6 +21,9 @@ package ca.uhn.fhir.rest.server.interceptor;
*/ */
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum; import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.RequestDetails;
@ -49,7 +52,8 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
* <li>The client explicitly requests raw output by adding the parameter <code>_output=data</code></li> * <li>The client explicitly requests raw output by adding the parameter <code>_output=data</code></li>
* </ul> * </ul>
*/ */
public class ServeMediaResourceRawInterceptor extends InterceptorAdapter { @Interceptor
public class ServeMediaResourceRawInterceptor {
public static final String MEDIA_CONTENT_CONTENT_TYPE_OPT = "Media.content.contentType"; public static final String MEDIA_CONTENT_CONTENT_TYPE_OPT = "Media.content.contentType";
@ -62,7 +66,7 @@ public class ServeMediaResourceRawInterceptor extends InterceptorAdapter {
RESPOND_TO_OPERATION_TYPES = Collections.unmodifiableSet(respondToOperationTypes); RESPOND_TO_OPERATION_TYPES = Collections.unmodifiableSet(respondToOperationTypes);
} }
@Override @Hook(value=Pointcut.SERVER_OUTGOING_RESPONSE, order = InterceptorOrders.SERVE_MEDIA_RESOURCE_RAW_INTERCEPTOR)
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 {
if (theResponseObject == null) { if (theResponseObject == null) {
return true; return true;