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.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.security.ac.AccessControlThreadLocal;
022    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
023    import com.liferay.portal.struts.JSONAction;
024    
025    import java.io.IOException;
026    
027    import javax.servlet.ServletConfig;
028    import javax.servlet.ServletContext;
029    import javax.servlet.ServletException;
030    import javax.servlet.http.HttpServlet;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class JSONServlet extends HttpServlet {
038    
039            @Override
040            public void init(ServletConfig servletConfig) {
041                    ServletContext servletContext = servletConfig.getServletContext();
042    
043                    _pluginClassLoader = (ClassLoader)servletContext.getAttribute(
044                            PluginContextListener.PLUGIN_CLASS_LOADER);
045    
046                    _jsonAction = getJSONAction(servletContext);
047            }
048    
049            @Override
050            @SuppressWarnings("unused")
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                                            PACLClassLoaderUtil.getContextClassLoader();
066    
067                                    try {
068                                            PACLClassLoaderUtil.setContextClassLoader(
069                                                    _pluginClassLoader);
070    
071                                            _jsonAction.execute(null, null, request, response);
072                                    }
073                                    finally {
074                                            PACLClassLoaderUtil.setContextClassLoader(
075                                                    contextClassLoader);
076                                    }
077                            }
078                    }
079                    catch (SecurityException se) {
080                            throw new ServletException(se);
081                    }
082                    catch (Exception e) {
083                            _log.error(e, e);
084                    }
085                    finally {
086                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
087                    }
088            }
089    
090            protected JSONAction getJSONAction(ServletContext servletContext) {
091                    JSONAction jsonAction = new JSONServiceAction();
092    
093                    jsonAction.setServletContext(servletContext);
094    
095                    return jsonAction;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
099    
100            private JSONAction _jsonAction;
101            private ClassLoader _pluginClassLoader;
102    
103    }