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.webscripts;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  
28  /**
29   * Web Script Request implementation that acts upon a string representation
30   * of a URL
31   * 
32   * @author davidc
33   */
34  public abstract class WebScriptRequestURLImpl extends WebScriptRequestImpl
35  {
36      /** Script Url components */
37      protected String contextPath;
38      protected String servletPath;
39      protected String pathInfo;
40      protected String queryString;
41      protected Map<String, String> queryArgs;
42      protected Map<String, List<String>> queryArgsMulti;
43      
44      /** Service bound to this request */
45      protected Match serviceMatch;
46  
47  
48      /**
49       * Splits a Web Script Url into its component parts
50       * 
51       * @param scriptUrl  url  e.g. /alfresco/service/mytasks?f=1 
52       * @return  url parts  [0] = context (e.g. alfresco), [1] = servlet (e.g. service), [2] = script (e.g. mytasks), [3] = args (e.g. f=1)
53       */
54      public static String[] splitURL(String scriptUrl)
55      {
56          return splitURL(true, scriptUrl);
57      }
58      
59      /**
60       * Splits a Web Script Url into its component parts
61       *
62       * @param context  true => context path is included in scriptUrl
63       * @param scriptUrl  url  e.g. /alfresco/service/mytasks?f=1 
64       * @return  url parts  [0] = context (e.g. alfresco, or empty if no context), [1] = servlet (e.g. service), [2] = script (e.g. mytasks), [3] = args (e.g. f=1)
65       */
66      public static String[] splitURL(boolean context, String scriptUrl)
67      {
68          String[] urlParts = new String[4];
69          String path;
70          String queryString;
71          
72          int argsIndex = scriptUrl.indexOf("?");
73          if (argsIndex != -1)
74          {
75              path = scriptUrl.substring(0, argsIndex);
76              queryString = scriptUrl.substring(argsIndex + 1);
77          }
78          else
79          {
80              path = scriptUrl;
81              queryString = null;
82          }
83          
84          String[] pathSegments = path.split("/");
85          String pathInfo = "";
86          for (int i = (context ? 3 : 2); i < pathSegments.length; i++)
87          {
88              pathInfo += "/" + pathSegments[i];
89          }
90          
91          urlParts[0] = context ? "/" + pathSegments[1] : "";   // context path
92          urlParts[1] = "/" + pathSegments[context ? 2 : 1];    // servlet path
93          urlParts[2] = pathInfo;                                   // path info
94          urlParts[3] = queryString;                                // query string
95      
96          return urlParts;
97      }
98  
99      /**
100      * Construct
101      * 
102      * Note: It's assumed scriptUrl contains context path
103      * 
104      * @param scriptUrl
105      * @param serviceMatch
106      */
107     public WebScriptRequestURLImpl(Runtime runtime, String scriptUrl, Match serviceMatch)
108     {
109         this(runtime, splitURL(scriptUrl), serviceMatch);
110     }
111     
112     /**
113      * Construct
114      * 
115      * @param scriptUrlParts
116      * @param serviceMatch
117      */
118     public WebScriptRequestURLImpl(Runtime runtime, String[] scriptUrlParts, Match serviceMatch)
119     {
120         super(runtime);
121         
122         this.contextPath = scriptUrlParts[0];
123         this.servletPath = scriptUrlParts[1];
124         this.pathInfo = scriptUrlParts[2];
125         this.queryString = scriptUrlParts[3];
126         this.queryArgs = new HashMap<String, String>(8, 1.0f);
127         this.queryArgsMulti = new HashMap<String, List<String>>(8, 1.0f);
128         if (this.queryString != null)
129         {
130             String[] args = this.queryString.split("&");
131             for (String arg : args)
132             {
133                 String[] parts = arg.split("=");
134                 if (this.queryArgs.containsKey(parts[0]))
135                 {
136                     List<String> values = this.queryArgsMulti.get(parts[0]);
137                     if (values == null)
138                     {
139                         values = new ArrayList<String>(4);
140                         this.queryArgsMulti.put(parts[0], values);
141                     }
142                     values.add(parts.length == 2 ? parts[1] : "");
143                 }
144                 else
145                 {
146                     this.queryArgs.put(parts[0], parts.length == 2 ? parts[1] : "");
147                 }
148             }
149         }
150         this.serviceMatch = serviceMatch;
151     }
152     
153     @Override
154     public String toString()
155     {
156         return "Request Service Match: " + serviceMatch +
157                " URL: " + this.contextPath + this.servletPath + this.pathInfo + "?" + queryString;
158     }
159 
160     /* (non-Javadoc)
161      * @see org.alfresco.web.scripts.WebScriptRequest#getServiceMatch()
162      */
163     public Match getServiceMatch()
164     {
165         return serviceMatch;
166     }
167 
168     /* (non-Javadoc)
169      * @see org.alfresco.web.scripts.WebScriptRequest#getContextPath()
170      */
171     public String getContextPath()
172     {
173         return contextPath;
174     }
175     
176     /* (non-Javadoc)
177      * @see org.alfresco.web.scripts.WebScriptRequest#getServiceContextPath()
178      */
179     public String getServiceContextPath()
180     {
181         return getContextPath() + servletPath;
182     }
183 
184     /* (non-Javadoc)
185      * @see org.alfresco.web.scripts.WebScriptRequest#getServicePath()
186      */
187     public String getServicePath()
188     {
189         return getServiceContextPath() + pathInfo;
190     }
191 
192     /* (non-Javadoc)
193      * @see org.alfresco.web.scripts.WebScriptRequest#getURL()
194      */
195     public String getURL()
196     {
197         return getServicePath() + (queryString != null ? "?" + queryString : "");
198     }
199 
200     /* (non-Javadoc)
201      * @see org.alfresco.web.scripts.WebScriptRequest#getPathInfo()
202      */
203     public String getPathInfo()
204     {
205        return pathInfo; 
206     }
207     
208     /* (non-Javadoc)
209      * @see org.alfresco.web.scripts.WebScriptRequest#getQueryString()
210      */
211     public String getQueryString()
212     {
213         return queryString;
214     }
215     
216     /* (non-Javadoc)
217      * @see org.alfresco.web.scripts.WebScriptRequest#getParameterNames()
218      */
219     public String[] getParameterNames()
220     {
221         Set<String> keys = queryArgs.keySet();
222         String[] names = new String[keys.size()];
223         keys.toArray(names);
224         return names;
225     }
226 
227     /* (non-Javadoc)
228      * @see org.alfresco.web.scripts.WebScriptRequest#getParameter(java.lang.String)
229      */
230     public String getParameter(String name)
231     {
232         return queryArgs.get(name);
233     }
234 
235     /* (non-Javadoc)
236      * @see org.alfresco.web.scripts.WebScriptRequest#getArrayParameter(java.lang.String)
237      */
238     public String[] getParameterValues(String name)
239     {
240         List<String> values = queryArgsMulti.get(name);
241         if (values != null)
242         {
243             String[] array = new String[values.size()];
244             values.toArray(array);
245             return array;
246         }
247         else
248         {
249             String value = queryArgs.get(name);
250             if (value != null)
251             {
252                 return new String[]{value};
253             }
254         }
255         return null;
256     }
257     
258 }