001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.action.JSONServiceAction;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.PortletServlet;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.auth.PrincipalThreadLocal;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
026    import com.liferay.portal.security.permission.PermissionThreadLocal;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    import javax.servlet.ServletConfig;
030    import javax.servlet.ServletContext;
031    import javax.servlet.http.HttpServlet;
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    import org.apache.struts.action.Action;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class JSONServlet extends HttpServlet {
041    
042            public void init(ServletConfig servletConfig) {
043                    ServletContext servletContext = servletConfig.getServletContext();
044    
045                    _portletClassLoader = (ClassLoader)servletContext.getAttribute(
046                            PortletServlet.PORTLET_CLASS_LOADER);
047    
048                    _action = new JSONServiceAction();
049            }
050    
051            public void service(
052                    HttpServletRequest request, HttpServletResponse response) {
053    
054                    try {
055                            String remoteUser = request.getRemoteUser();
056    
057                            if (_log.isDebugEnabled()) {
058                                    _log.debug("Remote user " + remoteUser);
059                            }
060    
061                            if (remoteUser != null) {
062                                    PrincipalThreadLocal.setName(remoteUser);
063    
064                                    long userId = GetterUtil.getLong(remoteUser);
065    
066                                    User user = UserLocalServiceUtil.getUserById(userId);
067    
068                                    PermissionChecker permissionChecker =
069                                            PermissionCheckerFactoryUtil.create(user, true);
070    
071                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
072                            }
073    
074                            if (_portletClassLoader == null) {
075                                    _action.execute(null, null, request, response);
076                            }
077                            else {
078                                    Thread currentThread = Thread.currentThread();
079    
080                                    ClassLoader contextClassLoader =
081                                            currentThread.getContextClassLoader();
082    
083                                    try {
084                                            currentThread.setContextClassLoader(_portletClassLoader);
085    
086                                            _action.execute(null, null, request, response);
087                                    }
088                                    finally {
089                                            currentThread.setContextClassLoader(contextClassLoader);
090                                    }
091                            }
092                    }
093                    catch (Exception e) {
094                            _log.error(e, e);
095                    }
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
099    
100            private Action _action;
101            private ClassLoader _portletClassLoader;
102    
103    }