001
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
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 _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 = jodd.util.StringUtil.wordsToCamelCase(name, CharPool.DASH);
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.isFormField(parameterName)) {
192
193 value = uploadServletRequest.getFile(parameterName);
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 _parameters.put(parameterName, value);
208 }
209 }
210
211 private ServiceContext _mergeServiceContext(ServiceContext serviceContext) {
212 _serviceContext.setAddGroupPermissions(
213 serviceContext.getAddGroupPermissions());
214 _serviceContext.setAddGuestPermissions(
215 serviceContext.getAddGuestPermissions());
216
217 if (serviceContext.getAssetCategoryIds() != null) {
218 _serviceContext.setAssetCategoryIds(
219 serviceContext.getAssetCategoryIds());
220 }
221
222 if (serviceContext.getAssetLinkEntryIds() != null) {
223 _serviceContext.setAssetLinkEntryIds(
224 serviceContext.getAssetLinkEntryIds());
225 }
226
227 if (serviceContext.getAssetTagNames() != null) {
228 _serviceContext.setAssetTagNames(serviceContext.getAssetTagNames());
229 }
230
231 if (serviceContext.getAttributes() != null) {
232 _serviceContext.setAttributes(serviceContext.getAttributes());
233 }
234
235 if (Validator.isNotNull(serviceContext.getCommand())) {
236 _serviceContext.setCommand(serviceContext.getCommand());
237 }
238
239 if (serviceContext.getCompanyId() > 0) {
240 _serviceContext.setCompanyId(serviceContext.getCompanyId());
241 }
242
243 if (serviceContext.getCreateDate() != null) {
244 _serviceContext.setCreateDate(serviceContext.getCreateDate());
245 }
246
247 if (Validator.isNotNull(serviceContext.getCurrentURL())) {
248 _serviceContext.setCurrentURL(serviceContext.getCurrentURL());
249 }
250
251 if (serviceContext.getExpandoBridgeAttributes() != null) {
252 _serviceContext.setExpandoBridgeAttributes(
253 serviceContext.getExpandoBridgeAttributes());
254 }
255
256 if (serviceContext.getGroupPermissions() != null) {
257 _serviceContext.setGroupPermissions(
258 serviceContext.getGroupPermissions());
259 }
260
261 if (serviceContext.getGuestPermissions() != null) {
262 _serviceContext.setGuestPermissions(
263 serviceContext.getGuestPermissions());
264 }
265
266 if (serviceContext.getHeaders() != null) {
267 _serviceContext.setHeaders(serviceContext.getHeaders());
268 }
269
270 if (Validator.isNotNull(serviceContext.getLanguageId())) {
271 _serviceContext.setLanguageId(serviceContext.getLanguageId());
272 }
273
274 if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
275 _serviceContext.setLayoutFullURL(serviceContext.getLayoutFullURL());
276 }
277
278 if (Validator.isNotNull(serviceContext.getLayoutURL())) {
279 _serviceContext.setLayoutURL(serviceContext.getLayoutURL());
280 }
281
282 if (serviceContext.getModifiedDate() != null) {
283 _serviceContext.setModifiedDate(serviceContext.getModifiedDate());
284 }
285
286 if (Validator.isNotNull(serviceContext.getPathMain())) {
287 _serviceContext.setPathMain(serviceContext.getPathMain());
288 }
289
290 if (serviceContext.getPlid() > 0) {
291 _serviceContext.setPlid(serviceContext.getPlid());
292 }
293
294 if (Validator.isNotNull(serviceContext.getPortalURL())) {
295 _serviceContext.setPortalURL(serviceContext.getPortalURL());
296 }
297
298 if (serviceContext.getPortletPreferencesIds() != null) {
299 _serviceContext.setPortletPreferencesIds(
300 serviceContext.getPortletPreferencesIds());
301 }
302
303 if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
304 _serviceContext.setRemoteAddr(serviceContext.getRemoteAddr());
305 }
306
307 if (Validator.isNotNull(serviceContext.getRemoteHost())) {
308 _serviceContext.setRemoteHost(serviceContext.getRemoteHost());
309 }
310
311 if (serviceContext.getScopeGroupId() > 0) {
312 _serviceContext.setScopeGroupId(serviceContext.getScopeGroupId());
313 }
314
315 _serviceContext.setSignedIn(serviceContext.isSignedIn());
316
317 if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
318 _serviceContext.setUserDisplayURL(
319 serviceContext.getUserDisplayURL());
320 }
321
322 if (serviceContext.getUserId() > 0) {
323 _serviceContext.setUserId(serviceContext.getUserId());
324 }
325
326 if (Validator.isNotNull(serviceContext.getUuid())) {
327 _serviceContext.setUuid(serviceContext.getUuid());
328 }
329
330 if (serviceContext.getWorkflowAction() > 0) {
331 _serviceContext.setWorkflowAction(
332 serviceContext.getWorkflowAction());
333 }
334
335 return serviceContext;
336 }
337
338 private Map<String, List<KeyValue<String, Object>>> _innerParameters;
339 private JSONRPCRequest _jsonRpcRequest;
340 private Map<String, String> _parameterTypes;
341 private Map<String, Object> _parameters = new HashMap<String, Object>() {
342
343 @Override
344 public Object put(String key, Object value) {
345
346 if (key.startsWith(StringPool.DASH)) {
347 key = key.substring(1);
348
349 value = null;
350 }
351 else if (key.startsWith(StringPool.PLUS)) {
352 key = key.substring(1);
353
354 int pos = key.indexOf(CharPool.COLON);
355
356 if (pos != -1) {
357 value = key.substring(pos + 1);
358
359 key = key.substring(0, pos);
360 }
361
362 if (Validator.isNotNull(value)) {
363 if (_parameterTypes == null) {
364 _parameterTypes = new HashMap<String, String>();
365 }
366
367 _parameterTypes.put(key, value.toString());
368 }
369
370 value = Void.TYPE;
371 }
372
373 int pos = key.indexOf(CharPool.PERIOD);
374
375 if (pos != -1) {
376 String baseName = key.substring(0, pos);
377
378 String innerName = key.substring(pos + 1);
379
380 if (_innerParameters == null) {
381 _innerParameters =
382 new HashMap<String, List<KeyValue<String, Object>>>();
383 }
384
385 List<KeyValue<String, Object>> values =
386 _innerParameters.get(baseName);
387
388 if (values == null) {
389 values = new ArrayList<KeyValue<String, Object>>();
390
391 _innerParameters.put(baseName, values);
392 }
393
394 values.add(new KeyValue<String, Object>(innerName, value));
395
396 return value;
397 }
398
399 if ((_serviceContext != null) && key.equals("serviceContext")) {
400 if ((value != null) &&
401 ServiceContext.class.isAssignableFrom(value.getClass())) {
402
403 value = _mergeServiceContext((ServiceContext)value);
404 }
405 else {
406 value = _serviceContext;
407 }
408 }
409
410 return super.put(key, value);
411 }
412
413 };
414
415 private ServiceContext _serviceContext;
416
417 }