1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.PermissionCheckerFactoryUtil;
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          try {
46              String remoteUser = request.getRemoteUser();
47  
48              if (_log.isDebugEnabled()) {
49                  _log.debug("Remote user " + remoteUser);
50              }
51  
52              if (remoteUser != null) {
53                  PrincipalThreadLocal.setName(remoteUser);
54  
55                  long userId = GetterUtil.getLong(remoteUser);
56  
57                  User user = UserLocalServiceUtil.getUserById(userId);
58  
59                  PermissionChecker permissionChecker =
60                      PermissionCheckerFactoryUtil.create(user, true);
61  
62                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
63              }
64  
65              Action action = (Action)InstancePool.get(
66                  JSONServiceAction.class.getName());
67  
68              action.execute(null, null, request, response);
69          }
70          catch (Exception e) {
71              _log.error(e, e);
72          }
73      }
74  
75      private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
76  
77  }