1 package com.pentila.basic;
3 * Copyright 2009 Jee Vang
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required
8 * by applicable law or agreed to in writing, software distributed
9 * under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Enumeration;
18 import java.util.List;
20 import javax.servlet.http.Cookie;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletRequestWrapper;
25 * Fake headers request object. Adds a request header
26 * with the name "username". The value of this request header
27 * will be taken from a cookie (also with the name, "username").
32 public class FakeHeadersRequest extends HttpServletRequestWrapper {
34 String attribute = null;
39 * @param request HttpServletRequest.
41 public FakeHeadersRequest(HttpServletRequest request, String attribute, String value) {
43 this.attribute = attribute;
48 public String getHeader(String name) {
49 //get the request object and cast it
50 HttpServletRequest request = (HttpServletRequest)getRequest();
52 //if we are looking for the "username" request header
53 if(attribute.equals(name)) {
57 //otherwise fall through to wrapped request object
58 return request.getHeader(name);
61 public Enumeration getHeaderNames() {
62 //create an enumeration of the request headers
63 //additionally, add the "username" request header
66 List list = new ArrayList();
68 //loop over request headers from wrapped request object
69 HttpServletRequest request = (HttpServletRequest)getRequest();
70 Enumeration e = request.getHeaderNames();
71 while(e.hasMoreElements()) {
72 //add the names of the request headers into the list
73 String n = (String)e.nextElement();
77 //additionally, add the "username" to the list of request header names
80 //create an enumeration from the list and return
81 Enumeration en = Collections.enumeration(list);