Home » commons-chain-1.2-src » org.apache.commons » chain » web » servlet » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.commons.chain.web.servlet;
   18   
   19   
   20   import java.util.ArrayList;
   21   import java.util.Collection;
   22   import java.util.Enumeration;
   23   import java.util.HashSet;
   24   import java.util.Iterator;
   25   import java.util.List;
   26   import java.util.Map;
   27   import java.util.Set;
   28   import javax.servlet.http.HttpServletRequest;
   29   import org.apache.commons.chain.web.MapEntry;
   30   
   31   
   32   /**
   33    * <p>Private implementation of <code>Map</code> for servlet request
   34    * name-values[].</p>
   35    *
   36    * @author Craig R. McClanahan
   37    * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
   38    */
   39   
   40   final class ServletHeaderValuesMap implements Map {
   41   
   42   
   43       public ServletHeaderValuesMap(HttpServletRequest request) {
   44           this.request = request;
   45       }
   46   
   47   
   48       private HttpServletRequest request = null;
   49   
   50   
   51       public void clear() {
   52           throw new UnsupportedOperationException();
   53       }
   54   
   55   
   56       public boolean containsKey(Object key) {
   57           return (request.getHeader(key(key)) != null);
   58       }
   59   
   60   
   61       public boolean containsValue(Object value) {
   62           if (!(value instanceof String[])) {
   63               return (false);
   64           }
   65           String[] test = (String[]) value;
   66           Iterator values = values().iterator();
   67           while (values.hasNext()) {
   68               String[] actual = (String[]) values.next();
   69               if (test.length == actual.length) {
   70                   boolean matched = true;
   71                   for (int i = 0; i < test.length; i++) {
   72                       if (!test[i].equals(actual[i])) {
   73                           matched = false;
   74                           break;
   75                       }
   76                   }
   77                   if (matched) {
   78                       return (true);
   79                   }
   80               }
   81           }
   82           return (false);
   83       }
   84   
   85   
   86       public Set entrySet() {
   87           Set set = new HashSet();
   88           Enumeration keys = request.getHeaderNames();
   89           String key;
   90           while (keys.hasMoreElements()) {
   91               key = (String) keys.nextElement();
   92               set.add(new MapEntry(key, request.getHeaders(key), false));
   93           }
   94           return (set);
   95       }
   96   
   97   
   98       public boolean equals(Object o) {
   99           return (request.equals(o));
  100       }
  101   
  102   
  103       public Object get(Object key) {
  104           List list = new ArrayList();
  105           Enumeration values = request.getHeaders(key(key));
  106           while (values.hasMoreElements()) {
  107               list.add((String) values.nextElement());
  108           }
  109           return (((String[]) list.toArray(new String[list.size()])));
  110       }
  111   
  112   
  113       public int hashCode() {
  114           return (request.hashCode());
  115       }
  116   
  117   
  118       public boolean isEmpty() {
  119           return (size() < 1);
  120       }
  121   
  122   
  123       public Set keySet() {
  124           Set set = new HashSet();
  125           Enumeration keys = request.getHeaderNames();
  126           while (keys.hasMoreElements()) {
  127               set.add(keys.nextElement());
  128           }
  129           return (set);
  130       }
  131   
  132   
  133       public Object put(Object key, Object value) {
  134           throw new UnsupportedOperationException();
  135       }
  136   
  137   
  138       public void putAll(Map map) {
  139           throw new UnsupportedOperationException();
  140       }
  141   
  142   
  143       public Object remove(Object key) {
  144           throw new UnsupportedOperationException();
  145       }
  146   
  147   
  148       public int size() {
  149           int n = 0;
  150           Enumeration keys = request.getHeaderNames();
  151           while (keys.hasMoreElements()) {
  152               keys.nextElement();
  153               n++;
  154           }
  155           return (n);
  156       }
  157   
  158   
  159       public Collection values() {
  160           List list = new ArrayList();
  161           Enumeration keys = request.getHeaderNames();
  162           while (keys.hasMoreElements()) {
  163               String key = (String) keys.nextElement();
  164               List list1 = new ArrayList();
  165               Enumeration values = request.getHeaders(key);
  166               while (values.hasMoreElements()) {
  167                   list1.add((String) values.nextElement());
  168               }
  169               list.add(((String[]) list1.toArray(new String[list1.size()])));
  170           }
  171           return (list);
  172       }
  173   
  174   
  175       private String key(Object key) {
  176           if (key == null) {
  177               throw new IllegalArgumentException();
  178           } else if (key instanceof String) {
  179               return ((String) key);
  180           } else {
  181               return (key.toString());
  182           }
  183       }
  184   
  185   
  186   }

Save This Page
Home » commons-chain-1.2-src » org.apache.commons » chain » web » servlet » [javadoc | source]