
001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.api.server; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.context.api.AddProfileTagEnum; 024import ca.uhn.fhir.interceptor.api.HookParams; 025import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster; 026import ca.uhn.fhir.interceptor.api.IInterceptorService; 027import ca.uhn.fhir.interceptor.api.Pointcut; 028import ca.uhn.fhir.interceptor.model.RequestPartitionId; 029import ca.uhn.fhir.rest.api.EncodingEnum; 030import ca.uhn.fhir.rest.server.ETagSupportEnum; 031import ca.uhn.fhir.rest.server.ElementsSupportEnum; 032import ca.uhn.fhir.rest.server.IPagingProvider; 033import ca.uhn.fhir.rest.server.IRestfulServerDefaults; 034import ca.uhn.fhir.rest.server.RestfulServer; 035import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; 036import com.google.common.collect.ArrayListMultimap; 037import com.google.common.collect.ImmutableListMultimap; 038import com.google.common.collect.ListMultimap; 039 040import java.io.IOException; 041import java.io.InputStream; 042import java.io.Reader; 043import java.nio.charset.Charset; 044import java.util.List; 045 046import static java.util.Objects.nonNull; 047 048/** 049 * A default RequestDetails implementation that can be used for system calls to 050 * Resource DAO methods when partitioning is enabled. Using a SystemRequestDetails 051 * instance for system calls will ensure that any resource queries or updates will 052 * use the DEFAULT partition when partitioning is enabled. 053 */ 054public class SystemRequestDetails extends RequestDetails { 055 private FhirContext myFhirContext; 056 private ListMultimap<String, String> myHeaders; 057 /** 058 * If a SystemRequestDetails has a RequestPartitionId, it will take precedence over the tenantId 059 */ 060 private RequestPartitionId myRequestPartitionId; 061 062 private IRestfulServerDefaults myServer = new MyRestfulServerDefaults(); 063 064 public SystemRequestDetails() { 065 this(new MyInterceptorBroadcaster()); 066 } 067 068 public SystemRequestDetails(IInterceptorBroadcaster theInterceptorBroadcaster) { 069 super(theInterceptorBroadcaster); 070 } 071 072 public SystemRequestDetails(RequestDetails theDetails) { 073 super(theDetails); 074 if (nonNull(theDetails.getServer())) { 075 myServer = theDetails.getServer(); 076 myFhirContext = theDetails.getFhirContext(); 077 } 078 } 079 080 public RequestPartitionId getRequestPartitionId() { 081 return myRequestPartitionId; 082 } 083 084 public SystemRequestDetails setRequestPartitionId(RequestPartitionId theRequestPartitionId) { 085 myRequestPartitionId = theRequestPartitionId; 086 return this; 087 } 088 089 @Override 090 protected byte[] getByteStreamRequestContents() { 091 return new byte[0]; 092 } 093 094 @Override 095 public Charset getCharset() { 096 return null; 097 } 098 099 @Override 100 public FhirContext getFhirContext() { 101 return myFhirContext; 102 } 103 104 public void setFhirContext(FhirContext theFhirContext) { 105 myFhirContext = theFhirContext; 106 } 107 108 @Override 109 public String getHeader(String name) { 110 List<String> headers = getHeaders(name); 111 if (headers.isEmpty()) { 112 return null; 113 } else { 114 return headers.get(0); 115 } 116 } 117 118 @Override 119 public List<String> getHeaders(String name) { 120 ListMultimap<String, String> headers = myHeaders; 121 if (headers == null) { 122 headers = ImmutableListMultimap.of(); 123 } 124 return headers.get(name); 125 } 126 127 public void addHeader(String theName, String theValue) { 128 if (myHeaders == null) { 129 myHeaders = ArrayListMultimap.create(); 130 } 131 myHeaders.put(theName, theValue); 132 } 133 134 @Override 135 public Object getAttribute(String theAttributeName) { 136 return null; 137 } 138 139 @Override 140 public void setAttribute(String theAttributeName, Object theAttributeValue) {} 141 142 @Override 143 public InputStream getInputStream() throws IOException { 144 return null; 145 } 146 147 @Override 148 public Reader getReader() throws IOException { 149 return null; 150 } 151 152 @Override 153 public IRestfulServerDefaults getServer() { 154 return myServer; 155 } 156 157 public void setServer(RestfulServer theServer) { 158 this.myServer = theServer; 159 } 160 161 @Override 162 public String getServerBaseForRequest() { 163 return null; 164 } 165 166 private static class MyRestfulServerDefaults implements IRestfulServerDefaults { 167 168 @Override 169 public AddProfileTagEnum getAddProfileTag() { 170 return null; 171 } 172 173 @Override 174 public EncodingEnum getDefaultResponseEncoding() { 175 return null; 176 } 177 178 @Override 179 public ETagSupportEnum getETagSupport() { 180 return null; 181 } 182 183 @Override 184 public ElementsSupportEnum getElementsSupport() { 185 return null; 186 } 187 188 @Override 189 public FhirContext getFhirContext() { 190 return null; 191 } 192 193 @Override 194 public List<IServerInterceptor> getInterceptors_() { 195 return null; 196 } 197 198 @Override 199 public IPagingProvider getPagingProvider() { 200 return null; 201 } 202 203 @Override 204 public boolean isDefaultPrettyPrint() { 205 return false; 206 } 207 208 @Override 209 public IInterceptorService getInterceptorService() { 210 return null; 211 } 212 } 213 214 private static class MyInterceptorBroadcaster implements IInterceptorBroadcaster { 215 216 @Override 217 public boolean callHooks(Pointcut thePointcut, HookParams theParams) { 218 return true; 219 } 220 221 @Override 222 public Object callHooksAndReturnObject(Pointcut thePointcut, HookParams theParams) { 223 return null; 224 } 225 226 @Override 227 public boolean hasHooks(Pointcut thePointcut) { 228 return false; 229 } 230 } 231 232 public static SystemRequestDetails forAllPartitions() { 233 return new SystemRequestDetails().setRequestPartitionId(RequestPartitionId.allPartitions()); 234 } 235 236 public static SystemRequestDetails newSystemRequestAllPartitions() { 237 SystemRequestDetails systemRequestDetails = new SystemRequestDetails(); 238 systemRequestDetails.setRequestPartitionId(RequestPartitionId.allPartitions()); 239 return systemRequestDetails; 240 } 241}