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.struts.JSONAction;
023    import com.liferay.portal.util.ClassLoaderUtil;
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                                            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 (SecurityException se) {
078                            throw new ServletException(se);
079                    }
080                    catch (Exception e) {
081                            _log.error(e, e);
082                    }
083                    finally {
084                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
085                    }
086            }
087    
088            protected JSONAction getJSONAction(ServletContext servletContext) {
089                    JSONAction jsonAction = new JSONServiceAction();
090    
091                    jsonAction.setServletContext(servletContext);
092    
093                    return jsonAction;
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
097    
098            private JSONAction _jsonAction;
099            private ClassLoader _pluginClassLoader;
100    
101    }