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.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            @Override
043            public void init(ServletConfig servletConfig) throws ServletException {
044                    ServletContext servletContext = servletConfig.getServletContext();
045    
046                    _portletClassLoader = (ClassLoader)servletContext.getAttribute(
047                            PortletServlet.PORTLET_CLASS_LOADER);
048    
049                    if (_portletClassLoader == null) {
050                            super.init(servletConfig);
051                    }
052                    else {
053                            Thread currentThread = Thread.currentThread();
054    
055                            ClassLoader contextClassLoader =
056                                    currentThread.getContextClassLoader();
057    
058                            try {
059                                    currentThread.setContextClassLoader(_portletClassLoader);
060    
061                                    super.init(servletConfig);
062                            }
063                            finally {
064                                    currentThread.setContextClassLoader(contextClassLoader);
065                            }
066                    }
067            }
068    
069            @Override
070            public void service(
071                            HttpServletRequest request, HttpServletResponse response)
072                    throws IOException, ServletException {
073    
074                    try {
075                            PortalInstances.getCompanyId(request);
076    
077                            String remoteUser = request.getRemoteUser();
078    
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug("Remote user " + remoteUser);
081                            }
082    
083                            if (remoteUser != null) {
084                                    PrincipalThreadLocal.setName(remoteUser);
085    
086                                    long userId = GetterUtil.getLong(remoteUser);
087    
088                                    User user = UserLocalServiceUtil.getUserById(userId);
089    
090                                    PermissionChecker permissionChecker =
091                                            PermissionCheckerFactoryUtil.create(user);
092    
093                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
094                            }
095    
096                            if (_portletClassLoader == null) {
097                                    super.service(request, response);
098                            }
099                            else {
100                                    Thread currentThread = Thread.currentThread();
101    
102                                    ClassLoader contextClassLoader =
103                                            currentThread.getContextClassLoader();
104    
105                                    try {
106                                            currentThread.setContextClassLoader(_portletClassLoader);
107    
108                                            super.service(request, response);
109                                    }
110                                    finally {
111                                            currentThread.setContextClassLoader(contextClassLoader);
112                                    }
113                            }
114                    }
115                    catch (IOException ioe) {
116                            throw ioe;
117                    }
118                    catch (ServletException se) {
119                            throw se;
120                    }
121                    catch (Exception e) {
122                            throw new ServletException(e);
123                    }
124            }
125    
126            private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
127    
128            private ClassLoader _portletClassLoader;
129    
130    }