View Javadoc
1   package ca.uhn.fhir.jpa.provider;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import ca.uhn.fhir.jpa.util.ExpungeOptions;
5   import ca.uhn.fhir.jpa.util.ExpungeOutcome;
6   import ca.uhn.fhir.jpa.util.JpaConstants;
7   import ca.uhn.fhir.rest.param.DateRangeParam;
8   import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
9   import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
10  import org.apache.commons.lang3.StringUtils;
11  import org.hl7.fhir.instance.model.api.IPrimitiveType;
12  import org.hl7.fhir.r4.model.IntegerType;
13  import org.hl7.fhir.r4.model.Parameters;
14  import org.jboss.logging.MDC;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import java.util.Date;
18  import java.util.Enumeration;
19  import java.util.Set;
20  import java.util.TreeSet;
21  
22  /*
23   * #%L
24   * HAPI FHIR JPA Server
25   * %%
26   * Copyright (C) 2014 - 2018 University Health Network
27   * %%
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   * 
32   *      http://www.apache.org/licenses/LICENSE-2.0
33   * 
34   * Unless required by applicable law or agreed to in writing, software
35   * distributed under the License is distributed on an "AS IS" BASIS,
36   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37   * See the License for the specific language governing permissions and
38   * limitations under the License.
39   * #L%
40   */
41  
42  public class BaseJpaProvider {
43  	public static final String REMOTE_ADDR = "req.remoteAddr";
44  	public static final String REMOTE_UA = "req.userAgent";
45  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseJpaProvider.class);
46  	private FhirContext myContext;
47  
48  	protected ExpungeOptions createExpungeOptions(IPrimitiveType<? extends Integer> theLimit, IPrimitiveType<? extends Boolean> theExpungeDeletedResources, IPrimitiveType<? extends Boolean> theExpungeOldVersions, IPrimitiveType<? extends Boolean> theExpungeEverything) {
49  		ExpungeOptions options = new ExpungeOptions();
50  		if (theLimit != null && theLimit.getValue() != null) {
51  			options.setLimit(theLimit.getValue());
52  		}
53  
54  		if (theExpungeOldVersions != null && theExpungeOldVersions.getValue() != null) {
55  			options.setExpungeOldVersions(theExpungeOldVersions.getValue());
56  		}
57  
58  		if (theExpungeDeletedResources != null && theExpungeDeletedResources.getValue() != null) {
59  			options.setExpungeDeletedResources(theExpungeDeletedResources.getValue());
60  		}
61  
62  		if (theExpungeEverything != null && theExpungeEverything.getValue() != null) {
63  			options.setExpungeEverything(theExpungeEverything.getValue());
64  		}
65  		return options;
66  	}
67  
68  	protected Parameters createExpungeResponse(ExpungeOutcome theOutcome) {
69  		Parameters retVal = new Parameters();
70  		retVal
71  			.addParameter()
72  			.setName(JpaConstants.OPERATION_EXPUNGE_OUT_PARAM_EXPUNGE_COUNT)
73  			.setValue(new IntegerType(theOutcome.getDeletedCount()));
74  		return retVal;
75  	}
76  
77  	/**
78  	 * @param theRequest The servlet request
79  	 */
80  	public void endRequest(HttpServletRequest theRequest) {
81  		MDC.remove(REMOTE_ADDR);
82  		MDC.remove(REMOTE_UA);
83  	}
84  
85  	public void endRequest(ServletRequestDetails theRequest) {
86  		endRequest(theRequest.getServletRequest());
87  	}
88  
89  	public FhirContext getContext() {
90  		return myContext;
91  	}
92  
93  	public void setContext(FhirContext theContext) {
94  		myContext = theContext;
95  	}
96  
97  	protected DateRangeParam processSinceOrAt(Date theSince, DateRangeParam theAt) {
98  		boolean haveAt = theAt != null && (theAt.getLowerBoundAsInstant() != null || theAt.getUpperBoundAsInstant() != null);
99  		if (haveAt && theSince != null) {
100 			String msg = getContext().getLocalizer().getMessage(BaseJpaProvider.class, "cantCombintAtAndSince");
101 			throw new InvalidRequestException(msg);
102 		}
103 
104 		if (haveAt) {
105 			return theAt;
106 		}
107 
108 		return new DateRangeParam(theSince, null);
109 	}
110 
111 	public void startRequest(HttpServletRequest theRequest) {
112 		if (theRequest == null) {
113 			return;
114 		}
115 
116 		Set<String> headerNames = new TreeSet<String>();
117 		for (Enumeration<String> enums = theRequest.getHeaderNames(); enums.hasMoreElements(); ) {
118 			headerNames.add(enums.nextElement());
119 		}
120 		ourLog.debug("Request headers: {}", headerNames);
121 
122 		Enumeration<String> forwardedFors = theRequest.getHeaders("x-forwarded-for");
123 		StringBuilder b = new StringBuilder();
124 		for (Enumeration<String> enums = forwardedFors; enums != null && enums.hasMoreElements(); ) {
125 			if (b.length() > 0) {
126 				b.append(" / ");
127 			}
128 			b.append(enums.nextElement());
129 		}
130 
131 		String forwardedFor = b.toString();
132 		String ip = theRequest.getRemoteAddr();
133 		if (StringUtils.isBlank(forwardedFor)) {
134 			org.slf4j.MDC.put(REMOTE_ADDR, ip);
135 			ourLog.debug("Request is from address: {}", ip);
136 		} else {
137 			org.slf4j.MDC.put(REMOTE_ADDR, forwardedFor);
138 			ourLog.debug("Request is from forwarded address: {}", forwardedFor);
139 		}
140 
141 		String userAgent = StringUtils.defaultString(theRequest.getHeader("user-agent"));
142 		org.slf4j.MDC.put(REMOTE_UA, userAgent);
143 
144 	}
145 
146 	public void startRequest(ServletRequestDetails theRequest) {
147 		startRequest(theRequest.getServletRequest());
148 	}
149 
150 
151 }