001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.fhirgw.model;
011
012import ca.uhn.fhir.rest.client.api.IClientInterceptor;
013import com.google.common.collect.ArrayListMultimap;
014import com.google.common.collect.ImmutableListMultimap;
015import com.google.common.collect.ListMultimap;
016import com.google.common.collect.Multimap;
017import com.google.common.collect.Multimaps;
018import jakarta.annotation.Nonnull;
019import jakarta.servlet.http.HttpServletRequest;
020import org.apache.commons.lang3.Validate;
021
022public class BaseRequest<T extends IBaseRequest> implements IBaseRequest {
023        static final ImmutableListMultimap<String, String> EMPTY_STRING_MULTIMAP = ImmutableListMultimap.of();
024        private HttpServletRequest myServletRequest;
025        private String myResourceType;
026        private ListMultimap<String, String> myAdditionalHeaders = EMPTY_STRING_MULTIMAP;
027        private boolean mySkip;
028        private boolean myServerCapabilityStatementValidationEnabled = true;
029        private IClientInterceptor myClientInterceptor;
030
031        /**
032         * Constructor
033         */
034        public BaseRequest() {
035                // nothing
036        }
037
038        /**
039         * Copy constructor
040         */
041        public BaseRequest(IBaseRequest theRequest) {
042                myServletRequest = theRequest.getServletRequest();
043                myResourceType = theRequest.getResourceType();
044                if (!theRequest.getAdditionalHeaders().isEmpty()) {
045                        myAdditionalHeaders = ArrayListMultimap.create();
046                        myAdditionalHeaders.putAll(theRequest.getAdditionalHeaders());
047                }
048                mySkip = theRequest.isSkip();
049                myServerCapabilityStatementValidationEnabled = theRequest.getServerCapabilityStatementValidationEnabled();
050                myClientInterceptor = theRequest.getClientInterceptor();
051        }
052
053        /**
054         * Should the request be skipped? If this flag is set in an interceptor prior to calling
055         * a target server, that server will be skipped.
056         */
057        @Override
058        public boolean isSkip() {
059                return mySkip;
060        }
061
062        /**
063         * Should the request be skipped? If this flag is set in an interceptor prior to calling
064         * a target server, that server will be skipped.
065         */
066        public void setSkip(boolean theSkip) {
067                mySkip = theSkip;
068        }
069
070        @Override
071        public boolean getServerCapabilityStatementValidationEnabled() {
072                return myServerCapabilityStatementValidationEnabled;
073        }
074
075        public void setServerCapabilityStatementValidationEnabled(boolean theServerCapabilityStatementValidationEnabled) {
076                myServerCapabilityStatementValidationEnabled = theServerCapabilityStatementValidationEnabled;
077        }
078
079        @Override
080        public Multimap<String, String> getAdditionalHeaders() {
081                return Multimaps.unmodifiableMultimap(myAdditionalHeaders);
082        }
083
084        /**
085         * Adds an HTTP header that will be added to the request sent to the target
086         *
087         * @param theName  The header name (must not be <code>null</code> or empty)
088         * @param theValue The header value (must not be <code>null</code>)
089         */
090        public void addAdditionalHeader(@Nonnull String theName, String theValue) {
091                Validate.notBlank(theName, "theName must not be empty");
092                Validate.notNull(theValue, "theValue must not be null");
093                if (myAdditionalHeaders.isEmpty()) {
094                        myAdditionalHeaders = ArrayListMultimap.create();
095                }
096                myAdditionalHeaders.put(theName, theValue);
097        }
098
099        @Override
100        public HttpServletRequest getServletRequest() {
101                return myServletRequest;
102        }
103
104        @Override
105        public T setServletRequest(HttpServletRequest theServletRequest) {
106                myServletRequest = theServletRequest;
107                return (T) this;
108        }
109
110        @Override
111        public String getResourceType() {
112                return myResourceType;
113        }
114
115        @Override
116        public T setResourceType(String theResourceType) {
117                myResourceType = theResourceType;
118                return (T) this;
119        }
120
121        @Override
122        public IClientInterceptor getClientInterceptor() {
123                return myClientInterceptor;
124        }
125
126        public void setClientInterceptor(IClientInterceptor theClientInterceptor) {
127                myClientInterceptor = theClientInterceptor;
128        }
129}