1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
18  import com.liferay.documentlibrary.service.DLServiceUtil;
19  import com.liferay.mail.service.MailServiceUtil;
20  import com.liferay.portal.kernel.json.JSONArray;
21  import com.liferay.portal.kernel.json.JSONException;
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.ArrayUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LocalizationUtil;
29  import com.liferay.portal.kernel.util.MethodInvoker;
30  import com.liferay.portal.kernel.util.MethodWrapper;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.BaseModel;
36  import com.liferay.portal.service.ServiceContext;
37  import com.liferay.portal.service.ServiceContextUtil;
38  import com.liferay.portal.struts.JSONAction;
39  import com.liferay.portlet.asset.model.AssetEntryDisplay;
40  import com.liferay.portlet.asset.model.AssetEntryType;
41  
42  import java.lang.reflect.InvocationTargetException;
43  import java.lang.reflect.Method;
44  import java.lang.reflect.Type;
45  
46  import java.util.Arrays;
47  import java.util.Date;
48  import java.util.HashMap;
49  import java.util.HashSet;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.Set;
53  import java.util.regex.Matcher;
54  import java.util.regex.Pattern;
55  
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.struts.action.ActionForm;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="JSONServiceAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Karthik Sudarshan
67   * @author Julio Camarero
68   * @author Eduardo Lundgren
69   */
70  public class JSONServiceAction extends JSONAction {
71  
72      public JSONServiceAction() {
73          _invalidClassNames.add(DLLocalServiceUtil.class.getName());
74          _invalidClassNames.add(DLServiceUtil.class.getName());
75          _invalidClassNames.add(MailServiceUtil.class.getName());
76      }
77  
78      public String getJSON(
79              ActionMapping mapping, ActionForm form, HttpServletRequest request,
80              HttpServletResponse response)
81          throws Exception {
82  
83          String className = ParamUtil.getString(request, "serviceClassName");
84          String methodName = ParamUtil.getString(request, "serviceMethodName");
85          String[] serviceParameters = getStringArrayFromJSON(
86              request, "serviceParameters");
87          String[] serviceParameterTypes = getStringArrayFromJSON(
88              request, "serviceParameterTypes");
89  
90          if (!isValidRequest(request)) {
91              return null;
92          }
93  
94          Thread currentThread = Thread.currentThread();
95  
96          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
97  
98          Class<?> classObj = contextClassLoader.loadClass(className);
99  
100         Object[] methodAndParameterTypes = getMethodAndParameterTypes(
101             classObj, methodName, serviceParameters, serviceParameterTypes);
102 
103         if (methodAndParameterTypes != null) {
104             Method method = (Method)methodAndParameterTypes[0];
105             Type[] parameterTypes = (Type[])methodAndParameterTypes[1];
106             Object[] args = new Object[serviceParameters.length];
107 
108             for (int i = 0; i < serviceParameters.length; i++) {
109                 args[i] = getArgValue(
110                     request, classObj, methodName, serviceParameters[i],
111                     parameterTypes[i]);
112             }
113 
114             try {
115                 if (_log.isDebugEnabled()) {
116                     _log.debug(
117                         "Invoking " + classObj + " on method " +
118                             method.getName() + " with args " +
119                                 Arrays.toString(args));
120                 }
121 
122                 Object returnObj = method.invoke(classObj, args);
123 
124                 if (returnObj != null) {
125                     return getReturnValue(returnObj);
126                 }
127                 else {
128                     JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
129 
130                     return jsonObject.toString();
131                 }
132             }
133             catch (Exception e) {
134                 if (_log.isDebugEnabled()) {
135                     _log.debug(
136                         "Invoked " + classObj + " on method " +
137                             method.getName() + " with args " +
138                                 Arrays.toString(args),
139                         e);
140                 }
141 
142                 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
143 
144                 if (e instanceof InvocationTargetException) {
145                     jsonObject.put("exception", e.getCause().toString());
146                 }
147                 else {
148                     jsonObject.put("exception", e.getMessage());
149                 }
150 
151                 return jsonObject.toString();
152             }
153         }
154 
155         return null;
156     }
157 
158     protected Object getArgValue(
159             HttpServletRequest request, Class<?> classObj, String methodName,
160             String parameter, Type parameterType)
161         throws Exception {
162 
163         String typeNameOrClassDescriptor = getTypeNameOrClassDescriptor(
164             parameterType);
165 
166         String value = ParamUtil.getString(request, parameter);
167 
168         if (Validator.isNull(value) &&
169             !typeNameOrClassDescriptor.equals("[Ljava.lang.String;")) {
170 
171             return null;
172         }
173         else if (typeNameOrClassDescriptor.equals("boolean") ||
174                  typeNameOrClassDescriptor.equals(Boolean.class.getName())) {
175 
176             return Boolean.valueOf(ParamUtil.getBoolean(request, parameter));
177         }
178         else if (typeNameOrClassDescriptor.equals("double") ||
179                  typeNameOrClassDescriptor.equals(Double.class.getName())) {
180 
181             return new Double(ParamUtil.getDouble(request, parameter));
182         }
183         else if (typeNameOrClassDescriptor.equals("int") ||
184                  typeNameOrClassDescriptor.equals(Integer.class.getName())) {
185 
186             return new Integer(ParamUtil.getInteger(request, parameter));
187         }
188         else if (typeNameOrClassDescriptor.equals("long") ||
189                  typeNameOrClassDescriptor.equals(Long.class.getName())) {
190 
191             return new Long(ParamUtil.getLong(request, parameter));
192         }
193         else if (typeNameOrClassDescriptor.equals("short") ||
194                  typeNameOrClassDescriptor.equals(Short.class.getName())) {
195 
196             return new Short(ParamUtil.getShort(request, parameter));
197         }
198         else if (typeNameOrClassDescriptor.equals(Date.class.getName())) {
199             return new Date(ParamUtil.getLong(request, parameter));
200         }
201         else if (typeNameOrClassDescriptor.equals(
202                     ServiceContext.class.getName())) {
203 
204             JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
205 
206             jsonObject.put("javaClass", ServiceContext.class.getName());
207 
208             return ServiceContextUtil.deserialize(jsonObject);
209         }
210         else if (typeNameOrClassDescriptor.equals(String.class.getName())) {
211             return value;
212         }
213         else if (typeNameOrClassDescriptor.equals("[Z")) {
214             return ParamUtil.getBooleanValues(request, parameter);
215         }
216         else if (typeNameOrClassDescriptor.equals("[D")) {
217             return ParamUtil.getDoubleValues(request, parameter);
218         }
219         else if (typeNameOrClassDescriptor.equals("[F")) {
220             return ParamUtil.getFloatValues(request, parameter);
221         }
222         else if (typeNameOrClassDescriptor.equals("[I")) {
223             return ParamUtil.getIntegerValues(request, parameter);
224         }
225         else if (typeNameOrClassDescriptor.equals("[J")) {
226             return ParamUtil.getLongValues(request, parameter);
227         }
228         else if (typeNameOrClassDescriptor.equals("[S")) {
229             return ParamUtil.getShortValues(request, parameter);
230         }
231         else if (typeNameOrClassDescriptor.equals("[Ljava.lang.String;")) {
232             return StringUtil.split(value);
233         }
234         else if (typeNameOrClassDescriptor.equals("[[Z")) {
235             String[] values = request.getParameterValues(parameter);
236 
237             if ((values != null) && (values.length > 0)) {
238                 String[] values0 = StringUtil.split(values[0]);
239 
240                 boolean[][] doubleArray =
241                     new boolean[values.length][values0.length];
242 
243                 for (int i = 0; i < values.length; i++) {
244                     String[] curValues = StringUtil.split(values[i]);
245 
246                     for (int j = 0; j < curValues.length; j++) {
247                         doubleArray[i][j] = GetterUtil.getBoolean(curValues[j]);
248                     }
249                 }
250 
251                 return doubleArray;
252             }
253             else {
254                 return new boolean[0][0];
255             }
256         }
257         else if (typeNameOrClassDescriptor.equals("[[D")) {
258             String[] values = request.getParameterValues(parameter);
259 
260             if ((values != null) && (values.length > 0)) {
261                 String[] values0 = StringUtil.split(values[0]);
262 
263                 double[][] doubleArray =
264                     new double[values.length][values0.length];
265 
266                 for (int i = 0; i < values.length; i++) {
267                     String[] curValues = StringUtil.split(values[i]);
268 
269                     for (int j = 0; j < curValues.length; j++) {
270                         doubleArray[i][j] = GetterUtil.getDouble(curValues[j]);
271                     }
272                 }
273 
274                 return doubleArray;
275             }
276             else {
277                 return new double[0][0];
278             }
279         }
280         else if (typeNameOrClassDescriptor.equals("[[F")) {
281             String[] values = request.getParameterValues(parameter);
282 
283             if ((values != null) && (values.length > 0)) {
284                 String[] values0 = StringUtil.split(values[0]);
285 
286                 float[][] doubleArray =
287                     new float[values.length][values0.length];
288 
289                 for (int i = 0; i < values.length; i++) {
290                     String[] curValues = StringUtil.split(values[i]);
291 
292                     for (int j = 0; j < curValues.length; j++) {
293                         doubleArray[i][j] = GetterUtil.getFloat(curValues[j]);
294                     }
295                 }
296 
297                 return doubleArray;
298             }
299             else {
300                 return new float[0][0];
301             }
302         }
303         else if (typeNameOrClassDescriptor.equals("[[I")) {
304             String[] values = request.getParameterValues(parameter);
305 
306             if ((values != null) && (values.length > 0)) {
307                 String[] values0 = StringUtil.split(values[0]);
308 
309                 int[][] doubleArray =
310                     new int[values.length][values0.length];
311 
312                 for (int i = 0; i < values.length; i++) {
313                     String[] curValues = StringUtil.split(values[i]);
314 
315                     for (int j = 0; j < curValues.length; j++) {
316                         doubleArray[i][j] = GetterUtil.getInteger(curValues[j]);
317                     }
318                 }
319 
320                 return doubleArray;
321             }
322             else {
323                 return new int[0][0];
324             }
325         }
326         else if (typeNameOrClassDescriptor.equals("[[J")) {
327             String[] values = request.getParameterValues(parameter);
328 
329             if ((values != null) && (values.length > 0)) {
330                 String[] values0 = StringUtil.split(values[0]);
331 
332                 long[][] doubleArray =
333                     new long[values.length][values0.length];
334 
335                 for (int i = 0; i < values.length; i++) {
336                     String[] curValues = StringUtil.split(values[i]);
337 
338                     for (int j = 0; j < curValues.length; j++) {
339                         doubleArray[i][j] = GetterUtil.getLong(curValues[j]);
340                     }
341                 }
342 
343                 return doubleArray;
344             }
345             else {
346                 return new long[0][0];
347             }
348         }
349         else if (typeNameOrClassDescriptor.equals("[[S")) {
350             String[] values = request.getParameterValues(parameter);
351 
352             if ((values != null) && (values.length > 0)) {
353                 String[] values0 = StringUtil.split(values[0]);
354 
355                 short[][] doubleArray =
356                     new short[values.length][values0.length];
357 
358                 for (int i = 0; i < values.length; i++) {
359                     String[] curValues = StringUtil.split(values[i]);
360 
361                     for (int j = 0; j < curValues.length; j++) {
362                         doubleArray[i][j] = GetterUtil.getShort(curValues[j]);
363                     }
364                 }
365 
366                 return doubleArray;
367             }
368             else {
369                 return new short[0][0];
370             }
371         }
372         else if (typeNameOrClassDescriptor.equals("[[Ljava.lang.String")) {
373             String[] values = request.getParameterValues(parameter);
374 
375             if ((values != null) && (values.length > 0)) {
376                 String[] values0 = StringUtil.split(values[0]);
377 
378                 String[][] doubleArray =
379                     new String[values.length][values0.length];
380 
381                 for (int i = 0; i < values.length; i++) {
382                     doubleArray[i] = StringUtil.split(values[i]);
383                 }
384 
385                 return doubleArray;
386             }
387             else {
388                 return new String[0][0];
389             }
390         }
391         else if (typeNameOrClassDescriptor.equals(
392             "java.util.Map<java.util.Locale, java.lang.String>")) {
393 
394             JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
395 
396             return LocalizationUtil.deserialize(jsonObject);
397         }
398         else {
399             _log.error(
400                 "Unsupported parameter type for class " + classObj +
401                     ", method " + methodName + ", parameter " + parameter +
402                         ", and type " + typeNameOrClassDescriptor);
403 
404             return null;
405         }
406     }
407 
408     protected Object[] getMethodAndParameterTypes(
409             Class<?> classObj, String methodName, String[] parameters,
410             String[] parameterTypes)
411         throws Exception {
412 
413         String parameterNames = StringUtil.merge(parameters);
414 
415         String key =
416             classObj.getName() + "_METHOD_NAME_" + methodName +
417                 "_PARAMETERS_" + parameterNames;
418 
419         Object[] methodAndParameterTypes = _methodCache.get(key);
420 
421         if (methodAndParameterTypes != null) {
422             return methodAndParameterTypes;
423         }
424 
425         Method method = null;
426         Type[] methodParameterTypes = null;
427 
428         Method[] methods = classObj.getMethods();
429 
430         for (int i = 0; i < methods.length; i++) {
431             Method curMethod = methods[i];
432 
433             if (curMethod.getName().equals(methodName)) {
434                 Type[] curParameterTypes = curMethod.getGenericParameterTypes();
435 
436                 if (curParameterTypes.length == parameters.length) {
437                     if ((parameterTypes.length > 0) &&
438                         (parameterTypes.length == curParameterTypes.length)) {
439 
440                         boolean match = true;
441 
442                         for (int j = 0; j < parameterTypes.length; j++) {
443                             String t1 = parameterTypes[j];
444                             String t2 = getTypeNameOrClassDescriptor(
445                                 curParameterTypes[j]);
446 
447                             if (!t1.equals(t2)) {
448                                 match = false;
449                             }
450                         }
451 
452                         if (match) {
453                             method = curMethod;
454                             methodParameterTypes = curParameterTypes;
455 
456                             break;
457                         }
458                     }
459                     else if (method != null) {
460                         _log.error(
461                             "Obscure method name for class " + classObj +
462                                 ", method " + methodName + ", and parameters " +
463                                     parameterNames);
464 
465                         return null;
466                     }
467                     else {
468                         method = curMethod;
469                         methodParameterTypes = curParameterTypes;
470                     }
471                 }
472             }
473         }
474 
475         if (method != null) {
476             methodAndParameterTypes = new Object[] {
477                 method, methodParameterTypes
478             };
479 
480             _methodCache.put(key, methodAndParameterTypes);
481 
482             return methodAndParameterTypes;
483         }
484         else {
485             _log.error(
486                 "No method found for class " + classObj + ", method " +
487                     methodName + ", and parameters " + parameterNames);
488 
489             return null;
490         }
491     }
492 
493     protected String getReturnValue(AssetEntryDisplay assetEntryDisplay)
494         throws Exception {
495 
496         JSONObject jsonObject = toJSONObject(assetEntryDisplay);
497 
498         return jsonObject.toString();
499     }
500 
501     protected String getReturnValue(AssetEntryDisplay[] assetEntryDisplays)
502         throws Exception {
503 
504         JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
505 
506         for (int i = 0; i < assetEntryDisplays.length; i++) {
507             AssetEntryDisplay assetEntryDisplay = assetEntryDisplays[i];
508 
509             jsonArray.put(toJSONObject(assetEntryDisplay));
510         }
511 
512         return jsonArray.toString();
513     }
514 
515     protected String getReturnValue(AssetEntryType assetEntryType)
516         throws Exception {
517 
518         JSONObject jsonObject = toJSONObject(assetEntryType);
519 
520         return jsonObject.toString();
521     }
522 
523     protected String getReturnValue(AssetEntryType[] assetEntryTypes)
524         throws Exception {
525 
526         JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
527 
528         for (int i = 0; i < assetEntryTypes.length; i++) {
529             AssetEntryType assetEntryType = assetEntryTypes[i];
530 
531             jsonArray.put(toJSONObject(assetEntryType));
532         }
533 
534         return jsonArray.toString();
535     }
536 
537     protected String getReturnValue(Object returnObj) throws Exception {
538         if ((returnObj instanceof Boolean) || (returnObj instanceof Double) ||
539             (returnObj instanceof Integer) || (returnObj instanceof Long) ||
540             (returnObj instanceof Short) || (returnObj instanceof String)) {
541 
542             JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
543 
544             jsonObject.put("returnValue", returnObj.toString());
545 
546             return jsonObject.toString();
547         }
548         else if (returnObj instanceof BaseModel<?>) {
549             String serlializerClassName = getSerializerClassName(returnObj);
550 
551             MethodWrapper methodWrapper = new MethodWrapper(
552                 serlializerClassName, "toJSONObject", returnObj);
553 
554             JSONObject jsonObject = (JSONObject)MethodInvoker.invoke(
555                 methodWrapper, false);
556 
557             return jsonObject.toString();
558         }
559         else if (returnObj instanceof BaseModel<?>[]) {
560             JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
561 
562             BaseModel<?>[] returnArray = (BaseModel[])returnObj;
563 
564             if (returnArray.length > 0) {
565                 BaseModel<?> returnItem0 = returnArray[0];
566 
567                 String serializerClassName = getSerializerClassName(
568                     returnItem0);
569 
570                 MethodWrapper methodWrapper = new MethodWrapper(
571                     serializerClassName, "toJSONArray", returnObj);
572 
573                 jsonArray = (JSONArray)MethodInvoker.invoke(
574                     methodWrapper, false);
575             }
576 
577             return jsonArray.toString();
578         }
579         else if (returnObj instanceof BaseModel<?>[][]) {
580             JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
581 
582             BaseModel<?>[][] returnArray = (BaseModel<?>[][])returnObj;
583 
584             if ((returnArray.length > 0) &&
585                 (returnArray[0].length > 0)) {
586 
587                 BaseModel<?> returnItem0 = returnArray[0][0];
588 
589                 String serializerClassName = getSerializerClassName(
590                     returnItem0);
591 
592                 MethodWrapper methodWrapper = new MethodWrapper(
593                     serializerClassName, "toJSONArray", returnObj);
594 
595                 jsonArray = (JSONArray)MethodInvoker.invoke(
596                     methodWrapper, false);
597             }
598 
599             return jsonArray.toString();
600         }
601         else if (returnObj instanceof List<?>) {
602             JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
603 
604             List<Object> returnList = (List<Object>)returnObj;
605 
606             if (!returnList.isEmpty()) {
607                 Object returnItem0 = returnList.get(0);
608 
609                 String serlializerClassName = getSerializerClassName(
610                     returnItem0);
611 
612                 MethodWrapper methodWrapper = new MethodWrapper(
613                     serlializerClassName, "toJSONArray", returnObj);
614 
615                 jsonArray = (JSONArray)MethodInvoker.invoke(
616                     methodWrapper, false);
617             }
618 
619             return jsonArray.toString();
620         }
621         else if (returnObj instanceof JSONArray) {
622             JSONArray jsonArray = (JSONArray)returnObj;
623 
624             return jsonArray.toString();
625         }
626         else if (returnObj instanceof JSONObject) {
627             JSONObject jsonObject = (JSONObject)returnObj;
628 
629             return jsonObject.toString();
630         }
631         else if (returnObj instanceof AssetEntryDisplay) {
632             return getReturnValue((AssetEntryDisplay)returnObj);
633         }
634         else if (returnObj instanceof AssetEntryDisplay[]) {
635             return getReturnValue((AssetEntryDisplay[])returnObj);
636         }
637         else if (returnObj instanceof AssetEntryType) {
638             return getReturnValue((AssetEntryType)returnObj);
639         }
640         else if (returnObj instanceof AssetEntryType[]) {
641             return getReturnValue((AssetEntryType[])returnObj);
642         }
643         else {
644             return JSONFactoryUtil.serialize(returnObj);
645         }
646     }
647 
648     protected String getSerializerClassName(Object obj) {
649         String serlializerClassName = StringUtil.replace(
650             obj.getClass().getName(),
651             new String[] {".model.impl.", "Impl"},
652             new String[] {".service.http.", "JSONSerializer"});
653 
654         return serlializerClassName;
655     }
656 
657     protected String[] getStringArrayFromJSON(
658             HttpServletRequest request, String param)
659         throws JSONException {
660 
661         String json = ParamUtil.getString(request, param, "[]");
662 
663         JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
664 
665         return ArrayUtil.toStringArray(jsonArray);
666     }
667 
668     protected String getTypeNameOrClassDescriptor(Type type) {
669         String typeName = type.toString();
670 
671         if (typeName.contains("class ")) {
672             return typeName.substring(6);
673         }
674 
675         Matcher matcher = _fieldDescriptorPattern.matcher(typeName);
676 
677         while (matcher.find()) {
678             String dimensions = matcher.group(2);
679             String fieldDescriptor = matcher.group(1);
680 
681             if (Validator.isNull(dimensions)) {
682                 return fieldDescriptor;
683             }
684 
685             dimensions = dimensions.replace(
686                 StringPool.CLOSE_BRACKET, StringPool.BLANK);
687 
688             if (fieldDescriptor.equals("boolean")) {
689                 fieldDescriptor = "Z";
690             }
691             else if (fieldDescriptor.equals("byte")) {
692                 fieldDescriptor = "B";
693             }
694             else if (fieldDescriptor.equals("char")) {
695                 fieldDescriptor = "C";
696             }
697             else if (fieldDescriptor.equals("double")) {
698                 fieldDescriptor = "D";
699             }
700             else if (fieldDescriptor.equals("float")) {
701                 fieldDescriptor = "F";
702             }
703             else if (fieldDescriptor.equals("int")) {
704                 fieldDescriptor = "I";
705             }
706             else if (fieldDescriptor.equals("long")) {
707                 fieldDescriptor = "J";
708             }
709             else if (fieldDescriptor.equals("short")) {
710                 fieldDescriptor = "S";
711             }
712             else {
713                 fieldDescriptor = "L".concat(fieldDescriptor).concat(
714                     StringPool.SEMICOLON);
715             }
716 
717             return dimensions.concat(fieldDescriptor);
718         }
719 
720         throw new IllegalArgumentException(type.toString() + " is invalid");
721     }
722 
723     protected boolean isValidRequest(HttpServletRequest request) {
724         String className = ParamUtil.getString(request, "serviceClassName");
725 
726         if (className.contains(".service.") &&
727             className.endsWith("ServiceUtil") &&
728             !className.endsWith("LocalServiceUtil") &&
729             !_invalidClassNames.contains(className)) {
730 
731             return true;
732         }
733         else {
734             return false;
735         }
736     }
737 
738     protected JSONObject toJSONObject(AssetEntryDisplay assetEntryDisplay) {
739         JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
740 
741         jsonObject.put("entryId", assetEntryDisplay.getEntryId());
742         jsonObject.put("companyId", assetEntryDisplay.getCompanyId());
743         jsonObject.put("userId", assetEntryDisplay.getUserId());
744         jsonObject.put("userName", assetEntryDisplay.getUserName());
745         jsonObject.put("createDate", assetEntryDisplay.getCreateDate());
746         jsonObject.put("modifiedDate", assetEntryDisplay.getModifiedDate());
747         jsonObject.put("classNameId", assetEntryDisplay.getClassNameId());
748         jsonObject.put("className", assetEntryDisplay.getClassName());
749         jsonObject.put("classPK", assetEntryDisplay.getClassPK());
750         jsonObject.put("portletId", assetEntryDisplay.getPortletId());
751         jsonObject.put("portletTitle", assetEntryDisplay.getPortletTitle());
752         jsonObject.put("startDate", assetEntryDisplay.getStartDate());
753         jsonObject.put("endDate", assetEntryDisplay.getEndDate());
754         jsonObject.put("publishDate", assetEntryDisplay.getPublishDate());
755         jsonObject.put("expirationDate", assetEntryDisplay.getExpirationDate());
756         jsonObject.put("mimeType", assetEntryDisplay.getMimeType());
757         jsonObject.put("title", assetEntryDisplay.getTitle());
758         jsonObject.put("description", assetEntryDisplay.getDescription());
759         jsonObject.put("summary", assetEntryDisplay.getSummary());
760         jsonObject.put("url", assetEntryDisplay.getUrl());
761         jsonObject.put("height", assetEntryDisplay.getHeight());
762         jsonObject.put("width", assetEntryDisplay.getWidth());
763         jsonObject.put("priority", assetEntryDisplay.getPriority());
764         jsonObject.put("viewCount", assetEntryDisplay.getViewCount());
765         jsonObject.put(
766             "assetCategoryIds",
767             StringUtil.merge(assetEntryDisplay.getCategoryIds()));
768         jsonObject.put("assetTagNames", assetEntryDisplay.getTagNames());
769 
770         return jsonObject;
771     }
772 
773     protected JSONObject toJSONObject(AssetEntryType assetEntryType) {
774         JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
775 
776         jsonObject.put("classNameId", assetEntryType.getClassNameId());
777         jsonObject.put("className", assetEntryType.getClassName());
778         jsonObject.put("portletId", assetEntryType.getPortletId());
779         jsonObject.put("portletTitle", assetEntryType.getPortletTitle());
780 
781         return jsonObject;
782     }
783 
784     private static Log _log = LogFactoryUtil.getLog(JSONServiceAction.class);
785 
786     private static Pattern _fieldDescriptorPattern = Pattern.compile(
787         "^(.*?)((\\[\\])*)$", Pattern.DOTALL);
788 
789     private Set<String> _invalidClassNames = new HashSet<String>();
790     private Map<String, Object[]> _methodCache =
791         new HashMap<String, Object[]>();
792 
793 }