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.action.JSONServiceAction;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.PortletServlet;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.security.auth.PrincipalThreadLocal;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
26  import com.liferay.portal.security.permission.PermissionThreadLocal;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  
29  import javax.servlet.ServletConfig;
30  import javax.servlet.ServletContext;
31  import javax.servlet.http.HttpServlet;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.struts.action.Action;
36  
37  /**
38   * <a href="JSONServlet.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class JSONServlet extends HttpServlet {
43  
44      public void init(ServletConfig servletConfig) {
45          ServletContext servletContext = servletConfig.getServletContext();
46  
47          _portletClassLoader = (ClassLoader)servletContext.getAttribute(
48              PortletServlet.PORTLET_CLASS_LOADER);
49  
50          _action = new JSONServiceAction();
51      }
52  
53      public void service(
54          HttpServletRequest request, HttpServletResponse response) {
55  
56          try {
57              String remoteUser = request.getRemoteUser();
58  
59              if (_log.isDebugEnabled()) {
60                  _log.debug("Remote user " + remoteUser);
61              }
62  
63              if (remoteUser != null) {
64                  PrincipalThreadLocal.setName(remoteUser);
65  
66                  long userId = GetterUtil.getLong(remoteUser);
67  
68                  User user = UserLocalServiceUtil.getUserById(userId);
69  
70                  PermissionChecker permissionChecker =
71                      PermissionCheckerFactoryUtil.create(user, true);
72  
73                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
74              }
75  
76              if (_portletClassLoader == null) {
77                  _action.execute(null, null, request, response);
78              }
79              else {
80                  Thread currentThread = Thread.currentThread();
81  
82                  ClassLoader contextClassLoader =
83                      currentThread.getContextClassLoader();
84  
85                  try {
86                      currentThread.setContextClassLoader(_portletClassLoader);
87  
88                      _action.execute(null, null, request, response);
89                  }
90                  finally {
91                      currentThread.setContextClassLoader(contextClassLoader);
92                  }
93              }
94          }
95          catch (Exception e) {
96              _log.error(e, e);
97          }
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
101 
102     private Action _action;
103     private ClassLoader _portletClassLoader;
104 
105 }