001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.spring.servlet;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.model.User;
025: import com.liferay.portal.security.auth.CompanyThreadLocal;
026: import com.liferay.portal.security.auth.PrincipalThreadLocal;
027: import com.liferay.portal.security.permission.PermissionCheckerFactory;
028: import com.liferay.portal.security.permission.PermissionCheckerImpl;
029: import com.liferay.portal.security.permission.PermissionThreadLocal;
030: import com.liferay.portal.service.UserLocalServiceUtil;
031: import com.liferay.portal.util.PortalInstances;
032: import com.liferay.util.spring.context.LazyWebApplicationContext;
033:
034: import java.io.IOException;
035:
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: import org.springframework.web.servlet.DispatcherServlet;
044:
045: /**
046: * <a href="RemotingServlet.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class RemotingServlet extends DispatcherServlet {
052:
053: public static final String CONTEXT_CLASS = LazyWebApplicationContext.class
054: .getName();
055:
056: public static final String CONTEXT_CONFIG_LOCATION = "/WEB-INF/remoting-servlet.xml,/WEB-INF/remoting-servlet-ext.xml";
057:
058: public Class getContextClass() {
059: try {
060: return Class.forName(CONTEXT_CLASS);
061: } catch (Exception e) {
062: _log.error(e);
063: }
064:
065: return null;
066: }
067:
068: public String getContextConfigLocation() {
069: return CONTEXT_CONFIG_LOCATION;
070: }
071:
072: public void service(HttpServletRequest req, HttpServletResponse res)
073: throws IOException, ServletException {
074:
075: PermissionCheckerImpl permissionChecker = null;
076:
077: try {
078: String remoteUser = req.getRemoteUser();
079:
080: if (_log.isDebugEnabled()) {
081: _log.debug("Remote user " + remoteUser);
082: }
083:
084: long companyId = PortalInstances.getCompanyId(req);
085:
086: CompanyThreadLocal.setCompanyId(companyId);
087:
088: if (remoteUser != null) {
089: PrincipalThreadLocal.setName(remoteUser);
090:
091: long userId = GetterUtil.getLong(remoteUser);
092:
093: User user = UserLocalServiceUtil.getUserById(userId);
094:
095: permissionChecker = PermissionCheckerFactory.create(
096: user, true);
097:
098: PermissionThreadLocal
099: .setPermissionChecker(permissionChecker);
100: } else {
101: if (_log.isWarnEnabled()) {
102: _log
103: .warn("User id is not provided. An exception will be "
104: + "thrown if a protected method is accessed.");
105: }
106: }
107:
108: super .service(req, res);
109: } catch (Exception e) {
110: throw new ServletException(e);
111: } finally {
112: try {
113: PermissionCheckerFactory.recycle(permissionChecker);
114: } catch (Exception e) {
115: }
116: }
117: }
118:
119: private static Log _log = LogFactory.getLog(RemotingServlet.class);
120:
121: }
|