1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.action.JSONServiceAction;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.security.auth.PrincipalThreadLocal;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.security.permission.PermissionCheckerFactory;
31  import com.liferay.portal.security.permission.PermissionThreadLocal;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.Action;
39  
40  /**
41   * <a href="JSONServlet.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class JSONServlet extends HttpServlet {
47  
48      public void service(
49          HttpServletRequest request, HttpServletResponse response) {
50  
51          PermissionChecker permissionChecker = null;
52  
53          try {
54              String remoteUser = request.getRemoteUser();
55  
56              if (_log.isDebugEnabled()) {
57                  _log.debug("Remote user " + remoteUser);
58              }
59  
60              if (remoteUser != null) {
61                  PrincipalThreadLocal.setName(remoteUser);
62  
63                  long userId = GetterUtil.getLong(remoteUser);
64  
65                  User user = UserLocalServiceUtil.getUserById(userId);
66  
67                  permissionChecker = PermissionCheckerFactory.create(user, true);
68  
69                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
70              }
71  
72              Action action = (Action)InstancePool.get(
73                  JSONServiceAction.class.getName());
74  
75              action.execute(null, null, request, response);
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80          finally {
81              try {
82                  PermissionCheckerFactory.recycle(permissionChecker);
83              }
84              catch (Exception e) {
85              }
86          }
87      }
88  
89      private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
90  
91  }