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