From 5a9fa4e549c70d188fee9486ff68b2f7bca5c858 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 24 Apr 2019 21:19:47 -0400 Subject: [PATCH] Interceptor tweaks --- hapi-deployable-pom/pom.xml | 1 - .../IServerOperationInterceptor.java | 4 +-- .../server/interceptor/InterceptorOrders.java | 33 ++++++++++++++++++ .../interceptor/LoggingInterceptor.java | 4 +-- .../ResponseHighlighterInterceptor.java | 34 +++++++++++-------- .../ServeMediaResourceRawInterceptor.java | 8 +++-- 6 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/InterceptorOrders.java diff --git a/hapi-deployable-pom/pom.xml b/hapi-deployable-pom/pom.xml index c69e244c5a8..21c46abf5b9 100644 --- a/hapi-deployable-pom/pom.xml +++ b/hapi-deployable-pom/pom.xml @@ -202,7 +202,6 @@ true false false - -Xdoclint:none -Xdoclint:none diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/IServerOperationInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/IServerOperationInterceptor.java index 27f4d7f00eb..48593df8e65 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/IServerOperationInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/IServerOperationInterceptor.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.rest.server.interceptor; * 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. diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/InterceptorOrders.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/InterceptorOrders.java new file mode 100644 index 00000000000..4d4d60d238d --- /dev/null +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/InterceptorOrders.java @@ -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 + } + +} diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptor.java index 56d1640e1a8..16fb683615c 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptor.java @@ -37,9 +37,9 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; * 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. diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java index cef5ea66b4b..77096fa1fa1 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java @@ -1,6 +1,9 @@ package ca.uhn.fhir.rest.server.interceptor; 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.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; @@ -60,7 +63,8 @@ import static org.apache.commons.lang3.StringUtils.*; * * @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 @@ -230,7 +234,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { 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) throws ServletException, IOException { /* @@ -238,7 +242,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { */ Set accept = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest); 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"); if (requestedWith != null) { - return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); + return true; } /* * Not a GET */ if (theRequestDetails.getRequestType() != RequestTypeEnum.GET) { - return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); + return true; } if (theException.getOperationOutcome() == null) { - return super.handleException(theRequestDetails, theException, theServletRequest, theServletResponse); + return true; } streamResponse(theRequestDetails, theServletResponse, theException.getOperationOutcome(), theServletRequest, theException.getStatusCode()); @@ -305,7 +309,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { 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) throws AuthenticationException { @@ -315,7 +319,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { String[] rawParamValues = theRequestDetails.getParameters().get(PARAM_RAW); 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"); - return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); + return true; } boolean force = false; @@ -337,7 +341,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { force = true; theRequestDetails.addParameter(Constants.PARAM_FORMAT, PARAM_FORMAT_VALUE_JSON); } else { - return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); + return true; } } @@ -346,34 +350,34 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter { */ Set highestRankedAcceptValues = RestfulServerUtils.parseAcceptHeaderAndReturnHighestRankedOptions(theServletRequest); 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 */ 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 (!force && isNotBlank(theServletRequest.getHeader(Constants.HEADER_ORIGIN))) { - return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); + return true; } /* * Not a GET */ if (!force && theRequestDetails.getRequestType() != RequestTypeEnum.GET) { - return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); + return true; } /* * Not binary */ if (!force && (theResponseObject.getResponseResource() instanceof IBaseBinary)) { - return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse); + return true; } 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 { - double kb = ((double)theLength) / FileUtils.ONE_KB; + double kb = ((double) theLength) / FileUtils.ONE_KB; if (kb <= 1000) { theServletResponse.getWriter().append(String.format("%.1f", kb)).append(" KB"); } else { diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptor.java index fef270671db..3cff6c9c3e3 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptor.java @@ -21,6 +21,9 @@ package ca.uhn.fhir.rest.server.interceptor; */ 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.RestOperationTypeEnum; import ca.uhn.fhir.rest.api.server.RequestDetails; @@ -49,7 +52,8 @@ import static org.apache.commons.lang3.StringUtils.isBlank; *
  • The client explicitly requests raw output by adding the parameter _output=data
  • * */ -public class ServeMediaResourceRawInterceptor extends InterceptorAdapter { +@Interceptor +public class ServeMediaResourceRawInterceptor { 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); } - @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 { if (theResponseObject == null) { return true;