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.ContextPathUtil;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.util.WebKeys;
029    
030    import java.lang.reflect.InvocationTargetException;
031    
032    import javax.servlet.ServletContext;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Igor Spasic
041     * @author Raymond Augé
042     */
043    public class JSONWebServiceServiceAction extends JSONServiceAction {
044    
045            public JSONWebServiceServiceAction(
046                    ServletContext servletContext, ClassLoader classLoader) {
047    
048                    _contextPath = ContextPathUtil.getContextPath(servletContext);
049    
050                    if (_log.isInfoEnabled()) {
051                            int count =
052                                    JSONWebServiceActionsManagerUtil.getJSONWebServiceActionsCount(
053                                            _contextPath);
054    
055                            _log.info("Configured " + count + " actions for " + _contextPath);
056                    }
057            }
058    
059            public void destroy() {
060                    JSONWebServiceActionsManagerUtil.unregisterJSONWebServiceActions(
061                            _contextPath);
062            }
063    
064            @Override
065            public String getJSON(
066                            ActionMapping actionMapping, ActionForm actionForm,
067                            HttpServletRequest request, HttpServletResponse response)
068                    throws Exception {
069    
070                    UploadException uploadException = (UploadException)request.getAttribute(
071                            WebKeys.UPLOAD_EXCEPTION);
072    
073                    if (uploadException != null) {
074                            return JSONFactoryUtil.serializeException(uploadException);
075                    }
076    
077                    JSONWebServiceAction jsonWebServiceAction = null;
078    
079                    try {
080                            jsonWebServiceAction = getJSONWebServiceAction(request);
081    
082                            Object returnObj = jsonWebServiceAction.invoke();
083    
084                            if (returnObj != null) {
085                                    return getReturnValue(returnObj);
086                            }
087                            else {
088                                    return JSONFactoryUtil.getNullJSON();
089                            }
090                    }
091                    catch (InvocationTargetException ite) {
092                            Throwable throwable = ite.getCause();
093    
094                            if (throwable instanceof SecurityException) {
095                                    throw (SecurityException)throwable;
096                            }
097    
098                            _log.error(throwable, throwable);
099    
100                            return JSONFactoryUtil.serializeThrowable(throwable);
101                    }
102                    catch (Exception e) {
103                            _log.error(e, e);
104    
105                            return JSONFactoryUtil.serializeException(e);
106                    }
107            }
108    
109            protected JSONWebServiceAction getJSONWebServiceAction(
110                    HttpServletRequest request) {
111    
112                    String path = GetterUtil.getString(request.getPathInfo());
113    
114                    if (path.equals("/invoke")) {
115                            return new JSONWebServiceInvokerAction(request);
116                    }
117    
118                    if (request.getParameter("discover") != null) {
119                            return new JSONWebServiceDiscoverAction(request);
120                    }
121    
122                    return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
123                            request);
124            }
125    
126            @Override
127            protected String getReroutePath() {
128                    return _REROUTE_PATH;
129            }
130    
131            private static final String _REROUTE_PATH = "/jsonws";
132    
133            private static Log _log = LogFactoryUtil.getLog(
134                    JSONWebServiceServiceAction.class);
135    
136            private String _contextPath;
137    
138    }