View Javadoc

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.jetspeed.ajax;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  /***
29   * Request used for AJAX services.
30   * 
31   * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
32   *
33   */
34  public class AJAXRequestImpl implements AJAXRequest
35  {
36      public static final String AJAX_SERVICE = "ajax_service";
37      public static final String AJAX_PARAM_PREFIX = "ajax_param_";
38      
39      private final HttpServletRequest request;
40      private List ajaxParams;
41      private final String serviceName;
42      private final String methodName;
43      private HttpServletResponse response;
44      private ServletContext context;
45  
46      public AJAXRequestImpl(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws AJAXException
47      {
48          this.request = request;
49          this.response = response;
50          this.context = context;
51          String serviceRequest =  request.getParameter(AJAX_SERVICE);
52          if(serviceRequest == null )
53          {
54              throw new AJAXException("No '"+AJAX_SERVICE+"' parameter could be found in the request or it was not in the '{service_name}.{method_name}' format.");
55          }
56          final String split = serviceRequest.split("//.")[0];
57          serviceName = split;
58          methodName = serviceRequest.split("//.")[1];
59          
60          parseRequestArguments();
61          
62      }
63      
64      /* (non-Javadoc)
65       * @see org.apache.jetspeed.ajax.AJAXRequest#getParameters()
66       */
67      public List getParameters()
68      {
69          return ajaxParams;
70      }
71  
72      /* (non-Javadoc)
73       * @see org.apache.jetspeed.ajax.AJAXRequest#getServiceName()
74       */
75      public String getServiceName()
76      {
77          return serviceName;
78      }
79  
80      protected List parseRequestArguments() throws AJAXException
81      {
82          try
83          {
84              ajaxParams = new ArrayList();
85              Map rawParams = request.getParameterMap();
86              Iterator entryItr = rawParams.entrySet().iterator();
87              while(entryItr.hasNext())
88              {
89                  Map.Entry entry = (Map.Entry) entryItr.next();
90                  String key = entry.getKey().toString();
91                  
92                  if(key.startsWith(AJAX_PARAM_PREFIX))
93                  {
94                      String[] paramInfo = key.split("_");
95                      int index = Integer.parseInt(paramInfo[2]);
96                      String type = paramInfo[3]; 
97                      AJAXParameter ajaxParam = new AJAXParameter(type, (String[])entry.getValue());
98                      ajaxParams.add(index, ajaxParam);
99                  }
100             }
101             return ajaxParams;
102         }
103         catch (Throwable e)
104         {
105             throw new AJAXException("Errors were encountered parsing request parameters for the AJAX service "+serviceName+": "+e.getMessage(), e);
106         }
107     }
108     
109     public class AJAXParameter
110     {
111         private Object value;
112              
113         public AJAXParameter(String typeName, String[] paramValues)
114         {
115             if(typeName.equals("int"))
116             {                
117                 if(paramValues.length > 1)
118                 {
119                     int[] intValues = new int[paramValues.length];
120                     for(int i=0; i<paramValues.length; i++)
121                     {
122                         intValues[i] = Integer.parseInt(paramValues[i]);
123                     }
124                 }
125                 else
126                 {
127                     value = new Integer(paramValues[0]);
128                 }
129             }
130             else if(typeName.equals("str"))
131             {
132               if(paramValues.length > 1)
133               {    
134                   value = paramValues;
135               }
136               else
137               {
138                   value = paramValues[0];
139               }
140             }
141         }
142         
143         public Object getValue()
144         {
145             return value;
146         }
147     }
148 
149 
150     /* (non-Javadoc)
151      * @see org.apache.jetspeed.ajax.AJAXRequest#getMethodName()
152      */
153     public String getMethodName()
154     {
155         return methodName;
156     }
157 
158     /* (non-Javadoc)
159      * @see org.apache.jetspeed.ajax.AJAXRequest#getContext()
160      */
161     public ServletContext getContext()
162     {
163         return context;
164     }
165 
166     /* (non-Javadoc)
167      * @see org.apache.jetspeed.ajax.AJAXRequest#getServletRequest()
168      */
169     public HttpServletRequest getServletRequest()
170     {
171         return request;
172     }
173 
174     /* (non-Javadoc)
175      * @see org.apache.jetspeed.ajax.AJAXRequest#getServletResponse()
176      */
177     public HttpServletResponse getServletResponse()
178     {
179         return response;
180     }
181 }