001package ca.uhn.fhir.rest.server.interceptor.partition;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
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.i18n.Msg;
024import ca.uhn.fhir.interceptor.api.Hook;
025import ca.uhn.fhir.interceptor.api.Interceptor;
026import ca.uhn.fhir.interceptor.api.Pointcut;
027import ca.uhn.fhir.interceptor.model.RequestPartitionId;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
030import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
031import ca.uhn.fhir.rest.server.tenant.ITenantIdentificationStrategy;
032
033import javax.annotation.Nonnull;
034
035import static org.apache.commons.lang3.StringUtils.isBlank;
036
037/**
038 * This interceptor uses the request tenant ID (as supplied to the server using
039 * {@link ca.uhn.fhir.rest.server.RestfulServer#setTenantIdentificationStrategy(ITenantIdentificationStrategy)}
040 * to indicate the partition ID. With this interceptor registered, The server treats the tenant name
041 * supplied by the {@link ITenantIdentificationStrategy tenant identification strategy} as a partition name.
042 * <p>
043 * Partition names (aka tenant IDs) must be registered in advance using the partition management operations.
044 * </p>
045 *
046 * @since 5.0.0
047 */
048@Interceptor
049public class RequestTenantPartitionInterceptor {
050
051        @Hook(Pointcut.STORAGE_PARTITION_IDENTIFY_CREATE)
052        public RequestPartitionId PartitionIdentifyCreate(RequestDetails theRequestDetails) {
053                return extractPartitionIdFromRequest(theRequestDetails);
054        }
055
056        @Hook(Pointcut.STORAGE_PARTITION_IDENTIFY_READ)
057        public RequestPartitionId PartitionIdentifyRead(RequestDetails theRequestDetails) {
058                return extractPartitionIdFromRequest(theRequestDetails);
059        }
060
061        @Nonnull
062        protected RequestPartitionId extractPartitionIdFromRequest(RequestDetails theRequestDetails) {
063
064                // We will use the tenant ID that came from the request as the partition name
065                String tenantId = theRequestDetails.getTenantId();
066                if (isBlank(tenantId)) {
067                        throw new InternalErrorException(Msg.code(343) + "No tenant ID has been specified");
068                }
069
070                return RequestPartitionId.fromPartitionName(tenantId);
071        }
072
073
074}