001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.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 (int i = 0; i < methods.length; i++) {
433                            Method curMethod = methods[i];
434    
435                            if (curMethod.getName().equals(methodName)) {
436                                    Type[] curParameterTypes = curMethod.getGenericParameterTypes();
437    
438                                    if (curParameterTypes.length == parameters.length) {
439                                            if ((parameterTypes.length > 0) &&
440                                                    (parameterTypes.length == curParameterTypes.length)) {
441    
442                                                    boolean match = true;
443    
444                                                    for (int j = 0; j < parameterTypes.length; j++) {
445                                                            String t1 = parameterTypes[j];
446                                                            String t2 = getTypeNameOrClassDescriptor(
447                                                                    curParameterTypes[j]);
448    
449                                                            if (!t1.equals(t2)) {
450                                                                    match = false;
451                                                            }
452                                                    }
453    
454                                                    if (match) {
455                                                            method = curMethod;
456                                                            methodParameterTypes = curParameterTypes;
457    
458                                                            break;
459                                                    }
460                                            }
461                                            else if (method != null) {
462                                                    _log.error(
463                                                            "Obscure method name for class " + classObj +
464                                                                    ", method " + methodName + ", and parameters " +
465                                                                            parameterNames);
466    
467                                                    return null;
468                                            }
469                                            else {
470                                                    method = curMethod;
471                                                    methodParameterTypes = curParameterTypes;
472                                            }
473                                    }
474                            }
475                    }
476    
477                    if (method != null) {
478                            methodAndParameterTypes = new Object[] {
479                                    method, methodParameterTypes
480                            };
481    
482                            _methodCache.put(key, methodAndParameterTypes);
483    
484                            return methodAndParameterTypes;
485                    }
486                    else {
487                            _log.error(
488                                    "No method found for class " + classObj + ", method " +
489                                            methodName + ", and parameters " + parameterNames);
490    
491                            return null;
492                    }
493            }
494    
495            protected String getReturnValue(AssetEntryDisplay assetEntryDisplay)
496                    throws Exception {
497    
498                    JSONObject jsonObject = toJSONObject(assetEntryDisplay);
499    
500                    return jsonObject.toString();
501            }
502    
503            protected String getReturnValue(AssetEntryDisplay[] assetEntryDisplays)
504                    throws Exception {
505    
506                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
507    
508                    for (int i = 0; i < assetEntryDisplays.length; i++) {
509                            AssetEntryDisplay assetEntryDisplay = assetEntryDisplays[i];
510    
511                            jsonArray.put(toJSONObject(assetEntryDisplay));
512                    }
513    
514                    return jsonArray.toString();
515            }
516    
517            protected String getReturnValue(AssetEntryType assetEntryType)
518                    throws Exception {
519    
520                    JSONObject jsonObject = toJSONObject(assetEntryType);
521    
522                    return jsonObject.toString();
523            }
524    
525            protected String getReturnValue(AssetEntryType[] assetEntryTypes)
526                    throws Exception {
527    
528                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
529    
530                    for (int i = 0; i < assetEntryTypes.length; i++) {
531                            AssetEntryType assetEntryType = assetEntryTypes[i];
532    
533                            jsonArray.put(toJSONObject(assetEntryType));
534                    }
535    
536                    return jsonArray.toString();
537            }
538    
539            protected String getReturnValue(Object returnObj, Class<?> returnType)
540                    throws Exception {
541    
542                    if ((returnObj instanceof Boolean) || (returnObj instanceof Double) ||
543                            (returnObj instanceof Integer) || (returnObj instanceof Long) ||
544                            (returnObj instanceof Short) || (returnObj instanceof String)) {
545    
546                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
547    
548                            jsonObject.put("returnValue", returnObj.toString());
549    
550                            return jsonObject.toString();
551                    }
552                    else if (returnObj instanceof BaseModel<?>) {
553                            String serlializerClassName = getSerializerClassName(returnObj);
554    
555                            MethodKey methodKey = new MethodKey(
556                                    serlializerClassName, "toJSONObject", returnType);
557    
558                            MethodHandler methodHandler = new MethodHandler(
559                                    methodKey, returnObj);
560    
561                            JSONObject jsonObject = (JSONObject)methodHandler.invoke(false);
562    
563                            return jsonObject.toString();
564                    }
565                    else if (returnObj instanceof BaseModel<?>[]) {
566                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
567    
568                            BaseModel<?>[] returnArray = (BaseModel[])returnObj;
569    
570                            if (returnArray.length > 0) {
571                                    BaseModel<?> returnItem0 = returnArray[0];
572    
573                                    String serializerClassName = getSerializerClassName(
574                                            returnItem0);
575    
576                                    MethodKey methodKey = new MethodKey(
577                                            serializerClassName, "toJSONArray", returnType);
578    
579                                    MethodHandler methodHandler = new MethodHandler(
580                                            methodKey, returnObj);
581    
582                                    jsonArray = (JSONArray)methodHandler.invoke(false);
583                            }
584    
585                            return jsonArray.toString();
586                    }
587                    else if (returnObj instanceof BaseModel<?>[][]) {
588                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
589    
590                            BaseModel<?>[][] returnArray = (BaseModel<?>[][])returnObj;
591    
592                            if ((returnArray.length > 0) &&
593                                    (returnArray[0].length > 0)) {
594    
595                                    BaseModel<?> returnItem0 = returnArray[0][0];
596    
597                                    String serializerClassName = getSerializerClassName(
598                                            returnItem0);
599    
600                                    MethodKey methodKey = new MethodKey(
601                                            serializerClassName, "toJSONArray", returnType);
602    
603                                    MethodHandler methodHandler = new MethodHandler(
604                                            methodKey, returnObj);
605    
606                                    jsonArray = (JSONArray)methodHandler.invoke(false);
607                            }
608    
609                            return jsonArray.toString();
610                    }
611                    else if (returnObj instanceof List<?>) {
612                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
613    
614                            List<Object> returnList = (List<Object>)returnObj;
615    
616                            if (!returnList.isEmpty()) {
617                                    Object returnItem0 = returnList.get(0);
618    
619                                    String serlializerClassName = getSerializerClassName(
620                                            returnItem0);
621    
622                                    MethodKey methodKey = new MethodKey(
623                                            serlializerClassName, "toJSONArray", returnType);
624    
625                                    MethodHandler methodHandler = new MethodHandler(
626                                            methodKey, returnObj);
627    
628                                    jsonArray = (JSONArray)methodHandler.invoke(false);
629                            }
630    
631                            return jsonArray.toString();
632                    }
633                    else if (returnObj instanceof JSONArray) {
634                            JSONArray jsonArray = (JSONArray)returnObj;
635    
636                            return jsonArray.toString();
637                    }
638                    else if (returnObj instanceof JSONObject) {
639                            JSONObject jsonObject = (JSONObject)returnObj;
640    
641                            return jsonObject.toString();
642                    }
643                    else if (returnObj instanceof AssetEntryDisplay) {
644                            return getReturnValue((AssetEntryDisplay)returnObj);
645                    }
646                    else if (returnObj instanceof AssetEntryDisplay[]) {
647                            return getReturnValue((AssetEntryDisplay[])returnObj);
648                    }
649                    else if (returnObj instanceof AssetEntryType) {
650                            return getReturnValue((AssetEntryType)returnObj);
651                    }
652                    else if (returnObj instanceof AssetEntryType[]) {
653                            return getReturnValue((AssetEntryType[])returnObj);
654                    }
655                    else {
656                            return JSONFactoryUtil.serialize(returnObj);
657                    }
658            }
659    
660            protected String getSerializerClassName(Object obj) {
661                    String serlializerClassName = StringUtil.replace(
662                            obj.getClass().getName(),
663                            new String[] {".model.impl.", "Impl"},
664                            new String[] {".service.http.", "JSONSerializer"});
665    
666                    return serlializerClassName;
667            }
668    
669            protected String[] getStringArrayFromJSON(
670                            HttpServletRequest request, String param)
671                    throws JSONException {
672    
673                    String json = ParamUtil.getString(request, param, "[]");
674    
675                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
676    
677                    return ArrayUtil.toStringArray(jsonArray);
678            }
679    
680            protected String getTypeNameOrClassDescriptor(Type type) {
681                    String typeName = type.toString();
682    
683                    if (typeName.contains("class ")) {
684                            return typeName.substring(6);
685                    }
686    
687                    Matcher matcher = _fieldDescriptorPattern.matcher(typeName);
688    
689                    while (matcher.find()) {
690                            String dimensions = matcher.group(2);
691                            String fieldDescriptor = matcher.group(1);
692    
693                            if (Validator.isNull(dimensions)) {
694                                    return fieldDescriptor;
695                            }
696    
697                            dimensions = dimensions.replace(
698                                    StringPool.CLOSE_BRACKET, StringPool.BLANK);
699    
700                            if (fieldDescriptor.equals("boolean")) {
701                                    fieldDescriptor = "Z";
702                            }
703                            else if (fieldDescriptor.equals("byte")) {
704                                    fieldDescriptor = "B";
705                            }
706                            else if (fieldDescriptor.equals("char")) {
707                                    fieldDescriptor = "C";
708                            }
709                            else if (fieldDescriptor.equals("double")) {
710                                    fieldDescriptor = "D";
711                            }
712                            else if (fieldDescriptor.equals("float")) {
713                                    fieldDescriptor = "F";
714                            }
715                            else if (fieldDescriptor.equals("int")) {
716                                    fieldDescriptor = "I";
717                            }
718                            else if (fieldDescriptor.equals("long")) {
719                                    fieldDescriptor = "J";
720                            }
721                            else if (fieldDescriptor.equals("short")) {
722                                    fieldDescriptor = "S";
723                            }
724                            else {
725                                    fieldDescriptor = "L".concat(fieldDescriptor).concat(
726                                            StringPool.SEMICOLON);
727                            }
728    
729                            return dimensions.concat(fieldDescriptor);
730                    }
731    
732                    throw new IllegalArgumentException(type.toString() + " is invalid");
733            }
734    
735            protected boolean isValidRequest(HttpServletRequest request) {
736                    String className = ParamUtil.getString(request, "serviceClassName");
737    
738                    if (className.contains(".service.") &&
739                            className.endsWith("ServiceUtil") &&
740                            !className.endsWith("LocalServiceUtil") &&
741                            !_invalidClassNames.contains(className)) {
742    
743                            return true;
744                    }
745                    else {
746                            return false;
747                    }
748            }
749    
750            protected JSONObject toJSONObject(AssetEntryDisplay assetEntryDisplay) {
751                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
752    
753                    jsonObject.put("entryId", assetEntryDisplay.getEntryId());
754                    jsonObject.put("companyId", assetEntryDisplay.getCompanyId());
755                    jsonObject.put("userId", assetEntryDisplay.getUserId());
756                    jsonObject.put("userName", assetEntryDisplay.getUserName());
757                    jsonObject.put("createDate", assetEntryDisplay.getCreateDate());
758                    jsonObject.put("modifiedDate", assetEntryDisplay.getModifiedDate());
759                    jsonObject.put("classNameId", assetEntryDisplay.getClassNameId());
760                    jsonObject.put("className", assetEntryDisplay.getClassName());
761                    jsonObject.put("classPK", assetEntryDisplay.getClassPK());
762                    jsonObject.put("portletId", assetEntryDisplay.getPortletId());
763                    jsonObject.put("portletTitle", assetEntryDisplay.getPortletTitle());
764                    jsonObject.put("startDate", assetEntryDisplay.getStartDate());
765                    jsonObject.put("endDate", assetEntryDisplay.getEndDate());
766                    jsonObject.put("publishDate", assetEntryDisplay.getPublishDate());
767                    jsonObject.put("expirationDate", assetEntryDisplay.getExpirationDate());
768                    jsonObject.put("mimeType", assetEntryDisplay.getMimeType());
769                    jsonObject.put("title", assetEntryDisplay.getTitle());
770                    jsonObject.put("description", assetEntryDisplay.getDescription());
771                    jsonObject.put("summary", assetEntryDisplay.getSummary());
772                    jsonObject.put("url", assetEntryDisplay.getUrl());
773                    jsonObject.put("height", assetEntryDisplay.getHeight());
774                    jsonObject.put("width", assetEntryDisplay.getWidth());
775                    jsonObject.put("priority", assetEntryDisplay.getPriority());
776                    jsonObject.put("viewCount", assetEntryDisplay.getViewCount());
777                    jsonObject.put(
778                            "assetCategoryIds",
779                            StringUtil.merge(assetEntryDisplay.getCategoryIds()));
780                    jsonObject.put("assetTagNames", assetEntryDisplay.getTagNames());
781    
782                    return jsonObject;
783            }
784    
785            protected JSONObject toJSONObject(AssetEntryType assetEntryType) {
786                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
787    
788                    jsonObject.put("classNameId", assetEntryType.getClassNameId());
789                    jsonObject.put("className", assetEntryType.getClassName());
790                    jsonObject.put("portletId", assetEntryType.getPortletId());
791                    jsonObject.put("portletTitle", assetEntryType.getPortletTitle());
792    
793                    return jsonObject;
794            }
795    
796            private static Log _log = LogFactoryUtil.getLog(JSONServiceAction.class);
797    
798            private static Pattern _fieldDescriptorPattern = Pattern.compile(
799                    "^(.*?)((\\[\\])*)$", Pattern.DOTALL);
800    
801            private Set<String> _invalidClassNames = new HashSet<String>();
802            private Map<String, Object[]> _methodCache =
803                    new HashMap<String, Object[]>();
804    
805    }