001    /**
002     * Copyright (c) 2000-2012 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.JSONWebServiceActionsManagerUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.lang.reflect.InvocationTargetException;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Igor Spasic
037     */
038    public class JSONWebServiceServiceAction extends JSONServiceAction {
039    
040            public JSONWebServiceServiceAction(
041                    String servletContextPath, ClassLoader classLoader) {
042    
043                    _jsonWebServiceConfigurator = new JSONWebServiceConfigurator(
044                            servletContextPath);
045    
046                    _jsonWebServiceConfigurator.clean();
047    
048                    if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
049                            try {
050                                    _jsonWebServiceConfigurator.configure(classLoader);
051                            }
052                            catch (Exception e) {
053                                    _log.error(e, e);
054                            }
055                    }
056                    else {
057                            if (_log.isInfoEnabled()) {
058                                    _log.info("JSON web service is disabled");
059                            }
060                    }
061            }
062    
063            public void destroy() {
064                    _jsonWebServiceConfigurator.clean();
065            }
066    
067            @Override
068            public String getJSON(
069                            ActionMapping actionMapping, ActionForm actionForm,
070                            HttpServletRequest request, HttpServletResponse response)
071                    throws Exception {
072    
073                    JSONWebServiceAction jsonWebServiceAction = null;
074    
075                    String path = GetterUtil.getString(request.getPathInfo());
076    
077                    try {
078                            if (path.equals("/invoke")) {
079                                    jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
080                            }
081                            else {
082                                    jsonWebServiceAction =
083                                            JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
084                                                    request);
085                            }
086    
087                            Object returnObj = jsonWebServiceAction.invoke();
088    
089                            if (returnObj != null) {
090                                    return getReturnValue(returnObj);
091                            }
092                            else {
093                                    return JSONFactoryUtil.getNullJSON();
094                            }
095                    }
096                    catch (InvocationTargetException ite) {
097                            Throwable cause = ite.getCause();
098    
099                            if (cause instanceof SecurityException) {
100                                    throw (SecurityException)cause;
101                            }
102    
103                            _log.error(cause, cause);
104    
105                            return JSONFactoryUtil.serializeThrowable(cause);
106                    }
107                    catch (Exception e) {
108                            _log.error(e, e);
109    
110                            return JSONFactoryUtil.serializeException(e);
111                    }
112            }
113    
114            @Override
115            protected String getReroutePath() {
116                    return _REROUTE_PATH;
117            }
118    
119            private static final String _REROUTE_PATH = "/jsonws";
120    
121            private static Log _log = LogFactoryUtil.getLog(
122                    JSONWebServiceServiceAction.class);
123    
124            private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
125    
126    }