1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
56   * <a href="TunnelServlet.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Michael Weisser
59   * @author Brian Wing Shun Chan
60   */
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 }