1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.PortletServlet;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.security.auth.PrincipalThreadLocal;
23  import com.liferay.portal.security.permission.PermissionChecker;
24  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
25  import com.liferay.portal.security.permission.PermissionThreadLocal;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  import com.liferay.portal.util.PortalInstances;
28  
29  import java.io.IOException;
30  
31  import javax.servlet.ServletConfig;
32  import javax.servlet.ServletContext;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class AxisServlet extends com.liferay.util.axis.AxisServlet {
43  
44      public void init(ServletConfig servletConfig) throws ServletException {
45          ServletContext servletContext = servletConfig.getServletContext();
46  
47          _portletClassLoader = (ClassLoader)servletContext.getAttribute(
48              PortletServlet.PORTLET_CLASS_LOADER);
49  
50          if (_portletClassLoader == null) {
51              super.init(servletConfig);
52          }
53          else {
54              Thread currentThread = Thread.currentThread();
55  
56              ClassLoader contextClassLoader =
57                  currentThread.getContextClassLoader();
58  
59              try {
60                  currentThread.setContextClassLoader(_portletClassLoader);
61  
62                  super.init(servletConfig);
63              }
64              finally {
65                  currentThread.setContextClassLoader(contextClassLoader);
66              }
67          }
68      }
69  
70      public void service(
71              HttpServletRequest request, HttpServletResponse response)
72          throws IOException, ServletException {
73  
74          try {
75              PortalInstances.getCompanyId(request);
76  
77              String remoteUser = request.getRemoteUser();
78  
79              if (_log.isDebugEnabled()) {
80                  _log.debug("Remote user " + remoteUser);
81              }
82  
83              if (remoteUser != null) {
84                  PrincipalThreadLocal.setName(remoteUser);
85  
86                  long userId = GetterUtil.getLong(remoteUser);
87  
88                  User user = UserLocalServiceUtil.getUserById(userId);
89  
90                  PermissionChecker permissionChecker =
91                      PermissionCheckerFactoryUtil.create(user, true);
92  
93                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
94              }
95  
96              if (_portletClassLoader == null) {
97                  super.service(request, response);
98              }
99              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 }