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.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.servlet.PluginContextListener;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.security.ac.AccessControlThreadLocal;
023    import com.liferay.portal.struts.JSONAction;
024    import com.liferay.portal.util.ClassLoaderUtil;
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) {
042                    ServletContext servletContext = servletConfig.getServletContext();
043    
044                    _pluginClassLoader = (ClassLoader)servletContext.getAttribute(
045                            PluginContextListener.PLUGIN_CLASS_LOADER);
046    
047                    _jsonAction = getJSONAction(servletContext);
048            }
049    
050            @Override
051            public void service(
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws IOException, ServletException {
054    
055                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
056    
057                    try {
058                            AccessControlThreadLocal.setRemoteAccess(true);
059    
060                            if (_pluginClassLoader == null) {
061                                    _jsonAction.execute(null, null, request, response);
062                            }
063                            else {
064                                    ClassLoader contextClassLoader =
065                                            ClassLoaderUtil.getContextClassLoader();
066    
067                                    try {
068                                            ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
069    
070                                            _jsonAction.execute(null, null, request, response);
071                                    }
072                                    finally {
073                                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
074                                    }
075                            }
076                    }
077                    catch (IOException ioe) {
078                            if (!ServletResponseUtil.isClientAbortException(ioe)) {
079                                    throw ioe;
080                            }
081                    }
082                    catch (SecurityException se) {
083                            throw new ServletException(se);
084                    }
085                    catch (Exception e) {
086                            _log.error(e, e);
087                    }
088                    finally {
089                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
090                    }
091            }
092    
093            protected JSONAction getJSONAction(ServletContext servletContext) {
094                    JSONAction jsonAction = new JSONServiceAction();
095    
096                    jsonAction.setServletContext(servletContext);
097    
098                    return jsonAction;
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
102    
103            private JSONAction _jsonAction;
104            private ClassLoader _pluginClassLoader;
105    
106    }