001    /**
002     * Copyright (c) 2000-2012 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.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    
025    import java.util.ArrayList;
026    import java.util.Enumeration;
027    import java.util.HashMap;
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.KeyValue;
035    
036    /**
037     * @author Igor Spasic
038     */
039    public class JSONWebServiceActionParameters {
040    
041            public void collectAll(
042                    HttpServletRequest request, String pathParameters,
043                    JSONRPCRequest jsonRpcRequest) {
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(pathParameters);
058                    _collectFromRequestParameters(request);
059                    _collectFromJSONRPCRequest(jsonRpcRequest);
060            }
061    
062            public List<KeyValue<String, Object>> getInnerParameters(String baseName) {
063                    if (_innerParameters == null) {
064                            return null;
065                    }
066    
067                    return _innerParameters.get(baseName);
068            }
069    
070            public JSONRPCRequest getJSONRPCRequest() {
071                    return _jsonRpcRequest;
072            }
073    
074            public Object getParameter(String name) {
075                    return _parameters.get(name);
076            }
077    
078            public String[] getParameterNames() {
079                    String[] names = new String[_parameters.size()];
080    
081                    int i = 0;
082    
083                    for (String key : _parameters.keySet()) {
084                            names[i] = key;
085    
086                            i++;
087                    }
088    
089                    return names;
090            }
091    
092            public String getParameterTypeName(String name) {
093                    if (_parameterTypes == null) {
094                            return null;
095                    }
096    
097                    return _parameterTypes.get(name);
098            }
099    
100            private void _addDefaultParameters() {
101                    _parameters.put("serviceContext", Void.TYPE);
102            }
103    
104            private void _collectDefaultsFromRequestAttributes(
105                    HttpServletRequest request) {
106    
107                    Enumeration<String> enu = request.getAttributeNames();
108    
109                    while (enu.hasMoreElements()) {
110                            String attributeName = enu.nextElement();
111    
112                            Object value = request.getAttribute(attributeName);
113    
114                            _parameters.put(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                            _parameters.put(parameterName, value);
131                    }
132            }
133    
134            private void _collectFromPath(String pathParameters) {
135                    if (pathParameters == null) {
136                            return;
137                    }
138    
139                    if (pathParameters.startsWith(StringPool.SLASH)) {
140                            pathParameters = pathParameters.substring(1);
141                    }
142    
143                    String[] pathParametersParts = StringUtil.split(
144                            pathParameters, CharPool.SLASH);
145    
146                    int i = 0;
147    
148                    while (i < pathParametersParts.length) {
149                            String name = pathParametersParts[i];
150    
151                            if (name.length() == 0) {
152                                    i++;
153    
154                                    continue;
155                            }
156    
157                            String value = null;
158    
159                            if (name.startsWith(StringPool.DASH)) {
160                                    name = name.substring(1);
161                            }
162                            else if (!name.startsWith(StringPool.PLUS)) {
163                                    i++;
164    
165                                    value = pathParametersParts[i];
166                            }
167    
168                            name = CamelCaseUtil.toCamelCase(name);
169    
170                            _parameters.put(name, value);
171    
172                            i++;
173                    }
174            }
175    
176            private void _collectFromRequestParameters(HttpServletRequest request) {
177                    UploadServletRequest uploadServletRequest = null;
178    
179                    if (request instanceof UploadServletRequest) {
180                            uploadServletRequest = (UploadServletRequest)request;
181                    }
182    
183                    Enumeration<String> enu = request.getParameterNames();
184    
185                    while (enu.hasMoreElements()) {
186                            String parameterName = enu.nextElement();
187    
188                            Object value = null;
189    
190                            if ((uploadServletRequest != null) &&
191                                    (uploadServletRequest.getFileName(parameterName) != null)) {
192    
193                                    value = uploadServletRequest.getFile(parameterName, true);
194                            }
195                            else {
196                                    String[] parameterValues = request.getParameterValues(
197                                            parameterName);
198    
199                                    if (parameterValues.length == 1) {
200                                            value = parameterValues[0];
201                                    }
202                                    else {
203                                            value = parameterValues;
204                                    }
205                            }
206    
207                            parameterName = CamelCaseUtil.normalizeCamelCase(parameterName);
208    
209                            _parameters.put(parameterName, value);
210                    }
211            }
212    
213            private ServiceContext _mergeServiceContext(ServiceContext serviceContext) {
214                    _serviceContext.setAddGroupPermissions(
215                            serviceContext.isAddGroupPermissions());
216                    _serviceContext.setAddGuestPermissions(
217                            serviceContext.isAddGuestPermissions());
218    
219                    if (serviceContext.getAssetCategoryIds() != null) {
220                            _serviceContext.setAssetCategoryIds(
221                                    serviceContext.getAssetCategoryIds());
222                    }
223    
224                    if (serviceContext.getAssetLinkEntryIds() != null) {
225                            _serviceContext.setAssetLinkEntryIds(
226                                    serviceContext.getAssetLinkEntryIds());
227                    }
228    
229                    if (serviceContext.getAssetTagNames() != null) {
230                            _serviceContext.setAssetTagNames(serviceContext.getAssetTagNames());
231                    }
232    
233                    if (serviceContext.getAttributes() != null) {
234                            _serviceContext.setAttributes(serviceContext.getAttributes());
235                    }
236    
237                    if (Validator.isNotNull(serviceContext.getCommand())) {
238                            _serviceContext.setCommand(serviceContext.getCommand());
239                    }
240    
241                    if (serviceContext.getCompanyId() > 0) {
242                            _serviceContext.setCompanyId(serviceContext.getCompanyId());
243                    }
244    
245                    if (serviceContext.getCreateDate() != null) {
246                            _serviceContext.setCreateDate(serviceContext.getCreateDate());
247                    }
248    
249                    if (Validator.isNotNull(serviceContext.getCurrentURL())) {
250                            _serviceContext.setCurrentURL(serviceContext.getCurrentURL());
251                    }
252    
253                    if (serviceContext.getExpandoBridgeAttributes() != null) {
254                            _serviceContext.setExpandoBridgeAttributes(
255                                    serviceContext.getExpandoBridgeAttributes());
256                    }
257    
258                    if (serviceContext.getGroupPermissions() != null) {
259                            _serviceContext.setGroupPermissions(
260                                    serviceContext.getGroupPermissions());
261                    }
262    
263                    if (serviceContext.getGuestPermissions() != null) {
264                            _serviceContext.setGuestPermissions(
265                                    serviceContext.getGuestPermissions());
266                    }
267    
268                    if (serviceContext.getHeaders() != null) {
269                            _serviceContext.setHeaders(serviceContext.getHeaders());
270                    }
271    
272                    if (Validator.isNotNull(serviceContext.getLanguageId())) {
273                            _serviceContext.setLanguageId(serviceContext.getLanguageId());
274                    }
275    
276                    if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
277                            _serviceContext.setLayoutFullURL(serviceContext.getLayoutFullURL());
278                    }
279    
280                    if (Validator.isNotNull(serviceContext.getLayoutURL())) {
281                            _serviceContext.setLayoutURL(serviceContext.getLayoutURL());
282                    }
283    
284                    if (serviceContext.getModifiedDate() != null) {
285                            _serviceContext.setModifiedDate(serviceContext.getModifiedDate());
286                    }
287    
288                    if (Validator.isNotNull(serviceContext.getPathMain())) {
289                            _serviceContext.setPathMain(serviceContext.getPathMain());
290                    }
291    
292                    if (serviceContext.getPlid() > 0) {
293                            _serviceContext.setPlid(serviceContext.getPlid());
294                    }
295    
296                    if (Validator.isNotNull(serviceContext.getPortalURL())) {
297                            _serviceContext.setPortalURL(serviceContext.getPortalURL());
298                    }
299    
300                    if (serviceContext.getPortletPreferencesIds() != null) {
301                            _serviceContext.setPortletPreferencesIds(
302                                    serviceContext.getPortletPreferencesIds());
303                    }
304    
305                    if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
306                            _serviceContext.setRemoteAddr(serviceContext.getRemoteAddr());
307                    }
308    
309                    if (Validator.isNotNull(serviceContext.getRemoteHost())) {
310                            _serviceContext.setRemoteHost(serviceContext.getRemoteHost());
311                    }
312    
313                    if (serviceContext.getScopeGroupId() > 0) {
314                            _serviceContext.setScopeGroupId(serviceContext.getScopeGroupId());
315                    }
316    
317                    _serviceContext.setSignedIn(serviceContext.isSignedIn());
318    
319                    if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
320                            _serviceContext.setUserDisplayURL(
321                                    serviceContext.getUserDisplayURL());
322                    }
323    
324                    if (serviceContext.getUserId() > 0) {
325                            _serviceContext.setUserId(serviceContext.getUserId());
326                    }
327    
328                    if (Validator.isNotNull(serviceContext.getUuid())) {
329                            _serviceContext.setUuid(serviceContext.getUuid());
330                    }
331    
332                    if (serviceContext.getWorkflowAction() > 0) {
333                            _serviceContext.setWorkflowAction(
334                                    serviceContext.getWorkflowAction());
335                    }
336    
337                    return serviceContext;
338            }
339    
340            private Map<String, List<KeyValue<String, Object>>> _innerParameters;
341            private JSONRPCRequest _jsonRpcRequest;
342            private Map<String, Object> _parameters = new HashMap<String, Object>() {
343    
344                    @Override
345                    public Object put(String key, Object value) {
346    
347                            if (key.startsWith(StringPool.DASH)) {
348                                    key = key.substring(1);
349    
350                                    value = null;
351                            }
352                            else if (key.startsWith(StringPool.PLUS)) {
353                                    key = key.substring(1);
354    
355                                    int pos = key.indexOf(CharPool.COLON);
356    
357                                    if (pos != -1) {
358                                            value = key.substring(pos + 1);
359    
360                                            key = key.substring(0, pos);
361                                    }
362    
363                                    if (Validator.isNotNull(value)) {
364                                            if (_parameterTypes == null) {
365                                                    _parameterTypes = new HashMap<String, String>();
366                                            }
367    
368                                            _parameterTypes.put(key, value.toString());
369                                    }
370    
371                                    value = Void.TYPE;
372                            }
373    
374                            int pos = key.indexOf(CharPool.PERIOD);
375    
376                            if (pos != -1) {
377                                    String baseName = key.substring(0, pos);
378    
379                                    String innerName = key.substring(pos + 1);
380    
381                                    if (_innerParameters == null) {
382                                            _innerParameters =
383                                                    new HashMap<String, List<KeyValue<String, Object>>>();
384                                    }
385    
386                                    List<KeyValue<String, Object>> values = _innerParameters.get(
387                                            baseName);
388    
389                                    if (values == null) {
390                                            values = new ArrayList<KeyValue<String, Object>>();
391    
392                                            _innerParameters.put(baseName, values);
393                                    }
394    
395                                    values.add(new KeyValue<String, Object>(innerName, value));
396    
397                                    return value;
398                            }
399    
400                            if ((_serviceContext != null) && key.equals("serviceContext")) {
401                                    if ((value != null) &&
402                                            ServiceContext.class.isAssignableFrom(value.getClass())) {
403    
404                                            value = _mergeServiceContext((ServiceContext)value);
405                                    }
406                                    else {
407                                            value = _serviceContext;
408                                    }
409                            }
410    
411                            return super.put(key, value);
412                    }
413    
414            };
415    
416            private Map<String, String> _parameterTypes;
417            private ServiceContext _serviceContext;
418    
419    }