001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.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.util.GetterUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.lang.reflect.Method;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Igor Spasic
038     */
039    public class JSONWebServiceServiceAction extends JSONServiceAction {
040    
041            public JSONWebServiceServiceAction(
042                    String servletContextPath, ClassLoader classLoader) {
043    
044                    _jsonWebServiceConfigurator = new JSONWebServiceConfigurator(
045                            servletContextPath);
046    
047                    _jsonWebServiceConfigurator.clean();
048    
049                    if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
050                            try {
051                                    _jsonWebServiceConfigurator.configure(classLoader);
052                            }
053                            catch (Exception e) {
054                                    _log.error(e, e);
055                            }
056                    }
057                    else {
058                            if (_log.isInfoEnabled()) {
059                                    _log.info("JSON web service is disabled");
060                            }
061                    }
062            }
063    
064            public void destroy() {
065                    _jsonWebServiceConfigurator.clean();
066            }
067    
068            @Override
069            public String getJSON(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            HttpServletRequest request, HttpServletResponse response)
072                    throws Exception {
073    
074                    JSONWebServiceAction jsonWebServiceAction = null;
075    
076                    String path = GetterUtil.getString(request.getPathInfo());
077    
078                    try {
079                            if (path.equals("/invoke")) {
080                                    jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
081                            }
082                            else {
083                                    jsonWebServiceAction =
084                                            JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
085                                                    request);
086                            }
087    
088                            JSONWebServiceActionMapping jsonWebServiceActionMapping =
089                                    jsonWebServiceAction.getJSONWebServiceActionMapping();
090    
091                            String actionMethodName = null;
092    
093                            if (jsonWebServiceActionMapping != null) {
094                                    Method actionMethod =
095                                            jsonWebServiceActionMapping.getActionMethod();
096    
097                                    actionMethodName = actionMethod.getName();
098                            }
099    
100                            checkMethodGuestAccess(
101                                    request, actionMethodName,
102                                    PropsValues.JSONWS_WEB_SERVICE_PUBLIC_METHODS);
103    
104                            Object returnObj = jsonWebServiceAction.invoke();
105    
106                            if (returnObj != null) {
107                                    return getReturnValue(returnObj);
108                            }
109                            else {
110                                    return JSONFactoryUtil.getNullJSON();
111                            }
112                    }
113                    catch (Exception e) {
114                            _log.error(e, e);
115    
116                            return JSONFactoryUtil.serializeException(e);
117                    }
118            }
119    
120            @Override
121            protected String getReroutePath() {
122                    return _REROUTE_PATH;
123            }
124    
125            private static final String _REROUTE_PATH = "/jsonws";
126    
127            private static Log _log = LogFactoryUtil.getLog(
128                    JSONWebServiceServiceAction.class);
129    
130            private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
131    
132    }