001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PortletServlet;
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.PortalInstances;
028    
029    import java.io.IOException;
030    
031    import javax.servlet.ServletConfig;
032    import javax.servlet.ServletContext;
033    import javax.servlet.ServletException;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class AxisServlet extends com.liferay.util.axis.AxisServlet {
041    
042            public void init(ServletConfig servletConfig) throws ServletException {
043                    ServletContext servletContext = servletConfig.getServletContext();
044    
045                    _portletClassLoader = (ClassLoader)servletContext.getAttribute(
046                            PortletServlet.PORTLET_CLASS_LOADER);
047    
048                    if (_portletClassLoader == null) {
049                            super.init(servletConfig);
050                    }
051                    else {
052                            Thread currentThread = Thread.currentThread();
053    
054                            ClassLoader contextClassLoader =
055                                    currentThread.getContextClassLoader();
056    
057                            try {
058                                    currentThread.setContextClassLoader(_portletClassLoader);
059    
060                                    super.init(servletConfig);
061                            }
062                            finally {
063                                    currentThread.setContextClassLoader(contextClassLoader);
064                            }
065                    }
066            }
067    
068            public void service(
069                            HttpServletRequest request, HttpServletResponse response)
070                    throws IOException, ServletException {
071    
072                    try {
073                            PortalInstances.getCompanyId(request);
074    
075                            String remoteUser = request.getRemoteUser();
076    
077                            if (_log.isDebugEnabled()) {
078                                    _log.debug("Remote user " + remoteUser);
079                            }
080    
081                            if (remoteUser != null) {
082                                    PrincipalThreadLocal.setName(remoteUser);
083    
084                                    long userId = GetterUtil.getLong(remoteUser);
085    
086                                    User user = UserLocalServiceUtil.getUserById(userId);
087    
088                                    PermissionChecker permissionChecker =
089                                            PermissionCheckerFactoryUtil.create(user, true);
090    
091                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
092                            }
093    
094                            if (_portletClassLoader == null) {
095                                    super.service(request, response);
096                            }
097                            else {
098                                    Thread currentThread = Thread.currentThread();
099    
100                                    ClassLoader contextClassLoader =
101                                            currentThread.getContextClassLoader();
102    
103                                    try {
104                                            currentThread.setContextClassLoader(_portletClassLoader);
105    
106                                            super.service(request, response);
107                                    }
108                                    finally {
109                                            currentThread.setContextClassLoader(contextClassLoader);
110                                    }
111                            }
112                    }
113                    catch (IOException ioe) {
114                            throw ioe;
115                    }
116                    catch (ServletException se) {
117                            throw se;
118                    }
119                    catch (Exception e) {
120                            throw new ServletException(e);
121                    }
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
125    
126            private ClassLoader _portletClassLoader;
127    
128    }