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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.PluginContextListener;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.auth.PrincipalThreadLocal;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
025    import com.liferay.portal.security.permission.PermissionThreadLocal;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.portal.util.ClassLoaderUtil;
028    import com.liferay.portal.util.PortalInstances;
029    
030    import java.io.IOException;
031    
032    import javax.servlet.ServletConfig;
033    import javax.servlet.ServletContext;
034    import javax.servlet.ServletException;
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class AxisServlet extends com.liferay.util.axis.AxisServlet {
042    
043            @Override
044            public void init(ServletConfig servletConfig) throws ServletException {
045                    ServletContext servletContext = servletConfig.getServletContext();
046    
047                    _pluginClassLoader = (ClassLoader)servletContext.getAttribute(
048                            PluginContextListener.PLUGIN_CLASS_LOADER);
049    
050                    if (_pluginClassLoader == null) {
051                            super.init(servletConfig);
052                    }
053                    else {
054                            ClassLoader contextClassLoader =
055                                    ClassLoaderUtil.getContextClassLoader();
056    
057                            try {
058                                    ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
059    
060                                    super.init(servletConfig);
061                            }
062                            finally {
063                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
064                            }
065                    }
066            }
067    
068            @Override
069            public void service(
070                            HttpServletRequest request, HttpServletResponse response)
071                    throws IOException, ServletException {
072    
073                    try {
074                            PortalInstances.getCompanyId(request);
075    
076                            String remoteUser = request.getRemoteUser();
077    
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug("Remote user " + remoteUser);
080                            }
081    
082                            if (remoteUser != null) {
083                                    PrincipalThreadLocal.setName(remoteUser);
084    
085                                    long userId = GetterUtil.getLong(remoteUser);
086    
087                                    User user = UserLocalServiceUtil.getUserById(userId);
088    
089                                    PermissionChecker permissionChecker =
090                                            PermissionCheckerFactoryUtil.create(user);
091    
092                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
093                            }
094    
095                            if (_pluginClassLoader == null) {
096                                    super.service(request, response);
097                            }
098                            else {
099                                    ClassLoader contextClassLoader =
100                                            ClassLoaderUtil.getContextClassLoader();
101    
102                                    try {
103                                            ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
104    
105                                            super.service(request, response);
106                                    }
107                                    finally {
108                                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
109                                    }
110                            }
111                    }
112                    catch (IOException ioe) {
113                            throw ioe;
114                    }
115                    catch (ServletException se) {
116                            throw se;
117                    }
118                    catch (Exception e) {
119                            throw new ServletException(e);
120                    }
121            }
122    
123            private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
124    
125            private ClassLoader _pluginClassLoader;
126    
127    }