1
22
23 package com.liferay.portal.action;
24
25 import com.liferay.portal.kernel.json.JSONArray;
26 import com.liferay.portal.kernel.json.JSONFactoryUtil;
27 import com.liferay.portal.kernel.json.JSONObject;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.ParamUtil;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.service.ServiceContext;
34 import com.liferay.portal.service.ServiceContextUtil;
35 import com.liferay.portal.struts.JSONAction;
36 import com.liferay.portlet.tags.model.TagsAssetDisplay;
37 import com.liferay.portlet.tags.model.TagsAssetType;
38
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.struts.action.ActionForm;
50 import org.apache.struts.action.ActionMapping;
51
52
59 public class JSONServiceAction extends JSONAction {
60
61 public static JSONObject toJSONObject(TagsAssetDisplay assetDisplay) {
62 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
63
64 jsonObj.put("assetId", assetDisplay.getAssetId());
65 jsonObj.put("companyId", assetDisplay.getCompanyId());
66 jsonObj.put("userId", assetDisplay.getUserId());
67 jsonObj.put("userName", assetDisplay.getUserName());
68 jsonObj.put("createDate", assetDisplay.getCreateDate());
69 jsonObj.put("modifiedDate", assetDisplay.getModifiedDate());
70 jsonObj.put("classNameId", assetDisplay.getClassNameId());
71 jsonObj.put("className", assetDisplay.getClassName());
72 jsonObj.put("classPK", assetDisplay.getClassPK());
73 jsonObj.put("portletId", assetDisplay.getPortletId());
74 jsonObj.put("portletTitle", assetDisplay.getPortletTitle());
75 jsonObj.put("startDate", assetDisplay.getStartDate());
76 jsonObj.put("endDate", assetDisplay.getEndDate());
77 jsonObj.put("publishDate", assetDisplay.getPublishDate());
78 jsonObj.put("expirationDate", assetDisplay.getExpirationDate());
79 jsonObj.put("mimeType", assetDisplay.getMimeType());
80 jsonObj.put("title", assetDisplay.getTitle());
81 jsonObj.put("description", assetDisplay.getDescription());
82 jsonObj.put("summary", assetDisplay.getSummary());
83 jsonObj.put("url", assetDisplay.getUrl());
84 jsonObj.put("height", assetDisplay.getHeight());
85 jsonObj.put("width", assetDisplay.getWidth());
86 jsonObj.put("priority", assetDisplay.getPriority());
87 jsonObj.put("viewCount", assetDisplay.getViewCount());
88 jsonObj.put("tagsEntries", assetDisplay.getTagsEntries());
89
90 return jsonObj;
91 }
92
93 public static JSONObject toJSONObject(TagsAssetType assetType) {
94 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
95
96 jsonObj.put("classNameId", assetType.getClassNameId());
97 jsonObj.put("className", assetType.getClassName());
98 jsonObj.put("portletId", assetType.getPortletId());
99 jsonObj.put("portletTitle", assetType.getPortletTitle());
100
101 return jsonObj;
102 }
103
104 public String getJSON(
105 ActionMapping mapping, ActionForm form, HttpServletRequest request,
106 HttpServletResponse response)
107 throws Exception {
108
109 String className = ParamUtil.getString(request, "serviceClassName");
110 String methodName = ParamUtil.getString(request, "serviceMethodName");
111 String[] serviceParameters = StringUtil.split(
112 ParamUtil.getString(request, "serviceParameters"));
113 String[] serviceParameterTypes = StringUtil.split(
114 ParamUtil.getString(request, "serviceParameterTypes"));
115
116 if (!isValidRequest(request)) {
117 return null;
118 }
119
120 Class<?> classObj = Class.forName(className);
121
122 Object[] methodAndParameterTypes = getMethodAndParameterTypes(
123 classObj, methodName, serviceParameters, serviceParameterTypes);
124
125 if (methodAndParameterTypes != null) {
126 Method method = (Method)methodAndParameterTypes[0];
127 Class<?>[] parameterTypes = (Class[])methodAndParameterTypes[1];
128 Object[] args = new Object[serviceParameters.length];
129
130 for (int i = 0; i < serviceParameters.length; i++) {
131 args[i] = getArgValue(
132 request, classObj, methodName, serviceParameters[i],
133 parameterTypes[i]);
134 }
135
136 try {
137 if (_log.isDebugEnabled()) {
138 _log.debug(
139 "Invoking class " + classObj + " on method " +
140 method.getName() + " with args " + args);
141 }
142
143 Object returnObj = method.invoke(classObj, args);
144
145 if (returnObj != null) {
146 if (returnObj instanceof JSONArray) {
147 JSONArray jsonArray = (JSONArray)returnObj;
148
149 return jsonArray.toString();
150 }
151 else if (returnObj instanceof JSONObject) {
152 JSONObject jsonObj = (JSONObject)returnObj;
153
154 return jsonObj.toString();
155 }
156 else if (returnObj instanceof Boolean ||
157 returnObj instanceof Double ||
158 returnObj instanceof Integer ||
159 returnObj instanceof Long ||
160 returnObj instanceof Short ||
161 returnObj instanceof String) {
162
163 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
164
165 jsonObj.put("returnValue", returnObj.toString());
166
167 return jsonObj.toString();
168 }
169 else {
170 String returnValue = getReturnValue(returnObj);
171
172 if (returnValue == null) {
173 _log.error(
174 "Unsupported return type for class " +
175 classObj + " and method " + methodName);
176 }
177
178 return returnValue;
179 }
180 }
181 else {
182 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
183
184 return jsonObj.toString();
185 }
186 }
187 catch (InvocationTargetException ite) {
188 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
189
190 jsonObj.put("exception", ite.getCause().toString());
191
192 return jsonObj.toString();
193 }
194 }
195
196 return null;
197 }
198
199 protected Object getArgValue(
200 HttpServletRequest request, Class<?> classObj, String methodName,
201 String parameter, Class<?> parameterType)
202 throws Exception {
203
204 String parameterTypeName = parameterType.getName();
205
206 String value = ParamUtil.getString(request, parameter);
207
208 if (Validator.isNull(value)) {
209 return null;
210 }
211 else if (parameterTypeName.equals("boolean") ||
212 parameterTypeName.equals(Boolean.class.getName())) {
213
214 return Boolean.valueOf(ParamUtil.getBoolean(request, parameter));
215 }
216 else if (parameterTypeName.equals("double") ||
217 parameterTypeName.equals(Double.class.getName())) {
218
219 return new Double(ParamUtil.getDouble(request, parameter));
220 }
221 else if (parameterTypeName.equals("int") ||
222 parameterTypeName.equals(Integer.class.getName())) {
223
224 return new Integer(ParamUtil.getInteger(request, parameter));
225 }
226 else if (parameterTypeName.equals("long") ||
227 parameterTypeName.equals(Long.class.getName())) {
228
229 return new Long(ParamUtil.getLong(request, parameter));
230 }
231 else if (parameterTypeName.equals("short") ||
232 parameterTypeName.equals(Short.class.getName())) {
233
234 return new Short(ParamUtil.getShort(request, parameter));
235 }
236 else if (parameterTypeName.equals(Date.class.getName())) {
237 return new Date(ParamUtil.getLong(request, parameter));
238 }
239 else if (parameterTypeName.equals(ServiceContext.class.getName())) {
240 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
241
242 jsonObject.put("javaClass", ServiceContext.class.getName());
243
244 return ServiceContextUtil.deserialize(jsonObject);
245 }
246 else if (parameterTypeName.equals(String.class.getName())) {
247 return value;
248 }
249 else if (parameterTypeName.equals("[Ljava.lang.String;")) {
250 return StringUtil.split(value);
251 }
252 else {
253 _log.error(
254 "Unsupported parameter type for class " + classObj +
255 ", method " + methodName + ", parameter " + parameter +
256 ", and type " + parameterTypeName);
257
258 return null;
259 }
260 }
261
262 protected Object[] getMethodAndParameterTypes(
263 Class<?> classObj, String methodName, String[] parameters,
264 String[] parameterTypes)
265 throws Exception {
266
267 String parameterNames = StringUtil.merge(parameters);
268
269 String key =
270 classObj.getName() + "_METHOD_NAME_" + methodName +
271 "_PARAMETERS_" + parameterNames;
272
273 Object[] methodAndParameterTypes = _methodCache.get(key);
274
275 if (methodAndParameterTypes != null) {
276 return methodAndParameterTypes;
277 }
278
279 Method method = null;
280 Class<?>[] methodParameterTypes = null;
281
282 Method[] methods = classObj.getMethods();
283
284 for (int i = 0; i < methods.length; i++) {
285 Method curMethod = methods[i];
286
287 if (curMethod.getName().equals(methodName)) {
288 Class<?>[] curParameterTypes = curMethod.getParameterTypes();
289
290 if (curParameterTypes.length == parameters.length) {
291 if ((parameterTypes.length > 0) &&
292 (parameterTypes.length == curParameterTypes.length)) {
293
294 boolean match = true;
295
296 for (int j = 0; j < parameterTypes.length; j++) {
297 String t1 = parameterTypes[j];
298 String t2 = curParameterTypes[j].getName();
299
300 if (!t1.equals(t2)) {
301 match = false;
302 }
303 }
304
305 if (match) {
306 method = curMethod;
307 methodParameterTypes = curParameterTypes;
308
309 break;
310 }
311 }
312 else if (method != null) {
313 _log.error(
314 "Obscure method name for class " + classObj +
315 ", method " + methodName + ", and parameters " +
316 parameterNames);
317
318 return null;
319 }
320 else {
321 method = curMethod;
322 methodParameterTypes = curParameterTypes;
323 }
324 }
325 }
326 }
327
328 if (method != null) {
329 methodAndParameterTypes =
330 new Object[] {method, methodParameterTypes};
331
332 _methodCache.put(key, methodAndParameterTypes);
333
334 return methodAndParameterTypes;
335 }
336 else {
337 _log.error(
338 "No method found for class " + classObj + ", method " +
339 methodName + ", and parameters " + parameterNames);
340
341 return null;
342 }
343 }
344
345 protected String getReturnValue(Object returnObj) throws Exception {
346 if (returnObj instanceof TagsAssetDisplay) {
347 return getReturnValue((TagsAssetDisplay)returnObj);
348 }
349 else if (returnObj instanceof TagsAssetDisplay[]) {
350 return getReturnValue((TagsAssetDisplay[])returnObj);
351 }
352 else if (returnObj instanceof TagsAssetType) {
353 return getReturnValue((TagsAssetType)returnObj);
354 }
355 else if (returnObj instanceof TagsAssetType[]) {
356 return getReturnValue((TagsAssetType[])returnObj);
357 }
358
359 return null;
360 }
361
362 protected String getReturnValue(TagsAssetDisplay assetDisplay)
363 throws Exception {
364
365 JSONObject jsonObj = toJSONObject(assetDisplay);
366
367 return jsonObj.toString();
368 }
369
370 protected String getReturnValue(TagsAssetDisplay[] assetDisplays)
371 throws Exception {
372
373 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
374
375 for (int i = 0; i < assetDisplays.length; i++) {
376 TagsAssetDisplay assetDisplay = assetDisplays[i];
377
378 jsonArray.put(toJSONObject(assetDisplay));
379 }
380
381 return jsonArray.toString();
382 }
383
384 protected String getReturnValue(TagsAssetType assetType)
385 throws Exception {
386
387 JSONObject jsonObj = toJSONObject(assetType);
388
389 return jsonObj.toString();
390 }
391
392 protected String getReturnValue(TagsAssetType[] assetTypes)
393 throws Exception {
394
395 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
396
397 for (int i = 0; i < assetTypes.length; i++) {
398 TagsAssetType assetType = assetTypes[i];
399
400 jsonArray.put(toJSONObject(assetType));
401 }
402
403 return jsonArray.toString();
404 }
405
406 protected boolean isValidRequest(HttpServletRequest request) {
407 String className = ParamUtil.getString(request, "serviceClassName");
408
409 if (className.contains(".service.http.") &&
410 className.endsWith("ServiceJSON")) {
411
412 return true;
413 }
414 else {
415 return false;
416 }
417 }
418
419 private static Log _log = LogFactoryUtil.getLog(JSONServiceAction.class);
420
421 private Map<String, Object[]> _methodCache =
422 new HashMap<String, Object[]>();
423
424 }