001    /**
002     * Copyright (c) 2000-present 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.servlet;
016    
017    import com.liferay.portal.action.JSONServiceAction;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.security.access.control.AccessControlThreadLocal;
021    import com.liferay.portal.kernel.servlet.PluginContextListener;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.util.ClassLoaderUtil;
024    import com.liferay.portal.struts.JSONAction;
025    
026    import java.io.IOException;
027    
028    import javax.servlet.ServletConfig;
029    import javax.servlet.ServletContext;
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServlet;
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class JSONServlet extends HttpServlet {
039    
040            @Override
041            public void init(ServletConfig servletConfig) throws ServletException {
042                    super.init(servletConfig);
043    
044                    ServletContext servletContext = servletConfig.getServletContext();
045    
046                    _pluginClassLoader = (ClassLoader)servletContext.getAttribute(
047                            PluginContextListener.PLUGIN_CLASS_LOADER);
048    
049                    _jsonAction = getJSONAction(servletContext);
050            }
051    
052            @Override
053            public void service(
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws IOException, ServletException {
056    
057                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
058    
059                    try {
060                            AccessControlThreadLocal.setRemoteAccess(true);
061    
062                            if (_pluginClassLoader == null) {
063                                    _jsonAction.execute(null, null, request, response);
064                            }
065                            else {
066                                    ClassLoader contextClassLoader =
067                                            ClassLoaderUtil.getContextClassLoader();
068    
069                                    try {
070                                            ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
071    
072                                            _jsonAction.execute(null, null, request, response);
073                                    }
074                                    finally {
075                                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
076                                    }
077                            }
078                    }
079                    catch (IOException ioe) {
080                            if (!ServletResponseUtil.isClientAbortException(ioe)) {
081                                    throw ioe;
082                            }
083                    }
084                    catch (SecurityException se) {
085                            throw new ServletException(se);
086                    }
087                    catch (Exception e) {
088                            _log.error(e, e);
089                    }
090                    finally {
091                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
092                    }
093            }
094    
095            protected JSONAction getJSONAction(ServletContext servletContext) {
096                    JSONAction jsonAction = new JSONServiceAction();
097    
098                    jsonAction.setServletContext(servletContext);
099    
100                    return jsonAction;
101            }
102    
103            private static final Log _log = LogFactoryUtil.getLog(JSONServlet.class);
104    
105            private JSONAction _jsonAction;
106            private ClassLoader _pluginClassLoader;
107    
108    }