re base code
[sdc.git] / utils / webseal-simulator / src / main / java / org / openecomp / sdc / webseal / simulator / MutableHttpServletRequest.java
1 package org.openecomp.sdc.webseal.simulator;
2
3
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletRequestWrapper;
6 import java.util.*;
7
8 public final class MutableHttpServletRequest extends HttpServletRequestWrapper {
9         // holds custom header and value mapping
10         private final Map<String, String> customHeaders;
11
12         public MutableHttpServletRequest(HttpServletRequest request){
13                 super(request);
14                 this.customHeaders = new HashMap<>();
15         }
16         
17         public void putHeader(String name, String value){
18                 this.customHeaders.put(name, value);
19         }
20
21         public String getHeader(String name) {
22                 // check the custom headers first
23                 String headerValue = customHeaders.get(name);
24                 
25                 if (headerValue != null){
26                         return headerValue;
27                 }
28                 // else return from into the original wrapped object
29                 return ((HttpServletRequest) getRequest()).getHeader(name);
30         }
31
32         public Enumeration<String> getHeaderNames() {
33                 // create a set of the custom header names
34                 Set<String> set = new HashSet<>(customHeaders.keySet());
35                 
36                 // now add the headers from the wrapped request object
37                 @SuppressWarnings("unchecked")
38                 Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
39                 while (e.hasMoreElements()) {
40                         // add the names of the request headers into the list
41                         String n = e.nextElement();
42                         set.add(n);
43                 }
44
45                 // create an enumeration from the set and return
46                 return Collections.enumeration(set);
47         }
48 }
49