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