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.servlet;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.MethodInvoker;
026: import com.liferay.portal.kernel.util.MethodWrapper;
027: import com.liferay.portal.kernel.util.ObjectValuePair;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.security.auth.CompanyThreadLocal;
030: import com.liferay.portal.security.auth.HttpPrincipal;
031: import com.liferay.portal.security.auth.PrincipalThreadLocal;
032: import com.liferay.portal.security.permission.PermissionCheckerFactory;
033: import com.liferay.portal.security.permission.PermissionCheckerImpl;
034: import com.liferay.portal.security.permission.PermissionThreadLocal;
035: import com.liferay.portal.service.UserLocalServiceUtil;
036: import com.liferay.portal.util.PortalInstances;
037:
038: import java.io.IOException;
039: import java.io.ObjectInputStream;
040: import java.io.ObjectOutputStream;
041:
042: import java.lang.reflect.InvocationTargetException;
043:
044: import javax.servlet.ServletException;
045: import javax.servlet.http.HttpServlet;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048:
049: import org.apache.commons.logging.Log;
050: import org.apache.commons.logging.LogFactory;
051:
052: /**
053: * <a href="TunnelServlet.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Michael Weisser
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class TunnelServlet extends HttpServlet {
060:
061: public void doPost(HttpServletRequest req, HttpServletResponse res)
062: throws IOException, ServletException {
063:
064: PermissionCheckerImpl permissionChecker = null;
065:
066: try {
067: ObjectInputStream ois = new ObjectInputStream(req
068: .getInputStream());
069:
070: Object returnObj = null;
071:
072: try {
073: ObjectValuePair ovp = (ObjectValuePair) ois
074: .readObject();
075:
076: HttpPrincipal httpPrincipal = (HttpPrincipal) ovp
077: .getKey();
078: MethodWrapper methodWrapper = (MethodWrapper) ovp
079: .getValue();
080:
081: long companyId = PortalInstances.getCompanyId(req);
082:
083: CompanyThreadLocal.setCompanyId(companyId);
084:
085: if (httpPrincipal.getUserId() > 0) {
086: String name = String.valueOf(httpPrincipal
087: .getUserId());
088:
089: PrincipalThreadLocal.setName(name);
090:
091: User user = UserLocalServiceUtil
092: .getUserById(httpPrincipal.getUserId());
093:
094: permissionChecker = PermissionCheckerFactory
095: .create(user, true);
096:
097: PermissionThreadLocal
098: .setPermissionChecker(permissionChecker);
099: }
100:
101: if (returnObj == null) {
102: returnObj = MethodInvoker.invoke(methodWrapper);
103: }
104: } catch (InvocationTargetException ite) {
105: returnObj = ite.getCause();
106:
107: if (!(returnObj instanceof PortalException)) {
108: ite.printStackTrace();
109:
110: returnObj = new SystemException();
111: }
112: } catch (Exception e) {
113: _log.error(e, e);
114: }
115:
116: if (returnObj != null) {
117: ObjectOutputStream oos = new ObjectOutputStream(res
118: .getOutputStream());
119:
120: oos.writeObject(returnObj);
121:
122: oos.flush();
123: oos.close();
124: }
125: } finally {
126: try {
127: PermissionCheckerFactory.recycle(permissionChecker);
128: } catch (Exception e) {
129: }
130: }
131: }
132:
133: private static Log _log = LogFactory.getLog(TunnelServlet.class);
134:
135: }
|