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.JSONWebServiceInvokerAction;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceAction;
021    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionMapping;
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.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    
030    import java.lang.reflect.Method;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Igor Spasic
040     */
041    public class JSONWebServiceServiceAction extends JSONServiceAction {
042    
043            public JSONWebServiceServiceAction(
044                    String servletContextPath, ClassLoader classLoader) {
045    
046                    _jsonWebServiceConfigurator = new JSONWebServiceConfigurator(
047                            servletContextPath);
048    
049                    _jsonWebServiceConfigurator.clean();
050    
051                    if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
052                            try {
053                                    _jsonWebServiceConfigurator.configure(classLoader);
054                            }
055                            catch (Exception e) {
056                                    _log.error(e, e);
057                            }
058                    }
059                    else {
060                            if (_log.isInfoEnabled()) {
061                                    _log.info("JSON web service is disabled");
062                            }
063                    }
064            }
065    
066            public void destroy() {
067                    _jsonWebServiceConfigurator.clean();
068            }
069    
070            @Override
071            public String getJSON(
072                            ActionMapping actionMapping, ActionForm actionForm,
073                            HttpServletRequest request, HttpServletResponse response)
074                    throws Exception {
075    
076                    UploadException uploadException = (UploadException)request.getAttribute(
077                            WebKeys.UPLOAD_EXCEPTION);
078    
079                    if (uploadException != null) {
080                            return JSONFactoryUtil.serializeException(uploadException);
081                    }
082    
083                    String path = GetterUtil.getString(request.getPathInfo());
084    
085                    try {
086                            JSONWebServiceAction jsonWebServiceAction = null;
087    
088                            if (path.equals("/invoke")) {
089                                    jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
090                            }
091                            else {
092                                    jsonWebServiceAction =
093                                            JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
094                                                    request);
095                            }
096    
097                            JSONWebServiceActionMapping jsonWebServiceActionMapping =
098                                    jsonWebServiceAction.getJSONWebServiceActionMapping();
099    
100                            String actionMethodName = null;
101    
102                            if (jsonWebServiceActionMapping != null) {
103                                    Method actionMethod =
104                                            jsonWebServiceActionMapping.getActionMethod();
105    
106                                    actionMethodName = actionMethod.getName();
107                            }
108    
109                            checkMethodGuestAccess(
110                                    request, actionMethodName,
111                                    PropsValues.JSONWS_WEB_SERVICE_PUBLIC_METHODS);
112    
113                            Object returnObj = jsonWebServiceAction.invoke();
114    
115                            if (returnObj != null) {
116                                    return getReturnValue(returnObj);
117                            }
118                            else {
119                                    return JSONFactoryUtil.getNullJSON();
120                            }
121                    }
122                    catch (Exception e) {
123                            if (_log.isWarnEnabled()) {
124                                    _log.warn(e, e);
125                            }
126    
127                            return JSONFactoryUtil.serializeException(e);
128                    }
129            }
130    
131            @Override
132            protected String getReroutePath() {
133                    return _REROUTE_PATH;
134            }
135    
136            private static final String _REROUTE_PATH = "/jsonws";
137    
138            private static Log _log = LogFactoryUtil.getLog(
139                    JSONWebServiceServiceAction.class);
140    
141            private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
142    
143    }