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.jsonwebservice.JSONWebServiceConfigurator;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.ContextPathUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.ServiceLoader;
028    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.lang.reflect.InvocationTargetException;
033    
034    import java.util.List;
035    
036    import javax.servlet.ServletContext;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Igor Spasic
045     * @author Raymond Augé
046     */
047    public class JSONWebServiceServiceAction extends JSONServiceAction {
048    
049            public JSONWebServiceServiceAction(
050                    ServletContext servletContext, ClassLoader classLoader) {
051    
052                    String contextPath = ContextPathUtil.getContextPath(servletContext);
053    
054                    if ((classLoader == null) &&
055                            contextPath.equals(PortalUtil.getPathContext())) {
056    
057                            classLoader = PACLClassLoaderUtil.getPortalClassLoader();
058                    }
059    
060                    _jsonWebServiceConfigurator = getJSONWebServiceConfigurator(
061                            classLoader);
062    
063                    _jsonWebServiceConfigurator.init(servletContext, classLoader);
064    
065                    _jsonWebServiceConfigurator.clean();
066    
067                    if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
068                            try {
069                                    _jsonWebServiceConfigurator.configure();
070                            }
071                            catch (Exception e) {
072                                    _log.error(e, e);
073                            }
074                    }
075                    else {
076                            if (_log.isInfoEnabled()) {
077                                    _log.info("JSON web service is disabled");
078                            }
079                    }
080            }
081    
082            public void destroy() {
083                    _jsonWebServiceConfigurator.clean();
084            }
085    
086            @Override
087            public String getJSON(
088                            ActionMapping actionMapping, ActionForm actionForm,
089                            HttpServletRequest request, HttpServletResponse response)
090                    throws Exception {
091    
092                    JSONWebServiceAction jsonWebServiceAction = null;
093    
094                    String path = GetterUtil.getString(request.getPathInfo());
095    
096                    try {
097                            if (path.equals("/invoke")) {
098                                    jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
099                            }
100                            else {
101                                    jsonWebServiceAction =
102                                            JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
103                                                    request);
104                            }
105    
106                            Object returnObj = jsonWebServiceAction.invoke();
107    
108                            if (returnObj != null) {
109                                    return getReturnValue(returnObj);
110                            }
111                            else {
112                                    return JSONFactoryUtil.getNullJSON();
113                            }
114                    }
115                    catch (InvocationTargetException ite) {
116                            Throwable throwable = ite.getCause();
117    
118                            if (throwable instanceof SecurityException) {
119                                    throw (SecurityException)throwable;
120                            }
121    
122                            _log.error(throwable, throwable);
123    
124                            return JSONFactoryUtil.serializeThrowable(throwable);
125                    }
126                    catch (Exception e) {
127                            _log.error(e, e);
128    
129                            return JSONFactoryUtil.serializeException(e);
130                    }
131            }
132    
133            protected JSONWebServiceConfigurator getJSONWebServiceConfigurator(
134                    ClassLoader classLoader) {
135    
136                    JSONWebServiceConfigurator jsonWebServiceConfigurator = null;
137    
138                    try {
139                            List<JSONWebServiceConfigurator> jsonWebServiceConfigurators =
140                                    ServiceLoader.load(
141                                            classLoader, JSONWebServiceConfigurator.class);
142    
143                            if (!jsonWebServiceConfigurators.isEmpty()) {
144                                    jsonWebServiceConfigurator = jsonWebServiceConfigurators.get(0);
145                            }
146                    }
147                    catch (Exception e) {
148                            _log.error(e, e);
149                    }
150    
151                    if (jsonWebServiceConfigurator == null) {
152                            jsonWebServiceConfigurator = new JSONWebServiceConfiguratorImpl();
153                    }
154    
155                    return jsonWebServiceConfigurator;
156            }
157    
158            @Override
159            protected String getReroutePath() {
160                    return _REROUTE_PATH;
161            }
162    
163            private static final String _REROUTE_PATH = "/jsonws";
164    
165            private static Log _log = LogFactoryUtil.getLog(
166                    JSONWebServiceServiceAction.class);
167    
168            private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
169    
170    }