001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.LocalizationUtil;
027    import com.liferay.portal.kernel.util.MethodHandler;
028    import com.liferay.portal.kernel.util.MethodKey;
029    import com.liferay.portal.kernel.util.ParamUtil;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.model.BaseModel;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.service.ServiceContextUtil;
036    import com.liferay.portal.struts.JSONAction;
037    import com.liferay.portal.util.PropsValues;
038    import com.liferay.portlet.asset.model.AssetEntryDisplay;
039    import com.liferay.portlet.asset.model.AssetEntryType;
040    
041    import java.lang.reflect.InvocationTargetException;
042    import java.lang.reflect.Method;
043    import java.lang.reflect.Type;
044    
045    import java.util.Arrays;
046    import java.util.Date;
047    import java.util.HashMap;
048    import java.util.HashSet;
049    import java.util.List;
050    import java.util.Map;
051    import java.util.Set;
052    import java.util.regex.Matcher;
053    import java.util.regex.Pattern;
054    
055    import javax.servlet.http.HttpServletRequest;
056    import javax.servlet.http.HttpServletResponse;
057    
058    import org.apache.struts.action.ActionForm;
059    import org.apache.struts.action.ActionMapping;
060    
061    /**
062     * @author Brian Wing Shun Chan
063     * @author Karthik Sudarshan
064     * @author Julio Camarero
065     * @author Eduardo Lundgren
066     */
067    public class JSONServiceAction extends JSONAction {
068    
069            public JSONServiceAction() {
070                    _invalidClassNames.addAll(
071                            ListUtil.fromArray(PropsValues.JSON_SERVICE_INVALID_CLASS_NAMES));
072    
073                    if (_log.isDebugEnabled()) {
074                            for (String invalidClassName : _invalidClassNames) {
075                                    _log.debug("Invalid class name " + invalidClassName);
076                            }
077                    }
078            }
079    
080            public String getJSON(
081                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
082                            HttpServletResponse response)
083                    throws Exception {
084    
085                    String className = ParamUtil.getString(request, "serviceClassName");
086                    String methodName = ParamUtil.getString(request, "serviceMethodName");
087                    String[] serviceParameters = getStringArrayFromJSON(
088                            request, "serviceParameters");
089                    String[] serviceParameterTypes = getStringArrayFromJSON(
090                            request, "serviceParameterTypes");
091    
092                    if (!isValidRequest(request)) {
093                            return null;
094                    }
095    
096                    Thread currentThread = Thread.currentThread();
097    
098                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
099    
100                    Class<?> classObj = contextClassLoader.loadClass(className);
101    
102                    Object[] methodAndParameterTypes = getMethodAndParameterTypes(
103                            classObj, methodName, serviceParameters, serviceParameterTypes);
104    
105                    if (methodAndParameterTypes != null) {
106                            Method method = (Method)methodAndParameterTypes[0];
107                            Type[] parameterTypes = (Type[])methodAndParameterTypes[1];
108                            Object[] args = new Object[serviceParameters.length];
109    
110                            for (int i = 0; i < serviceParameters.length; i++) {
111                                    args[i] = getArgValue(
112                                            request, classObj, methodName, serviceParameters[i],
113                                            parameterTypes[i]);
114                            }
115    
116                            try {
117                                    if (_log.isDebugEnabled()) {
118                                            _log.debug(
119                                                    "Invoking " + classObj + " on method " +
120                                                            method.getName() + " with args " +
121                                                                    Arrays.toString(args));
122                                    }
123    
124                                    Object returnObj = method.invoke(classObj, args);
125    
126                                    if (returnObj != null) {
127                                            return getReturnValue(returnObj, method.getReturnType());
128                                    }
129                                    else {
130                                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
131    
132                                            return jsonObject.toString();
133                                    }
134                            }
135                            catch (Exception e) {
136                                    if (_log.isDebugEnabled()) {
137                                            _log.debug(
138                                                    "Invoked " + classObj + " on method " +
139                                                            method.getName() + " with args " +
140                                                                    Arrays.toString(args),
141                                                    e);
142                                    }
143    
144                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
145    
146                                    if (e instanceof InvocationTargetException) {
147                                            jsonObject.put("exception", e.getCause().toString());
148                                    }
149                                    else {
150                                            jsonObject.put("exception", e.getMessage());
151                                    }
152    
153                                    return jsonObject.toString();
154                            }
155                    }
156    
157                    return null;
158            }
159    
160            protected Object getArgValue(
161                            HttpServletRequest request, Class<?> classObj, String methodName,
162                            String parameter, Type parameterType)
163                    throws Exception {
164    
165                    String typeNameOrClassDescriptor = getTypeNameOrClassDescriptor(
166                            parameterType);
167    
168                    String value = ParamUtil.getString(request, parameter);
169    
170                    if (Validator.isNull(value) &&
171                            !typeNameOrClassDescriptor.equals("[Ljava.lang.String;")) {
172    
173                            return null;
174                    }
175                    else if (typeNameOrClassDescriptor.equals("boolean") ||
176                                     typeNameOrClassDescriptor.equals(Boolean.class.getName())) {
177    
178                            return Boolean.valueOf(ParamUtil.getBoolean(request, parameter));
179                    }
180                    else if (typeNameOrClassDescriptor.equals("double") ||
181                                     typeNameOrClassDescriptor.equals(Double.class.getName())) {
182    
183                            return new Double(ParamUtil.getDouble(request, parameter));
184                    }
185                    else if (typeNameOrClassDescriptor.equals("int") ||
186                                     typeNameOrClassDescriptor.equals(Integer.class.getName())) {
187    
188                            return new Integer(ParamUtil.getInteger(request, parameter));
189                    }
190                    else if (typeNameOrClassDescriptor.equals("long") ||
191                                     typeNameOrClassDescriptor.equals(Long.class.getName())) {
192    
193                            return new Long(ParamUtil.getLong(request, parameter));
194                    }
195                    else if (typeNameOrClassDescriptor.equals("short") ||
196                                     typeNameOrClassDescriptor.equals(Short.class.getName())) {
197    
198                            return new Short(ParamUtil.getShort(request, parameter));
199                    }
200                    else if (typeNameOrClassDescriptor.equals(Date.class.getName())) {
201                            return new Date(ParamUtil.getLong(request, parameter));
202                    }
203                    else if (typeNameOrClassDescriptor.equals(
204                                            ServiceContext.class.getName())) {
205    
206                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
207    
208                            jsonObject.put("javaClass", ServiceContext.class.getName());
209    
210                            return ServiceContextUtil.deserialize(jsonObject);
211                    }
212                    else if (typeNameOrClassDescriptor.equals(String.class.getName())) {
213                            return value;
214                    }
215                    else if (typeNameOrClassDescriptor.equals("[Z")) {
216                            return ParamUtil.getBooleanValues(request, parameter);
217                    }
218                    else if (typeNameOrClassDescriptor.equals("[D")) {
219                            return ParamUtil.getDoubleValues(request, parameter);
220                    }
221                    else if (typeNameOrClassDescriptor.equals("[F")) {
222                            return ParamUtil.getFloatValues(request, parameter);
223                    }
224                    else if (typeNameOrClassDescriptor.equals("[I")) {
225                            return ParamUtil.getIntegerValues(request, parameter);
226                    }
227                    else if (typeNameOrClassDescriptor.equals("[J")) {
228                            return ParamUtil.getLongValues(request, parameter);
229                    }
230                    else if (typeNameOrClassDescriptor.equals("[S")) {
231                            return ParamUtil.getShortValues(request, parameter);
232                    }
233                    else if (typeNameOrClassDescriptor.equals("[Ljava.lang.String;")) {
234                            return StringUtil.split(value);
235                    }
236                    else if (typeNameOrClassDescriptor.equals("[[Z")) {
237                            String[] values = request.getParameterValues(parameter);
238    
239                            if ((values != null) && (values.length > 0)) {
240                                    String[] values0 = StringUtil.split(values[0]);
241    
242                                    boolean[][] doubleArray =
243                                            new boolean[values.length][values0.length];
244    
245                                    for (int i = 0; i < values.length; i++) {
246                                            String[] curValues = StringUtil.split(values[i]);
247    
248                                            for (int j = 0; j < curValues.length; j++) {
249                                                    doubleArray[i][j] = GetterUtil.getBoolean(curValues[j]);
250                                            }
251                                    }
252    
253                                    return doubleArray;
254                            }
255                            else {
256                                    return new boolean[0][0];
257                            }
258                    }
259                    else if (typeNameOrClassDescriptor.equals("[[D")) {
260                            String[] values = request.getParameterValues(parameter);
261    
262                            if ((values != null) && (values.length > 0)) {
263                                    String[] values0 = StringUtil.split(values[0]);
264    
265                                    double[][] doubleArray =
266                                            new double[values.length][values0.length];
267    
268                                    for (int i = 0; i < values.length; i++) {
269                                            String[] curValues = StringUtil.split(values[i]);
270    
271                                            for (int j = 0; j < curValues.length; j++) {
272                                                    doubleArray[i][j] = GetterUtil.getDouble(curValues[j]);
273                                            }
274                                    }
275    
276                                    return doubleArray;
277                            }
278                            else {
279                                    return new double[0][0];
280                            }
281                    }
282                    else if (typeNameOrClassDescriptor.equals("[[F")) {
283                            String[] values = request.getParameterValues(parameter);
284    
285                            if ((values != null) && (values.length > 0)) {
286                                    String[] values0 = StringUtil.split(values[0]);
287    
288                                    float[][] doubleArray =
289                                            new float[values.length][values0.length];
290    
291                                    for (int i = 0; i < values.length; i++) {
292                                            String[] curValues = StringUtil.split(values[i]);
293    
294                                            for (int j = 0; j < curValues.length; j++) {
295                                                    doubleArray[i][j] = GetterUtil.getFloat(curValues[j]);
296                                            }
297                                    }
298    
299                                    return doubleArray;
300                            }
301                            else {
302                                    return new float[0][0];
303                            }
304                    }
305                    else if (typeNameOrClassDescriptor.equals("[[I")) {
306                            String[] values = request.getParameterValues(parameter);
307    
308                            if ((values != null) && (values.length > 0)) {
309                                    String[] values0 = StringUtil.split(values[0]);
310    
311                                    int[][] doubleArray =
312                                            new int[values.length][values0.length];
313    
314                                    for (int i = 0; i < values.length; i++) {
315                                            String[] curValues = StringUtil.split(values[i]);
316    
317                                            for (int j = 0; j < curValues.length; j++) {
318                                                    doubleArray[i][j] = GetterUtil.getInteger(curValues[j]);
319                                            }
320                                    }
321    
322                                    return doubleArray;
323                            }
324                            else {
325                                    return new int[0][0];
326                            }
327                    }
328                    else if (typeNameOrClassDescriptor.equals("[[J")) {
329                            String[] values = request.getParameterValues(parameter);
330    
331                            if ((values != null) && (values.length > 0)) {
332                                    String[] values0 = StringUtil.split(values[0]);
333    
334                                    long[][] doubleArray =
335                                            new long[values.length][values0.length];
336    
337                                    for (int i = 0; i < values.length; i++) {
338                                            String[] curValues = StringUtil.split(values[i]);
339    
340                                            for (int j = 0; j < curValues.length; j++) {
341                                                    doubleArray[i][j] = GetterUtil.getLong(curValues[j]);
342                                            }
343                                    }
344    
345                                    return doubleArray;
346                            }
347                            else {
348                                    return new long[0][0];
349                            }
350                    }
351                    else if (typeNameOrClassDescriptor.equals("[[S")) {
352                            String[] values = request.getParameterValues(parameter);
353    
354                            if ((values != null) && (values.length > 0)) {
355                                    String[] values0 = StringUtil.split(values[0]);
356    
357                                    short[][] doubleArray =
358                                            new short[values.length][values0.length];
359    
360                                    for (int i = 0; i < values.length; i++) {
361                                            String[] curValues = StringUtil.split(values[i]);
362    
363                                            for (int j = 0; j < curValues.length; j++) {
364                                                    doubleArray[i][j] = GetterUtil.getShort(curValues[j]);
365                                            }
366                                    }
367    
368                                    return doubleArray;
369                            }
370                            else {
371                                    return new short[0][0];
372                            }
373                    }
374                    else if (typeNameOrClassDescriptor.equals("[[Ljava.lang.String")) {
375                            String[] values = request.getParameterValues(parameter);
376    
377                            if ((values != null) && (values.length > 0)) {
378                                    String[] values0 = StringUtil.split(values[0]);
379    
380                                    String[][] doubleArray =
381                                            new String[values.length][values0.length];
382    
383                                    for (int i = 0; i < values.length; i++) {
384                                            doubleArray[i] = StringUtil.split(values[i]);
385                                    }
386    
387                                    return doubleArray;
388                            }
389                            else {
390                                    return new String[0][0];
391                            }
392                    }
393                    else if (typeNameOrClassDescriptor.equals(
394                            "java.util.Map<java.util.Locale, java.lang.String>")) {
395    
396                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
397    
398                            return LocalizationUtil.deserialize(jsonObject);
399                    }
400                    else {
401                            _log.error(
402                                    "Unsupported parameter type for class " + classObj +
403                                            ", method " + methodName + ", parameter " + parameter +
404                                                    ", and type " + typeNameOrClassDescriptor);
405    
406                            return null;
407                    }
408            }
409    
410            protected Object[] getMethodAndParameterTypes(
411                            Class<?> classObj, String methodName, String[] parameters,
412                            String[] parameterTypes)
413                    throws Exception {
414    
415                    String parameterNames = StringUtil.merge(parameters);
416    
417                    String key =
418                            classObj.getName() + "_METHOD_NAME_" + methodName +
419                                    "_PARAMETERS_" + parameterNames;
420    
421                    Object[] methodAndParameterTypes = _methodCache.get(key);
422    
423                    if (methodAndParameterTypes != null) {
424                            return methodAndParameterTypes;
425                    }
426    
427                    Method method = null;
428                    Type[] methodParameterTypes = null;
429    
430                    Method[] methods = classObj.getMethods();
431    
432                    for (Method curMethod : methods) {
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 (AssetEntryDisplay assetEntryDisplay : assetEntryDisplays) {
507                            jsonArray.put(toJSONObject(assetEntryDisplay));
508                    }
509    
510                    return jsonArray.toString();
511            }
512    
513            protected String getReturnValue(AssetEntryType assetEntryType)
514                    throws Exception {
515    
516                    JSONObject jsonObject = toJSONObject(assetEntryType);
517    
518                    return jsonObject.toString();
519            }
520    
521            protected String getReturnValue(AssetEntryType[] assetEntryTypes)
522                    throws Exception {
523    
524                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
525    
526                    for (AssetEntryType assetEntryType : assetEntryTypes) {
527                            jsonArray.put(toJSONObject(assetEntryType));
528                    }
529    
530                    return jsonArray.toString();
531            }
532    
533            protected String getReturnValue(Object returnObj, Class<?> returnType)
534                    throws Exception {
535    
536                    if ((returnObj instanceof Boolean) || (returnObj instanceof Double) ||
537                            (returnObj instanceof Integer) || (returnObj instanceof Long) ||
538                            (returnObj instanceof Short) || (returnObj instanceof String)) {
539    
540                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
541    
542                            jsonObject.put("returnValue", returnObj.toString());
543    
544                            return jsonObject.toString();
545                    }
546                    else if (returnObj instanceof BaseModel<?>) {
547                            String serlializerClassName = getSerializerClassName(returnObj);
548    
549                            MethodKey methodKey = new MethodKey(
550                                    serlializerClassName, "toJSONObject", returnType);
551    
552                            MethodHandler methodHandler = new MethodHandler(
553                                    methodKey, returnObj);
554    
555                            JSONObject jsonObject = (JSONObject)methodHandler.invoke(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                                    MethodKey methodKey = new MethodKey(
571                                            serializerClassName, "toJSONArray", returnType);
572    
573                                    MethodHandler methodHandler = new MethodHandler(
574                                            methodKey, returnObj);
575    
576                                    jsonArray = (JSONArray)methodHandler.invoke(false);
577                            }
578    
579                            return jsonArray.toString();
580                    }
581                    else if (returnObj instanceof BaseModel<?>[][]) {
582                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
583    
584                            BaseModel<?>[][] returnArray = (BaseModel<?>[][])returnObj;
585    
586                            if ((returnArray.length > 0) &&
587                                    (returnArray[0].length > 0)) {
588    
589                                    BaseModel<?> returnItem0 = returnArray[0][0];
590    
591                                    String serializerClassName = getSerializerClassName(
592                                            returnItem0);
593    
594                                    MethodKey methodKey = new MethodKey(
595                                            serializerClassName, "toJSONArray", returnType);
596    
597                                    MethodHandler methodHandler = new MethodHandler(
598                                            methodKey, returnObj);
599    
600                                    jsonArray = (JSONArray)methodHandler.invoke(false);
601                            }
602    
603                            return jsonArray.toString();
604                    }
605                    else if (returnObj instanceof List<?>) {
606                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
607    
608                            List<Object> returnList = (List<Object>)returnObj;
609    
610                            if (!returnList.isEmpty()) {
611                                    Object returnItem0 = returnList.get(0);
612    
613                                    String serlializerClassName = getSerializerClassName(
614                                            returnItem0);
615    
616                                    MethodKey methodKey = new MethodKey(
617                                            serlializerClassName, "toJSONArray", returnType);
618    
619                                    MethodHandler methodHandler = new MethodHandler(
620                                            methodKey, returnObj);
621    
622                                    jsonArray = (JSONArray)methodHandler.invoke(false);
623                            }
624    
625                            return jsonArray.toString();
626                    }
627                    else if (returnObj instanceof JSONArray) {
628                            JSONArray jsonArray = (JSONArray)returnObj;
629    
630                            return jsonArray.toString();
631                    }
632                    else if (returnObj instanceof JSONObject) {
633                            JSONObject jsonObject = (JSONObject)returnObj;
634    
635                            return jsonObject.toString();
636                    }
637                    else if (returnObj instanceof AssetEntryDisplay) {
638                            return getReturnValue((AssetEntryDisplay)returnObj);
639                    }
640                    else if (returnObj instanceof AssetEntryDisplay[]) {
641                            return getReturnValue((AssetEntryDisplay[])returnObj);
642                    }
643                    else if (returnObj instanceof AssetEntryType) {
644                            return getReturnValue((AssetEntryType)returnObj);
645                    }
646                    else if (returnObj instanceof AssetEntryType[]) {
647                            return getReturnValue((AssetEntryType[])returnObj);
648                    }
649                    else {
650                            return JSONFactoryUtil.serialize(returnObj);
651                    }
652            }
653    
654            protected String getSerializerClassName(Object obj) {
655                    String serlializerClassName = StringUtil.replace(
656                            obj.getClass().getName(),
657                            new String[] {".model.impl.", "Impl"},
658                            new String[] {".service.http.", "JSONSerializer"});
659    
660                    return serlializerClassName;
661            }
662    
663            protected String[] getStringArrayFromJSON(
664                            HttpServletRequest request, String param)
665                    throws JSONException {
666    
667                    String json = ParamUtil.getString(request, param, "[]");
668    
669                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
670    
671                    return ArrayUtil.toStringArray(jsonArray);
672            }
673    
674            protected String getTypeNameOrClassDescriptor(Type type) {
675                    String typeName = type.toString();
676    
677                    if (typeName.contains("class ")) {
678                            return typeName.substring(6);
679                    }
680    
681                    Matcher matcher = _fieldDescriptorPattern.matcher(typeName);
682    
683                    while (matcher.find()) {
684                            String dimensions = matcher.group(2);
685                            String fieldDescriptor = matcher.group(1);
686    
687                            if (Validator.isNull(dimensions)) {
688                                    return fieldDescriptor;
689                            }
690    
691                            dimensions = dimensions.replace(
692                                    StringPool.CLOSE_BRACKET, StringPool.BLANK);
693    
694                            if (fieldDescriptor.equals("boolean")) {
695                                    fieldDescriptor = "Z";
696                            }
697                            else if (fieldDescriptor.equals("byte")) {
698                                    fieldDescriptor = "B";
699                            }
700                            else if (fieldDescriptor.equals("char")) {
701                                    fieldDescriptor = "C";
702                            }
703                            else if (fieldDescriptor.equals("double")) {
704                                    fieldDescriptor = "D";
705                            }
706                            else if (fieldDescriptor.equals("float")) {
707                                    fieldDescriptor = "F";
708                            }
709                            else if (fieldDescriptor.equals("int")) {
710                                    fieldDescriptor = "I";
711                            }
712                            else if (fieldDescriptor.equals("long")) {
713                                    fieldDescriptor = "J";
714                            }
715                            else if (fieldDescriptor.equals("short")) {
716                                    fieldDescriptor = "S";
717                            }
718                            else {
719                                    fieldDescriptor = "L".concat(fieldDescriptor).concat(
720                                            StringPool.SEMICOLON);
721                            }
722    
723                            return dimensions.concat(fieldDescriptor);
724                    }
725    
726                    throw new IllegalArgumentException(type.toString() + " is invalid");
727            }
728    
729            protected boolean isValidRequest(HttpServletRequest request) {
730                    String className = ParamUtil.getString(request, "serviceClassName");
731    
732                    if (className.contains(".service.") &&
733                            className.endsWith("ServiceUtil") &&
734                            !className.endsWith("LocalServiceUtil") &&
735                            !_invalidClassNames.contains(className)) {
736    
737                            return true;
738                    }
739                    else {
740                            return false;
741                    }
742            }
743    
744            protected JSONObject toJSONObject(AssetEntryDisplay assetEntryDisplay) {
745                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
746    
747                    jsonObject.put("entryId", assetEntryDisplay.getEntryId());
748                    jsonObject.put("companyId", assetEntryDisplay.getCompanyId());
749                    jsonObject.put("userId", assetEntryDisplay.getUserId());
750                    jsonObject.put("userName", assetEntryDisplay.getUserName());
751                    jsonObject.put("createDate", assetEntryDisplay.getCreateDate());
752                    jsonObject.put("modifiedDate", assetEntryDisplay.getModifiedDate());
753                    jsonObject.put("classNameId", assetEntryDisplay.getClassNameId());
754                    jsonObject.put("className", assetEntryDisplay.getClassName());
755                    jsonObject.put("classPK", assetEntryDisplay.getClassPK());
756                    jsonObject.put("portletId", assetEntryDisplay.getPortletId());
757                    jsonObject.put("portletTitle", assetEntryDisplay.getPortletTitle());
758                    jsonObject.put("startDate", assetEntryDisplay.getStartDate());
759                    jsonObject.put("endDate", assetEntryDisplay.getEndDate());
760                    jsonObject.put("publishDate", assetEntryDisplay.getPublishDate());
761                    jsonObject.put("expirationDate", assetEntryDisplay.getExpirationDate());
762                    jsonObject.put("mimeType", assetEntryDisplay.getMimeType());
763                    jsonObject.put("title", assetEntryDisplay.getTitle());
764                    jsonObject.put("description", assetEntryDisplay.getDescription());
765                    jsonObject.put("summary", assetEntryDisplay.getSummary());
766                    jsonObject.put("url", assetEntryDisplay.getUrl());
767                    jsonObject.put("height", assetEntryDisplay.getHeight());
768                    jsonObject.put("width", assetEntryDisplay.getWidth());
769                    jsonObject.put("priority", assetEntryDisplay.getPriority());
770                    jsonObject.put("viewCount", assetEntryDisplay.getViewCount());
771                    jsonObject.put(
772                            "assetCategoryIds",
773                            StringUtil.merge(assetEntryDisplay.getCategoryIds()));
774                    jsonObject.put("assetTagNames", assetEntryDisplay.getTagNames());
775    
776                    return jsonObject;
777            }
778    
779            protected JSONObject toJSONObject(AssetEntryType assetEntryType) {
780                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
781    
782                    jsonObject.put("classNameId", assetEntryType.getClassNameId());
783                    jsonObject.put("className", assetEntryType.getClassName());
784                    jsonObject.put("portletId", assetEntryType.getPortletId());
785                    jsonObject.put("portletTitle", assetEntryType.getPortletTitle());
786    
787                    return jsonObject;
788            }
789    
790            private static Log _log = LogFactoryUtil.getLog(JSONServiceAction.class);
791    
792            private static Pattern _fieldDescriptorPattern = Pattern.compile(
793                    "^(.*?)((\\[\\])*)$", Pattern.DOTALL);
794    
795            private Set<String> _invalidClassNames = new HashSet<String>();
796            private Map<String, Object[]> _methodCache =
797                    new HashMap<String, Object[]>();
798    
799    }