1
22
23 package com.liferay.portal.servlet;
24
25 import com.liferay.portal.NoSuchUserException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.MethodInvoker;
32 import com.liferay.portal.kernel.util.MethodWrapper;
33 import com.liferay.portal.kernel.util.ObjectValuePair;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.model.User;
36 import com.liferay.portal.security.auth.CompanyThreadLocal;
37 import com.liferay.portal.security.auth.HttpPrincipal;
38 import com.liferay.portal.security.auth.PrincipalThreadLocal;
39 import com.liferay.portal.security.permission.PermissionChecker;
40 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
41 import com.liferay.portal.security.permission.PermissionThreadLocal;
42 import com.liferay.portal.service.UserLocalServiceUtil;
43 import com.liferay.portal.util.PortalInstances;
44
45 import java.io.IOException;
46 import java.io.ObjectInputStream;
47 import java.io.ObjectOutputStream;
48
49 import java.lang.reflect.InvocationTargetException;
50
51 import javax.servlet.http.HttpServlet;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55
61 public class TunnelServlet extends HttpServlet {
62
63 public void doPost(HttpServletRequest request, HttpServletResponse response)
64 throws IOException {
65
66 ObjectInputStream ois = new ObjectInputStream(
67 request.getInputStream());
68
69 Object returnObj = null;
70
71 try {
72 ObjectValuePair<HttpPrincipal, MethodWrapper> ovp =
73 (ObjectValuePair<HttpPrincipal, MethodWrapper>)
74 ois.readObject();
75
76 HttpPrincipal httpPrincipal = ovp.getKey();
77 MethodWrapper methodWrapper = ovp.getValue();
78
79 if (!isValidRequest(methodWrapper)) {
80 return;
81 }
82
83 long companyId = PortalInstances.getCompanyId(request);
84
85 CompanyThreadLocal.setCompanyId(companyId);
86
87 if (Validator.isNotNull(httpPrincipal.getLogin())) {
88 User user = null;
89
90 try {
91 user = UserLocalServiceUtil.getUserByEmailAddress(
92 companyId, httpPrincipal.getLogin());
93 }
94 catch (NoSuchUserException nsue) {
95 }
96
97 if (user == null) {
98 try {
99 user = UserLocalServiceUtil.getUserByScreenName(
100 companyId, httpPrincipal.getLogin());
101 }
102 catch (NoSuchUserException nsue) {
103 }
104 }
105
106 if (user == null) {
107 try {
108 user = UserLocalServiceUtil.getUserById(
109 GetterUtil.getLong(httpPrincipal.getLogin()));
110 }
111 catch (NoSuchUserException nsue) {
112 }
113 }
114
115 if (user != null) {
116 PrincipalThreadLocal.setName(user.getUserId());
117
118 PermissionChecker permissionChecker =
119 PermissionCheckerFactoryUtil.create(user, true);
120
121 PermissionThreadLocal.setPermissionChecker(
122 permissionChecker);
123 }
124 }
125
126 if (returnObj == null) {
127 returnObj = MethodInvoker.invoke(methodWrapper);
128 }
129 }
130 catch (InvocationTargetException ite) {
131 returnObj = ite.getCause();
132
133 if (!(returnObj instanceof PortalException)) {
134 ite.printStackTrace();
135
136 returnObj = new SystemException();
137 }
138 }
139 catch (Exception e) {
140 _log.error(e, e);
141 }
142
143 if (returnObj != null) {
144 ObjectOutputStream oos = new ObjectOutputStream(
145 response.getOutputStream());
146
147 oos.writeObject(returnObj);
148
149 oos.flush();
150 oos.close();
151 }
152 }
153
154 protected boolean isValidRequest(MethodWrapper methodWrapper) {
155 String className = methodWrapper.getClassName();
156
157 if (className.contains(".service.") &&
158 className.endsWith("ServiceUtil") &&
159 !className.endsWith("LocalServiceUtil")) {
160
161 return true;
162 }
163 else {
164 return false;
165 }
166 }
167
168 private static Log _log = LogFactoryUtil.getLog(TunnelServlet.class);
169
170 }