View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.fhir;
18  
19  import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
20  import ca.uhn.fhir.rest.api.server.RequestDetails;
21  import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
22  import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;
23  import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  /**
29   * Track the {@link RequestDetails} and make it available to downstream processing. This
30   * needs to be thread local, because the request details are normally not forwarded to
31   * the resource provider methods.
32   *
33   * @author Christian Ohr
34   * @since 3.5
35   */
36  public class RequestDetailProvider extends InterceptorAdapter {
37  
38      private static ThreadLocal<RequestDetails> requestDetails = new ThreadLocal<>();
39  
40      public static RequestDetails getRequestDetails() {
41          return requestDetails.get();
42      }
43  
44      @Override
45      public void incomingRequestPreHandled(RestOperationTypeEnum operation, ActionRequestDetails processedRequest) {
46          requestDetails.set(processedRequest.getRequestDetails());
47      }
48  
49      @Override
50      public void processingCompletedNormally(ServletRequestDetails theRequestDetails) {
51          requestDetails.set(null);
52      }
53  
54      @Override
55      public boolean handleException(RequestDetails theRequestDetails,
56                                     BaseServerResponseException theException,
57                                     HttpServletRequest theServletRequest,
58                                     HttpServletResponse theServletResponse) {
59          requestDetails.set(null);
60          return true;
61      }
62  
63  }