This project has retired. For details please refer to its Attic page.
QueryStringHttpServletRequestWrapper xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.shared;
20  
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletRequestWrapper;
29  
30  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
31  
32  /**
33   * HttpServletRequest wrapper that reads the query string in container
34   * independent way and decodes the parameter values with UTF-8.
35   */
36  public class QueryStringHttpServletRequestWrapper extends HttpServletRequestWrapper {
37  
38      protected Map<String, String[]> parameters;
39  
40      public QueryStringHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
41          super(request);
42  
43          parameters = new HashMap<String, String[]>();
44  
45          // parse query string
46          parseFormData(request.getQueryString());
47      }
48  
49      /**
50       * Parses the query string.
51       */
52      protected final void parseFormData(String queryString) throws IOException {
53          if (queryString == null || queryString.length() < 3) {
54              return;
55          }
56  
57          String[] nameValuePairs = queryString.split("&");
58          for (String nameValuePair : nameValuePairs) {
59              int x = nameValuePair.indexOf('=');
60              if (x > 0) {
61                  String name = IOUtils.decodeURL(nameValuePair.substring(0, x));
62                  String value = (x == nameValuePair.length() - 1 ? "" : IOUtils
63                          .decodeURL(nameValuePair.substring(x + 1)));
64                  addParameter(name, value);
65              } else {
66                  String name = IOUtils.decodeURL(nameValuePair);
67                  addParameter(name, (String) null);
68              }
69          }
70      }
71  
72      /**
73       * Adds a value to a parameter.
74       */
75      protected final void addParameter(String name, String value) {
76          String[] values = parameters.get(name);
77  
78          if (values == null) {
79              parameters.put(name, new String[] { value });
80          } else {
81              String[] newValues = new String[values.length + 1];
82              System.arraycopy(values, 0, newValues, 0, values.length);
83              newValues[newValues.length - 1] = value;
84              parameters.put(name, newValues);
85          }
86      }
87  
88      /**
89       * Adds an array of values to a parameter.
90       */
91      protected final void addParameter(String name, String[] additionalValues) {
92          String[] values = parameters.get(name);
93  
94          if (values == null) {
95              parameters.put(name, additionalValues);
96          } else {
97              String[] newValues = new String[values.length + additionalValues.length];
98              System.arraycopy(values, 0, newValues, 0, values.length);
99              System.arraycopy(additionalValues, 0, newValues, values.length, additionalValues.length);
100             parameters.put(name, newValues);
101         }
102     }
103 
104     @Override
105     public final String getParameter(String name) {
106         String[] values = parameters.get(name);
107         if (values == null || values.length == 0) {
108             return null;
109         }
110 
111         return values[0];
112     }
113 
114     @Override
115     public final Map<String, String[]> getParameterMap() {
116         return parameters;
117     }
118 
119     @Override
120     public final Enumeration<String> getParameterNames() {
121         return Collections.enumeration(parameters.keySet());
122     }
123 
124     @Override
125     public final String[] getParameterValues(String name) {
126         return parameters.get(name);
127     }
128 }