001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.MethodHandler;
022    import com.liferay.portal.kernel.util.MethodKey;
023    import com.liferay.portal.kernel.util.ObjectValuePair;
024    import com.liferay.portal.security.ac.AccessControlThreadLocal;
025    import com.liferay.portal.security.auth.HttpPrincipal;
026    
027    import java.io.IOException;
028    import java.io.ObjectInputStream;
029    import java.io.ObjectOutputStream;
030    
031    import java.lang.reflect.InvocationTargetException;
032    
033    import javax.servlet.http.HttpServlet;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Michael Weisser
039     * @author Brian Wing Shun Chan
040     */
041    public class TunnelServlet extends HttpServlet {
042    
043            @Override
044            public void doPost(HttpServletRequest request, HttpServletResponse response)
045                    throws IOException {
046    
047                    ObjectInputStream ois;
048    
049                    try {
050                            ois = new ObjectInputStream(request.getInputStream());
051                    }
052                    catch (IOException ioe) {
053                            if (_log.isWarnEnabled()) {
054                                    _log.warn(ioe, ioe);
055                            }
056    
057                            return;
058                    }
059    
060                    Object returnObj = null;
061    
062                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
063    
064                    try {
065                            AccessControlThreadLocal.setRemoteAccess(true);
066    
067                            ObjectValuePair<HttpPrincipal, MethodHandler> ovp =
068                                    (ObjectValuePair<HttpPrincipal, MethodHandler>)ois.readObject();
069    
070                            MethodHandler methodHandler = ovp.getValue();
071    
072                            if (methodHandler != null) {
073                                    MethodKey methodKey = methodHandler.getMethodKey();
074    
075                                    if (!isValidRequest(methodKey.getDeclaringClass())) {
076                                            return;
077                                    }
078    
079                                    returnObj = methodHandler.invoke(true);
080                            }
081                    }
082                    catch (InvocationTargetException ite) {
083                            returnObj = ite.getCause();
084    
085                            if (!(returnObj instanceof PortalException)) {
086                                    ite.printStackTrace();
087    
088                                    returnObj = new SystemException();
089                            }
090                    }
091                    catch (Exception e) {
092                            _log.error(e, e);
093                    }
094                    finally {
095                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
096                    }
097    
098                    if (returnObj != null) {
099                            try {
100                                    ObjectOutputStream oos = new ObjectOutputStream(
101                                            response.getOutputStream());
102    
103                                    oos.writeObject(returnObj);
104    
105                                    oos.flush();
106                                    oos.close();
107                            }
108                            catch (IOException ioe) {
109                                    _log.error(ioe, ioe);
110    
111                                    throw ioe;
112                            }
113                    }
114            }
115    
116            protected boolean isValidRequest(Class<?> clazz) {
117                    String className = clazz.getName();
118    
119                    if (className.contains(".service.") &&
120                            className.endsWith("ServiceUtil") &&
121                            !className.endsWith("LocalServiceUtil")) {
122    
123                            return true;
124                    }
125                    else {
126                            return false;
127                    }
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(TunnelServlet.class);
131    
132    }