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.util.GetterUtil;
21  import com.liferay.portal.kernel.util.InstancePool;
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.PermissionCheckerFactory;
26  import com.liferay.portal.security.permission.PermissionThreadLocal;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.struts.action.Action;
34  
35  /**
36   * <a href="JSONServlet.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class JSONServlet extends HttpServlet {
41  
42      public void service(
43          HttpServletRequest request, HttpServletResponse response) {
44  
45          PermissionChecker permissionChecker = null;
46  
47          try {
48              String remoteUser = request.getRemoteUser();
49  
50              if (_log.isDebugEnabled()) {
51                  _log.debug("Remote user " + remoteUser);
52              }
53  
54              if (remoteUser != null) {
55                  PrincipalThreadLocal.setName(remoteUser);
56  
57                  long userId = GetterUtil.getLong(remoteUser);
58  
59                  User user = UserLocalServiceUtil.getUserById(userId);
60  
61                  permissionChecker = PermissionCheckerFactory.create(user, true);
62  
63                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
64              }
65  
66              Action action = (Action)InstancePool.get(
67                  JSONServiceAction.class.getName());
68  
69              action.execute(null, null, request, response);
70          }
71          catch (Exception e) {
72              _log.error(e, e);
73          }
74          finally {
75              try {
76                  PermissionCheckerFactory.recycle(permissionChecker);
77              }
78              catch (Exception e) {
79              }
80          }
81      }
82  
83      private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
84  
85  }