1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchUserException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.MethodInvoker;
29  import com.liferay.portal.kernel.util.MethodWrapper;
30  import com.liferay.portal.kernel.util.ObjectValuePair;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.security.auth.CompanyThreadLocal;
34  import com.liferay.portal.security.auth.HttpPrincipal;
35  import com.liferay.portal.security.auth.PrincipalThreadLocal;
36  import com.liferay.portal.security.permission.PermissionChecker;
37  import com.liferay.portal.security.permission.PermissionCheckerFactory;
38  import com.liferay.portal.security.permission.PermissionThreadLocal;
39  import com.liferay.portal.service.UserLocalServiceUtil;
40  import com.liferay.portal.util.PortalInstances;
41  
42  import java.io.IOException;
43  import java.io.ObjectInputStream;
44  import java.io.ObjectOutputStream;
45  
46  import java.lang.reflect.InvocationTargetException;
47  
48  import javax.servlet.http.HttpServlet;
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * <a href="TunnelServlet.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Michael Weisser
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class TunnelServlet extends HttpServlet {
60  
61      public void doPost(HttpServletRequest request, HttpServletResponse response)
62          throws IOException {
63  
64          PermissionChecker permissionChecker = null;
65  
66          try {
67              ObjectInputStream ois = new ObjectInputStream(
68                  request.getInputStream());
69  
70              Object returnObj = null;
71  
72              try {
73                  ObjectValuePair<HttpPrincipal, MethodWrapper> ovp =
74                      (ObjectValuePair<HttpPrincipal, MethodWrapper>)
75                          ois.readObject();
76  
77                  HttpPrincipal httpPrincipal = ovp.getKey();
78                  MethodWrapper methodWrapper = ovp.getValue();
79  
80                  if (!isValidRequest(methodWrapper)) {
81                      return;
82                  }
83  
84                  long companyId = PortalInstances.getCompanyId(request);
85  
86                  CompanyThreadLocal.setCompanyId(companyId);
87  
88                  if (Validator.isNotNull(httpPrincipal.getLogin())) {
89                      User user = null;
90  
91                      try {
92                          user = UserLocalServiceUtil.getUserByEmailAddress(
93                              companyId, httpPrincipal.getLogin());
94                      }
95                      catch (NoSuchUserException nsue) {
96                      }
97  
98                      if (user == null) {
99                          try {
100                             user = UserLocalServiceUtil.getUserByScreenName(
101                                 companyId, httpPrincipal.getLogin());
102                         }
103                         catch (NoSuchUserException nsue) {
104                         }
105                     }
106 
107                     if (user == null) {
108                         try {
109                             user = UserLocalServiceUtil.getUserById(
110                                 GetterUtil.getLong(httpPrincipal.getLogin()));
111                         }
112                         catch (NoSuchUserException nsue) {
113                         }
114                     }
115 
116                     if (user != null) {
117                         PrincipalThreadLocal.setName(user.getUserId());
118 
119                         permissionChecker = PermissionCheckerFactory.create(
120                             user, true);
121 
122                         PermissionThreadLocal.setPermissionChecker(
123                             permissionChecker);
124                     }
125                 }
126 
127                 if (returnObj == null) {
128                     returnObj = MethodInvoker.invoke(methodWrapper);
129                 }
130             }
131             catch (InvocationTargetException ite) {
132                 returnObj = ite.getCause();
133 
134                 if (!(returnObj instanceof PortalException)) {
135                     ite.printStackTrace();
136 
137                     returnObj = new SystemException();
138                 }
139             }
140             catch (Exception e) {
141                 _log.error(e, e);
142             }
143 
144             if (returnObj != null) {
145                 ObjectOutputStream oos = new ObjectOutputStream(
146                     response.getOutputStream());
147 
148                 oos.writeObject(returnObj);
149 
150                 oos.flush();
151                 oos.close();
152             }
153         }
154         finally {
155             try {
156                 PermissionCheckerFactory.recycle(permissionChecker);
157             }
158             catch (Exception e) {
159             }
160         }
161     }
162 
163     protected boolean isValidRequest(MethodWrapper methodWrapper) {
164         String className = methodWrapper.getClassName();
165 
166         if (className.contains(".service.") &&
167             className.endsWith("ServiceUtil") &&
168             !className.endsWith("LocalServiceUtil")) {
169 
170             return true;
171         }
172         else {
173             return false;
174         }
175     }
176 
177     private static Log _log = LogFactoryUtil.getLog(TunnelServlet.class);
178 
179 }