001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.jsonwebservice;
016    
017    import com.liferay.portal.kernel.upload.FileItem;
018    import com.liferay.portal.kernel.upload.UploadServletRequest;
019    import com.liferay.portal.kernel.util.CamelCaseUtil;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    
026    import java.util.Collections;
027    import java.util.Enumeration;
028    import java.util.List;
029    import java.util.Map;
030    import java.util.Set;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    import jodd.util.NameValue;
035    
036    /**
037     * @author Igor Spasic
038     */
039    public class JSONWebServiceActionParameters {
040    
041            public void collectAll(
042                    HttpServletRequest request, String parameterPath,
043                    JSONRPCRequest jsonRPCRequest, Map<String, Object> parameterMap) {
044    
045                    _jsonRPCRequest = jsonRPCRequest;
046    
047                    try {
048                            _serviceContext = ServiceContextFactory.getInstance(request);
049                    }
050                    catch (Exception e) {
051                    }
052    
053                    _addDefaultParameters();
054    
055                    _collectDefaultsFromRequestAttributes(request);
056    
057                    _collectFromPath(parameterPath);
058                    _collectFromRequestParameters(request);
059                    _collectFromJSONRPCRequest(jsonRPCRequest);
060                    _collectFromMap(parameterMap);
061            }
062    
063            public List<NameValue<String, Object>> getInnerParameters(String baseName) {
064                    return _jsonWebServiceActionParameters.getInnerParameters(baseName);
065            }
066    
067            public JSONRPCRequest getJSONRPCRequest() {
068                    return _jsonRPCRequest;
069            }
070    
071            public Object getParameter(String name) {
072                    return _jsonWebServiceActionParameters.get(name);
073            }
074    
075            public String[] getParameterNames() {
076                    String[] names = new String[_jsonWebServiceActionParameters.size()];
077    
078                    int i = 0;
079    
080                    for (String key : _jsonWebServiceActionParameters.keySet()) {
081                            names[i] = key;
082    
083                            i++;
084                    }
085    
086                    return names;
087            }
088    
089            public String getParameterTypeName(String name) {
090                    return _jsonWebServiceActionParameters.getParameterTypeName(name);
091            }
092    
093            public ServiceContext getServiceContext() {
094                    return _serviceContext;
095            }
096    
097            public boolean includeDefaultParameters() {
098                    return _jsonWebServiceActionParameters.includeDefaultParameters();
099            }
100    
101            private void _addDefaultParameters() {
102                    _jsonWebServiceActionParameters.put("serviceContext", Void.TYPE);
103            }
104    
105            private void _collectDefaultsFromRequestAttributes(
106                    HttpServletRequest request) {
107    
108                    Enumeration<String> enu = request.getAttributeNames();
109    
110                    while (enu.hasMoreElements()) {
111                            String attributeName = enu.nextElement();
112    
113                            Object value = request.getAttribute(attributeName);
114    
115                            _jsonWebServiceActionParameters.putDefaultParameter(
116                                    attributeName, value);
117                    }
118            }
119    
120            private void _collectFromJSONRPCRequest(JSONRPCRequest jsonRPCRequest) {
121                    if (jsonRPCRequest == null) {
122                            return;
123                    }
124    
125                    Set<String> parameterNames = jsonRPCRequest.getParameterNames();
126    
127                    for (String parameterName : parameterNames) {
128                            String value = jsonRPCRequest.getParameter(parameterName);
129    
130                            parameterName = CamelCaseUtil.normalizeCamelCase(parameterName);
131    
132                            _jsonWebServiceActionParameters.put(parameterName, value);
133                    }
134            }
135    
136            private void _collectFromMap(Map<String, Object> parameterMap) {
137                    if (parameterMap == null) {
138                            return;
139                    }
140    
141                    for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
142                            String parameterName = entry.getKey();
143    
144                            Object value = entry.getValue();
145    
146                            _jsonWebServiceActionParameters.put(parameterName, value);
147                    }
148            }
149    
150            private void _collectFromPath(String parameterPath) {
151                    if (parameterPath == null) {
152                            return;
153                    }
154    
155                    if (parameterPath.startsWith(StringPool.SLASH)) {
156                            parameterPath = parameterPath.substring(1);
157                    }
158    
159                    String[] parameterPathParts = StringUtil.split(
160                            parameterPath, CharPool.SLASH);
161    
162                    int i = 0;
163    
164                    while (i < parameterPathParts.length) {
165                            String name = parameterPathParts[i];
166    
167                            if (name.length() == 0) {
168                                    i++;
169    
170                                    continue;
171                            }
172    
173                            String value = null;
174    
175                            if (name.startsWith(StringPool.DASH)) {
176                                    name = name.substring(1);
177                            }
178                            else if (!name.startsWith(StringPool.PLUS)) {
179                                    i++;
180    
181                                    if (i >= parameterPathParts.length) {
182                                            throw new IllegalArgumentException(
183                                                    "Missing value for parameter " + name);
184                                    }
185    
186                                    value = parameterPathParts[i];
187                            }
188    
189                            name = CamelCaseUtil.toCamelCase(name);
190    
191                            _jsonWebServiceActionParameters.put(name, value);
192    
193                            i++;
194                    }
195            }
196    
197            private void _collectFromRequestParameters(HttpServletRequest request) {
198                    UploadServletRequest uploadServletRequest = null;
199    
200                    if (request instanceof UploadServletRequest) {
201                            uploadServletRequest = (UploadServletRequest)request;
202                    }
203    
204                    List<String> parameterNames = Collections.list(
205                            request.getParameterNames());
206    
207                    if (uploadServletRequest != null) {
208                            Map<String, FileItem[]> multipartParameterMap =
209                                    uploadServletRequest.getMultipartParameterMap();
210    
211                            parameterNames.addAll(multipartParameterMap.keySet());
212                    }
213    
214                    for (String parameterName : parameterNames) {
215                            Object value = null;
216    
217                            if ((uploadServletRequest != null) &&
218                                    (uploadServletRequest.getFileName(parameterName) != null)) {
219    
220                                    value = uploadServletRequest.getFile(parameterName, true);
221                            }
222                            else {
223                                    String[] parameterValues = request.getParameterValues(
224                                            parameterName);
225    
226                                    if (parameterValues.length == 1) {
227                                            value = parameterValues[0];
228                                    }
229                                    else {
230                                            value = parameterValues;
231                                    }
232                            }
233    
234                            parameterName = CamelCaseUtil.normalizeCamelCase(parameterName);
235    
236                            _jsonWebServiceActionParameters.put(parameterName, value);
237                    }
238            }
239    
240            private JSONRPCRequest _jsonRPCRequest;
241            private final JSONWebServiceActionParametersMap
242                    _jsonWebServiceActionParameters =
243                            new JSONWebServiceActionParametersMap();
244            private ServiceContext _serviceContext;
245    
246    }