001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.ajax;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: /**
029: * Request used for AJAX services.
030: *
031: * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
032: *
033: */
034: public class AJAXRequestImpl implements AJAXRequest {
035: public static final String AJAX_SERVICE = "ajax_service";
036: public static final String AJAX_PARAM_PREFIX = "ajax_param_";
037:
038: private final HttpServletRequest request;
039: private List ajaxParams;
040: private final String serviceName;
041: private final String methodName;
042: private HttpServletResponse response;
043: private ServletContext context;
044:
045: public AJAXRequestImpl(HttpServletRequest request,
046: HttpServletResponse response, ServletContext context)
047: throws AJAXException {
048: this .request = request;
049: this .response = response;
050: this .context = context;
051: String serviceRequest = request.getParameter(AJAX_SERVICE);
052: if (serviceRequest == null) {
053: throw new AJAXException(
054: "No '"
055: + AJAX_SERVICE
056: + "' parameter could be found in the request or it was not in the '{service_name}.{method_name}' format.");
057: }
058: final String split = serviceRequest.split("\\.")[0];
059: serviceName = split;
060: methodName = serviceRequest.split("\\.")[1];
061:
062: parseRequestArguments();
063:
064: }
065:
066: /* (non-Javadoc)
067: * @see org.apache.jetspeed.ajax.AJAXRequest#getParameters()
068: */
069: public List getParameters() {
070: return ajaxParams;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.apache.jetspeed.ajax.AJAXRequest#getServiceName()
075: */
076: public String getServiceName() {
077: return serviceName;
078: }
079:
080: protected List parseRequestArguments() throws AJAXException {
081: try {
082: ajaxParams = new ArrayList();
083: Map rawParams = request.getParameterMap();
084: Iterator entryItr = rawParams.entrySet().iterator();
085: while (entryItr.hasNext()) {
086: Map.Entry entry = (Map.Entry) entryItr.next();
087: String key = entry.getKey().toString();
088:
089: if (key.startsWith(AJAX_PARAM_PREFIX)) {
090: String[] paramInfo = key.split("_");
091: int index = Integer.parseInt(paramInfo[2]);
092: String type = paramInfo[3];
093: AJAXParameter ajaxParam = new AJAXParameter(type,
094: (String[]) entry.getValue());
095: ajaxParams.add(index, ajaxParam);
096: }
097: }
098: return ajaxParams;
099: } catch (Throwable e) {
100: throw new AJAXException(
101: "Errors were encountered parsing request parameters for the AJAX service "
102: + serviceName + ": " + e.getMessage(), e);
103: }
104: }
105:
106: public class AJAXParameter {
107: private Object value;
108:
109: public AJAXParameter(String typeName, String[] paramValues) {
110: if (typeName.equals("int")) {
111: if (paramValues.length > 1) {
112: int[] intValues = new int[paramValues.length];
113: for (int i = 0; i < paramValues.length; i++) {
114: intValues[i] = Integer.parseInt(paramValues[i]);
115: }
116: } else {
117: value = new Integer(paramValues[0]);
118: }
119: } else if (typeName.equals("str")) {
120: if (paramValues.length > 1) {
121: value = paramValues;
122: } else {
123: value = paramValues[0];
124: }
125: }
126: }
127:
128: public Object getValue() {
129: return value;
130: }
131: }
132:
133: /* (non-Javadoc)
134: * @see org.apache.jetspeed.ajax.AJAXRequest#getMethodName()
135: */
136: public String getMethodName() {
137: return methodName;
138: }
139:
140: /* (non-Javadoc)
141: * @see org.apache.jetspeed.ajax.AJAXRequest#getContext()
142: */
143: public ServletContext getContext() {
144: return context;
145: }
146:
147: /* (non-Javadoc)
148: * @see org.apache.jetspeed.ajax.AJAXRequest#getServletRequest()
149: */
150: public HttpServletRequest getServletRequest() {
151: return request;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.apache.jetspeed.ajax.AJAXRequest#getServletResponse()
156: */
157: public HttpServletResponse getServletResponse() {
158: return response;
159: }
160: }
|