View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.util;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.StringTokenizer;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.springframework.extensions.surf.RequestContext;
29  
30  /**
31   * Static helper methods for working with query strings and maps.
32   * 
33   * @author muzquiano
34   */
35  public class WebUtil
36  {
37      /**
38       * Creates a Map of query string key and value parameters from the
39       * given request
40       * 
41       * @param request the request
42       * 
43       * @return the query string map
44       */
45      public static Map getQueryStringMap(HttpServletRequest request)
46      {
47          String queryString = request.getQueryString();
48          return getQueryStringMap(queryString);
49      }
50  
51      /**
52       * Creates a Map of query string key and value parameters from the
53       * given query string.
54       * 
55       * Values are decoded using the default encoding.
56       * 
57       * @param queryString the query string
58       * 
59       * @return the query string map
60       */    
61      public static Map getQueryStringMap(String queryString)
62      {
63          return getQueryStringMap(queryString, EncodingUtil.DEFAULT_ENCODING);        
64      }
65  
66      /**
67       * Creates a Map of query string key and value parameters from the
68       * given query string.
69       * 
70       * Values are decoded using the specified encoding.
71       * If the specified encoding is null, not decoding is applied.
72       * 
73       * @param queryString the query string
74       * 
75       * @return the query string map
76       */    
77      public static Map getQueryStringMap(String queryString, String encoding)
78      {
79          HashMap map = new HashMap(24, 1.0f);
80          
81          if (queryString != null)
82          {
83              StringTokenizer tokenizer = new StringTokenizer(queryString, "&");
84              while (tokenizer.hasMoreTokens())
85              {
86                  String combo = (String) tokenizer.nextToken();
87                  int c = combo.indexOf("=");
88                  if (c > -1)
89                  {
90                      String key = combo.substring(0, c);
91                      String value = combo.substring(c + 1, combo.length());
92                      
93                      if (encoding != null)
94                      {
95                          value = EncodingUtil.decode(value, encoding);
96                      }
97                      
98                      map.put(key, value);
99                  }
100             }
101         }
102         
103         return map;        
104     }
105 
106     /**
107      * Returns the query string for a given map of key and value pairs
108      * 
109      * @param map the map
110      * 
111      * @return the query string for map
112      */
113     public static String getQueryStringForMap(Map map)
114     {
115         if (map == null)
116         {
117             return "";
118         }
119 
120         boolean first = true;
121         String result = "";
122 
123         Iterator it = map.keySet().iterator();
124         while (it.hasNext())
125         {
126             String key = (String) it.next();
127             String value = (String) map.get(key);
128 
129             if (!first)
130                 result = result + "&";
131 
132             result = result + key + "=" + value;
133             first = false;
134         }
135         return result;
136     }
137     
138     /**
139      * Converts a relative URI to a fully qualified URL 
140      * If the URI is already fully qualified, it is simply returned
141      * 
142      * @param context
143      * @param uri
144      * 
145      * @return the fully qualified url
146      */
147     public static String toFullyQualifiedURL(RequestContext context, String uri)
148     {
149         return toFullyQualifiedURL(context.getRequest(), uri);
150     }
151 
152     /**
153      * Converts a relative URI to a fully qualified URL 
154      * If the URI is already fully qualified, it is simply returned
155      *  
156      * @param request
157      * @param uri
158      * 
159      * @return the fully qualified url
160      */
161     public static String toFullyQualifiedURL(HttpServletRequest request, String uri)
162     {
163         StringBuilder builder = new StringBuilder(128);
164         
165         if (uri.startsWith("/"))
166         {                    
167             builder.append(request.getScheme());
168             builder.append("://");
169             builder.append(request.getServerName());
170             
171             if (request.getServerPort() != 80)
172             {
173                 builder.append(":");
174                 builder.append(request.getServerPort());
175             }
176         }
177         
178         builder.append(uri);
179         
180         return builder.toString();
181     }
182     
183 }