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