package ca.uhn.fhir.rest.server.servlet;

/*
 * #%L
 * HAPI FHIR - Server Framework
 * %%
 * Copyright (C) 2014 - 2019 University Health Network
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServletSubRequestDetails extends ServletRequestDetails {

	private Map<String, List<String>> myHeaders = new HashMap<>();

	/**
	 * Constructor
	 *
	 * @param theRequestDetails The parent request details
	 */
	public ServletSubRequestDetails(ServletRequestDetails theRequestDetails) {
		super(theRequestDetails.getInterceptorBroadcaster());
		if (theRequestDetails != null) {
			Map<String, List<String>> headers = theRequestDetails.getHeaders();
			for (Map.Entry<String, List<String>> next : headers.entrySet()) {
				myHeaders.put(next.getKey().toLowerCase(), next.getValue());
			}
		}
	}

	public void addHeader(String theName, String theValue) {
		String lowerCase = theName.toLowerCase();
		List<String> list = myHeaders.get(lowerCase);
		if (list == null) {
			list = new ArrayList<>();
			myHeaders.put(lowerCase, list);
		}
		list.add(theValue);
	}
	
	@Override
	public String getHeader(String theName) {
		List<String> list = myHeaders.get(theName.toLowerCase());
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<String> getHeaders(String theName) {
		List<String> list = myHeaders.get(theName.toLowerCase());
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list;
	}

	@Override
	public boolean isSubRequest() {
		return true;
	}

}

@@ -883,16 +883,63 @@
 ails %7B%0A%0A
+%09private final ServletRequestDetails myWrap;%0A//
 %09private
@@ -1209,24 +1209,58 @@
 dcaster());%0A
+%0A%09%09myWrap = theRequestDetails;%0A%0A//
 %09%09if (theReq
@@ -1282,16 +1282,18 @@
 null) %7B%0A
+//
 %09%09%09Map%3CS
@@ -1355,16 +1355,18 @@
 ders();%0A
+//
 %09%09%09for (
@@ -1426,16 +1426,18 @@
 et()) %7B%0A
+//
 %09%09%09%09myHe
@@ -1497,21 +1497,25 @@
 ));%0A
+//
 %09%09%09%7D%0A
+//
 %09%09%7D%0A%09%7D%0A%0A
 %09pub
@@ -1510,16 +1510,18 @@
 %09%09%7D%0A%09%7D%0A%0A
+//
 %09public 
@@ -1570,16 +1570,18 @@
 alue) %7B%0A
+//
 %09%09String
@@ -1616,16 +1616,18 @@
 Case();%0A
+//
 %09%09List%3CS
@@ -1666,16 +1666,18 @@
 rCase);%0A
+//
 %09%09if (li
@@ -1690,16 +1690,18 @@
 null) %7B%0A
+//
 %09%09%09list 
@@ -1721,16 +1721,18 @@
 st%3C%3E();%0A
+//
 %09%09%09myHea
@@ -1758,20 +1758,24 @@
  list);%0A
+//
 %09%09%7D%0A
+//
 %09%09list.a
@@ -1788,16 +1788,18 @@
 Value);%0A
+//
 %09%7D%0A%09%0A%09@O
@@ -1841,32 +1841,71 @@
 ring theName) %7B%0A
+%09%09return myWrap.getHeader(theName);%0A%0A//
 %09%09List%3CString%3E l
@@ -1940,32 +1940,34 @@
 toLowerCase());%0A
+//
 %09%09if (list == nu
@@ -1982,32 +1982,34 @@
 st.isEmpty()) %7B%0A
+//
 %09%09%09return null;%0A
@@ -2000,36 +2000,40 @@
 %09%09%09return null;%0A
+//
 %09%09%7D%0A
+//
 %09%09return list.ge
@@ -2103,16 +2103,56 @@
 Name) %7B%0A
+%09%09return myWrap.getHeaders(theName);%0A%0A//
 %09%09List%3CS
@@ -2203,16 +2203,18 @@
 ase());%0A
+//
 %09%09if (li
@@ -2245,16 +2245,18 @@
 ty()) %7B%0A
+//
 %09%09%09retur
@@ -2263,20 +2263,24 @@
 n null;%0A
+//
 %09%09%7D%0A
+//
 %09%09return
@@ -2286,24 +2286,114 @@
 n list;%0A%09%7D%0A%0A
+%09@Override%0A%09public Map%3CObject, Object%3E getUserData() %7B%0A%09%09return myWrap.getUserData();%0A%09%7D%0A%0A
 %09@Override%0A%09

Trim subrequest