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