001/*
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 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.server.servlet;
021
022import com.google.common.collect.ListMultimap;
023import com.google.common.collect.MultimapBuilder;
024import jakarta.annotation.Nonnull;
025import jakarta.servlet.http.HttpServletRequest;
026import jakarta.servlet.http.HttpServletResponse;
027
028import java.util.List;
029import java.util.Map;
030
031public class ServletSubRequestDetails extends ServletRequestDetails {
032
033        private final ServletRequestDetails myWrap;
034        /**
035         * Map with case-insensitive keys
036         */
037        private final ListMultimap<String, String> myHeaders = MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
038                        .arrayListValues()
039                        .build();
040
041        /**
042         * Constructor
043         *
044         * @param theRequestDetails The parent request details
045         */
046        public ServletSubRequestDetails(@Nonnull ServletRequestDetails theRequestDetails) {
047                super(theRequestDetails.getInterceptorBroadcaster());
048
049                myWrap = theRequestDetails;
050
051                Map<String, List<String>> headers = theRequestDetails.getHeaders();
052                for (Map.Entry<String, List<String>> next : headers.entrySet()) {
053                        myHeaders.putAll(next.getKey(), next.getValue());
054                }
055        }
056
057        @Override
058        public HttpServletRequest getServletRequest() {
059                return myWrap.getServletRequest();
060        }
061
062        @Override
063        public HttpServletResponse getServletResponse() {
064                return myWrap.getServletResponse();
065        }
066
067        @Override
068        public void addHeader(String theName, String theValue) {
069                myHeaders.put(theName, theValue);
070        }
071
072        @Override
073        public String getHeader(String theName) {
074                List<String> list = myHeaders.get(theName);
075                if (list.isEmpty()) {
076                        return null;
077                }
078                return list.get(0);
079        }
080
081        @Override
082        public List<String> getHeaders(String theName) {
083                List<String> list = myHeaders.get(theName.toLowerCase());
084                if (list.isEmpty()) {
085                        return null;
086                }
087                return list;
088        }
089
090        @Override
091        public Map<Object, Object> getUserData() {
092                return myWrap.getUserData();
093        }
094
095        @Override
096        public boolean isSubRequest() {
097                return true;
098        }
099}