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.PermissionCheckerFactory;
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
62 public class TunnelServlet extends HttpServlet {
63
64 public void doPost(HttpServletRequest request, HttpServletResponse response)
65 throws IOException {
66
67 PermissionChecker permissionChecker = null;
68
69 try {
70 ObjectInputStream ois = new ObjectInputStream(
71 request.getInputStream());
72
73 Object returnObj = null;
74
75 try {
76 ObjectValuePair<HttpPrincipal, MethodWrapper> ovp =
77 (ObjectValuePair<HttpPrincipal, MethodWrapper>)
78 ois.readObject();
79
80 HttpPrincipal httpPrincipal = ovp.getKey();
81 MethodWrapper methodWrapper = ovp.getValue();
82
83 if (!isValidRequest(methodWrapper)) {
84 return;
85 }
86
87 long companyId = PortalInstances.getCompanyId(request);
88
89 CompanyThreadLocal.setCompanyId(companyId);
90
91 if (Validator.isNotNull(httpPrincipal.getLogin())) {
92 User user = null;
93
94 try {
95 user = UserLocalServiceUtil.getUserByEmailAddress(
96 companyId, httpPrincipal.getLogin());
97 }
98 catch (NoSuchUserException nsue) {
99 }
100
101 if (user == null) {
102 try {
103 user = UserLocalServiceUtil.getUserByScreenName(
104 companyId, httpPrincipal.getLogin());
105 }
106 catch (NoSuchUserException nsue) {
107 }
108 }
109
110 if (user == null) {
111 try {
112 user = UserLocalServiceUtil.getUserById(
113 GetterUtil.getLong(httpPrincipal.getLogin()));
114 }
115 catch (NoSuchUserException nsue) {
116 }
117 }
118
119 if (user != null) {
120 PrincipalThreadLocal.setName(user.getUserId());
121
122 permissionChecker = PermissionCheckerFactory.create(
123 user, true);
124
125 PermissionThreadLocal.setPermissionChecker(
126 permissionChecker);
127 }
128 }
129
130 if (returnObj == null) {
131 returnObj = MethodInvoker.invoke(methodWrapper);
132 }
133 }
134 catch (InvocationTargetException ite) {
135 returnObj = ite.getCause();
136
137 if (!(returnObj instanceof PortalException)) {
138 ite.printStackTrace();
139
140 returnObj = new SystemException();
141 }
142 }
143 catch (Exception e) {
144 _log.error(e, e);
145 }
146
147 if (returnObj != null) {
148 ObjectOutputStream oos = new ObjectOutputStream(
149 response.getOutputStream());
150
151 oos.writeObject(returnObj);
152
153 oos.flush();
154 oos.close();
155 }
156 }
157 finally {
158 try {
159 PermissionCheckerFactory.recycle(permissionChecker);
160 }
161 catch (Exception e) {
162 }
163 }
164 }
165
166 protected boolean isValidRequest(MethodWrapper methodWrapper) {
167 String className = methodWrapper.getClassName();
168
169 if (className.contains(".service.") &&
170 className.endsWith("ServiceUtil") &&
171 !className.endsWith("LocalServiceUtil")) {
172
173 return true;
174 }
175 else {
176 return false;
177 }
178 }
179
180 private static Log _log = LogFactoryUtil.getLog(TunnelServlet.class);
181
182 }