001    /**
002     * Copyright (c) 2000-2013 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.jsonwebservice;
016    
017    import com.liferay.portal.action.JSONServiceAction;
018    import com.liferay.portal.jsonwebservice.action.JSONWebServiceDiscoverAction;
019    import com.liferay.portal.jsonwebservice.action.JSONWebServiceInvokerAction;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceAction;
022    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.upload.UploadException;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.util.WebKeys;
028    
029    import java.lang.reflect.InvocationTargetException;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Igor Spasic
039     * @author Raymond Aug??
040     */
041    public class JSONWebServiceServiceAction extends JSONServiceAction {
042    
043            @Override
044            public String getJSON(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            HttpServletRequest request, HttpServletResponse response)
047                    throws Exception {
048    
049                    UploadException uploadException = (UploadException)request.getAttribute(
050                            WebKeys.UPLOAD_EXCEPTION);
051    
052                    if (uploadException != null) {
053                            return JSONFactoryUtil.serializeException(uploadException);
054                    }
055    
056                    JSONWebServiceAction jsonWebServiceAction = null;
057    
058                    try {
059                            jsonWebServiceAction = getJSONWebServiceAction(request);
060    
061                            Object returnObj = jsonWebServiceAction.invoke();
062    
063                            if (returnObj != null) {
064                                    return getReturnValue(returnObj);
065                            }
066                            else {
067                                    return JSONFactoryUtil.getNullJSON();
068                            }
069                    }
070                    catch (InvocationTargetException ite) {
071                            Throwable throwable = ite.getCause();
072    
073                            if (throwable instanceof SecurityException) {
074                                    throw (SecurityException)throwable;
075                            }
076    
077                            _log.error(throwable, throwable);
078    
079                            return JSONFactoryUtil.serializeThrowable(throwable);
080                    }
081                    catch (Exception e) {
082                            if (_log.isWarnEnabled()) {
083                                    _log.warn(e, e);
084                            }
085    
086                            return JSONFactoryUtil.serializeException(e);
087                    }
088            }
089    
090            protected JSONWebServiceAction getJSONWebServiceAction(
091                    HttpServletRequest request) {
092    
093                    String path = GetterUtil.getString(request.getPathInfo());
094    
095                    if (path.equals("/invoke")) {
096                            return new JSONWebServiceInvokerAction(request);
097                    }
098    
099                    if (request.getParameter("discover") != null) {
100                            return new JSONWebServiceDiscoverAction(request);
101                    }
102    
103                    return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
104                            request);
105            }
106    
107            @Override
108            protected String getReroutePath() {
109                    return _REROUTE_PATH;
110            }
111    
112            private static final String _REROUTE_PATH = "/jsonws";
113    
114            private static Log _log = LogFactoryUtil.getLog(
115                    JSONWebServiceServiceAction.class);
116    
117    }