001    /**
002     * Copyright (c) 2000-2010 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.service.http;
016    
017    import com.liferay.portal.kernel.servlet.HttpHeaders;
018    import com.liferay.portal.kernel.util.Base64;
019    import com.liferay.portal.kernel.util.MethodWrapper;
020    import com.liferay.portal.kernel.util.ObjectValuePair;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.HttpPrincipal;
024    import com.liferay.portal.security.auth.PrincipalException;
025    
026    import java.io.EOFException;
027    import java.io.IOException;
028    import java.io.ObjectInputStream;
029    import java.io.ObjectOutputStream;
030    
031    import java.net.HttpURLConnection;
032    import java.net.URL;
033    
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class TunnelUtil {
040    
041            public static Object invoke(
042                            HttpPrincipal httpPrincipal, MethodWrapper methodWrapper)
043                    throws Exception {
044    
045                    HttpURLConnection urlc = _getConnection(httpPrincipal);
046    
047                    ObjectOutputStream oos = new ObjectOutputStream(urlc.getOutputStream());
048    
049                    oos.writeObject(
050                            new ObjectValuePair<HttpPrincipal, MethodWrapper>(
051                                    httpPrincipal, methodWrapper));
052    
053                    oos.flush();
054                    oos.close();
055    
056                    Object returnObj = null;
057    
058                    try {
059                            ObjectInputStream ois = new ObjectInputStream(
060                                    urlc.getInputStream());
061    
062                            returnObj = ois.readObject();
063    
064                            ois.close();
065                    }
066                    catch (EOFException eofe) {
067                    }
068                    catch (IOException ioe) {
069                            String ioeMessage = ioe.getMessage();
070    
071                            if ((ioeMessage != null) &&
072                                            (ioeMessage.indexOf("HTTP response code: 401") != -1)) {
073    
074                                    throw new PrincipalException(ioeMessage);
075                            }
076                            else {
077                                    throw ioe;
078                            }
079                    }
080    
081                    if ((returnObj != null) && returnObj instanceof Exception) {
082                            throw (Exception)returnObj;
083                    }
084    
085                    return returnObj;
086            }
087    
088            private static HttpURLConnection _getConnection(HttpPrincipal httpPrincipal)
089                    throws IOException {
090    
091                    if (httpPrincipal == null || httpPrincipal.getUrl() == null) {
092                            return null;
093                    }
094    
095                    URL url = null;
096    
097                    if (Validator.isNull(httpPrincipal.getLogin()) ||
098                            Validator.isNull(httpPrincipal.getPassword())) {
099    
100                            url = new URL(httpPrincipal.getUrl() + "/tunnel-web/liferay/do");
101                    }
102                    else {
103                            url = new URL(
104                                    httpPrincipal.getUrl() + "/tunnel-web/secure/liferay/do");
105                    }
106    
107                    HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
108    
109                    urlc.setDoInput(true);
110                    urlc.setDoOutput(true);
111                    urlc.setUseCaches(false);
112    
113                    urlc.setRequestMethod("POST");
114    
115                    if (Validator.isNotNull(httpPrincipal.getLogin()) &&
116                            Validator.isNotNull(httpPrincipal.getPassword())) {
117    
118                            String userNameAndPassword =
119                                    httpPrincipal.getLogin() + StringPool.COLON +
120                                            httpPrincipal.getPassword();
121    
122                            urlc.setRequestProperty(
123                                    HttpHeaders.AUTHORIZATION,
124                                    HttpServletRequest.BASIC_AUTH + StringPool.SPACE +
125                                            Base64.encode(userNameAndPassword.getBytes()));
126                    }
127    
128                    return urlc;
129            }
130    
131    }