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