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