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.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceAction;
020    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.struts.action.ActionForm;
029    import org.apache.struts.action.ActionMapping;
030    
031    /**
032     * @author Igor Spasic
033     */
034    public class JSONWebServiceServiceAction extends JSONServiceAction {
035    
036            public JSONWebServiceServiceAction(
037                    String servletContextPath, ClassLoader classLoader) {
038    
039                    _jsonWebServiceConfigurator = new JSONWebServiceConfigurator(
040                            servletContextPath);
041    
042                    _jsonWebServiceConfigurator.clean();
043    
044                    if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
045                            try {
046                                    _jsonWebServiceConfigurator.configure(classLoader);
047                            }
048                            catch (Exception e) {
049                                    _log.error(e, e);
050                            }
051                    }
052                    else {
053                            if (_log.isInfoEnabled()) {
054                                    _log.info("JSON web service is disabled");
055                            }
056                    }
057            }
058    
059            public void destroy() {
060                    _jsonWebServiceConfigurator.clean();
061            }
062    
063            @Override
064            public String getJSON(
065                            ActionMapping actionMapping, ActionForm actionForm,
066                            HttpServletRequest request, HttpServletResponse response)
067                    throws Exception {
068    
069                    try {
070                            JSONWebServiceAction jsonWebServiceAction =
071                                    JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
072                                            request);
073    
074                            Object returnObj = jsonWebServiceAction.invoke();
075    
076                            if (returnObj != null) {
077                                    return getReturnValue(returnObj);
078                            }
079                            else {
080                                    return JSONFactoryUtil.getNullJSON();
081                            }
082                    }
083                    catch (Exception e) {
084                            _log.error(e, e);
085    
086                            return JSONFactoryUtil.serializeException(e);
087                    }
088            }
089    
090            @Override
091            protected String getReroutePath() {
092                    return _REROUTE_PATH;
093            }
094    
095            private static final String _REROUTE_PATH = "/jsonws";
096    
097            private static Log _log = LogFactoryUtil.getLog(
098                    JSONWebServiceServiceAction.class);
099    
100            private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
101    
102    }