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>
<verbose>false</verbose>
<debug>false</debug>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
<executions>

View File

@ -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.

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

@ -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.

View File

@ -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<String> 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<String> 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 {

View File

@ -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;
* <li>The client explicitly requests raw output by adding the parameter <code>_output=data</code></li>
* </ul>
*/
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;