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.UploadServletRequest;
018    import com.liferay.portal.kernel.util.CamelCaseUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    
025    import java.util.Enumeration;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    import jodd.util.NameValue;
033    
034    /**
035     * @author Igor Spasic
036     */
037    public class JSONWebServiceActionParameters {
038    
039            public void collectAll(
040                    HttpServletRequest request, String parameterPath,
041                    JSONRPCRequest jsonRPCRequest, Map<String, Object> parameterMap) {
042    
043                    _jsonRPCRequest = jsonRPCRequest;
044    
045                    try {
046                            _serviceContext = ServiceContextFactory.getInstance(request);
047                    }
048                    catch (Exception e) {
049                    }
050    
051                    _addDefaultParameters();
052    
053                    _collectDefaultsFromRequestAttributes(request);
054    
055                    _collectFromPath(parameterPath);
056                    _collectFromRequestParameters(request);
057                    _collectFromJSONRPCRequest(jsonRPCRequest);
058                    _collectFromMap(parameterMap);
059            }
060    
061            public List<NameValue<String, Object>> getInnerParameters(String baseName) {
062                    return _jsonWebServiceActionParameters.getInnerParameters(baseName);
063            }
064    
065            public JSONRPCRequest getJSONRPCRequest() {
066                    return _jsonRPCRequest;
067            }
068    
069            public Object getParameter(String name) {
070                    return _jsonWebServiceActionParameters.get(name);
071            }
072    
073            public String[] getParameterNames() {
074                    String[] names = new String[_jsonWebServiceActionParameters.size()];
075    
076                    int i = 0;
077    
078                    for (String key : _jsonWebServiceActionParameters.keySet()) {
079                            names[i] = key;
080    
081                            i++;
082                    }
083    
084                    return names;
085            }
086    
087            public String getParameterTypeName(String name) {
088                    return _jsonWebServiceActionParameters.getParameterTypeName(name);
089            }
090    
091            public ServiceContext getServiceContext() {
092                    return _serviceContext;
093            }
094    
095            public boolean includeDefaultParameters() {
096                    return _jsonWebServiceActionParameters.includeDefaultParameters();
097            }
098    
099            private void _addDefaultParameters() {
100                    _jsonWebServiceActionParameters.put("serviceContext", Void.TYPE);
101            }
102    
103            private void _collectDefaultsFromRequestAttributes(
104                    HttpServletRequest request) {
105    
106                    Enumeration<String> enu = request.getAttributeNames();
107    
108                    while (enu.hasMoreElements()) {
109                            String attributeName = enu.nextElement();
110    
111                            Object value = request.getAttribute(attributeName);
112    
113                            _jsonWebServiceActionParameters.putDefaultParameter(
114                                    attributeName, value);
115                    }
116            }
117    
118            private void _collectFromJSONRPCRequest(JSONRPCRequest jsonRPCRequest) {
119                    if (jsonRPCRequest == null) {
120                            return;
121                    }
122    
123                    Set<String> parameterNames = jsonRPCRequest.getParameterNames();
124    
125                    for (String parameterName : parameterNames) {
126                            String value = jsonRPCRequest.getParameter(parameterName);
127    
128                            parameterName = CamelCaseUtil.normalizeCamelCase(parameterName);
129    
130                            _jsonWebServiceActionParameters.put(parameterName, value);
131                    }
132            }
133    
134            private void _collectFromMap(Map<String, Object> parameterMap) {
135                    if (parameterMap == null) {
136                            return;
137                    }
138    
139                    for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
140                            String parameterName = entry.getKey();
141    
142                            Object value = entry.getValue();
143    
144                            _jsonWebServiceActionParameters.put(parameterName, value);
145                    }
146            }
147    
148            private void _collectFromPath(String parameterPath) {
149                    if (parameterPath == null) {
150                            return;
151                    }
152    
153                    if (parameterPath.startsWith(StringPool.SLASH)) {
154                            parameterPath = parameterPath.substring(1);
155                    }
156    
157                    String[] parameterPathParts = StringUtil.split(
158                            parameterPath, CharPool.SLASH);
159    
160                    int i = 0;
161    
162                    while (i < parameterPathParts.length) {
163                            String name = parameterPathParts[i];
164    
165                            if (name.length() == 0) {
166                                    i++;
167    
168                                    continue;
169                            }
170    
171                            String value = null;
172    
173                            if (name.startsWith(StringPool.DASH)) {
174                                    name = name.substring(1);
175                            }
176                            else if (!name.startsWith(StringPool.PLUS)) {
177                                    i++;
178    
179                                    if (i >= parameterPathParts.length) {
180                                            throw new IllegalArgumentException(
181                                                    "Missing value for parameter " + name);
182                                    }
183    
184                                    value = parameterPathParts[i];
185                            }
186    
187                            name = CamelCaseUtil.toCamelCase(name);
188    
189                            _jsonWebServiceActionParameters.put(name, value);
190    
191                            i++;
192                    }
193            }
194    
195            private void _collectFromRequestParameters(HttpServletRequest request) {
196                    UploadServletRequest uploadServletRequest = null;
197    
198                    if (request instanceof UploadServletRequest) {
199                            uploadServletRequest = (UploadServletRequest)request;
200                    }
201    
202                    Enumeration<String> enu = request.getParameterNames();
203    
204                    while (enu.hasMoreElements()) {
205                            String name = enu.nextElement();
206    
207                            Object value = null;
208    
209                            if ((uploadServletRequest != null) &&
210                                    (uploadServletRequest.getFileName(name) != null)) {
211    
212                                    value = uploadServletRequest.getFile(name, true);
213                            }
214                            else {
215                                    String[] parameterValues = request.getParameterValues(name);
216    
217                                    if (parameterValues.length == 1) {
218                                            value = parameterValues[0];
219                                    }
220                                    else {
221                                            value = parameterValues;
222                                    }
223                            }
224    
225                            name = CamelCaseUtil.normalizeCamelCase(name);
226    
227                            _jsonWebServiceActionParameters.put(name, value);
228                    }
229            }
230    
231            private JSONRPCRequest _jsonRPCRequest;
232            private JSONWebServiceActionParametersMap _jsonWebServiceActionParameters =
233                    new JSONWebServiceActionParametersMap();
234            private ServiceContext _serviceContext;
235    
236    }