001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodWrapper;
021    import com.liferay.portal.kernel.util.ObjectValuePair;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.HttpPrincipal;
025    import com.liferay.portal.security.auth.PrincipalException;
026    
027    import java.io.EOFException;
028    import java.io.IOException;
029    import java.io.ObjectInputStream;
030    import java.io.ObjectOutputStream;
031    
032    import java.net.HttpURLConnection;
033    import java.net.URL;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    @SuppressWarnings("deprecation")
041    public class TunnelUtil {
042    
043            public static Object invoke(
044                            HttpPrincipal httpPrincipal, MethodHandler methodHandler)
045                    throws Exception {
046    
047                    HttpURLConnection urlc = _getConnection(httpPrincipal);
048    
049                    ObjectOutputStream oos = new ObjectOutputStream(urlc.getOutputStream());
050    
051                    oos.writeObject(
052                            new ObjectValuePair<HttpPrincipal, MethodHandler>(
053                                    httpPrincipal, methodHandler));
054    
055                    oos.flush();
056                    oos.close();
057    
058                    Object returnObj = null;
059    
060                    try {
061                            ObjectInputStream ois = new ObjectInputStream(
062                                    urlc.getInputStream());
063    
064                            returnObj = ois.readObject();
065    
066                            ois.close();
067                    }
068                    catch (EOFException eofe) {
069                    }
070                    catch (IOException ioe) {
071                            String ioeMessage = ioe.getMessage();
072    
073                            if ((ioeMessage != null) &&
074                                            (ioeMessage.indexOf("HTTP response code: 401") != -1)) {
075    
076                                    throw new PrincipalException(ioeMessage);
077                            }
078                            else {
079                                    throw ioe;
080                            }
081                    }
082    
083                    if ((returnObj != null) && returnObj instanceof Exception) {
084                            throw (Exception)returnObj;
085                    }
086    
087                    return returnObj;
088            }
089    
090            /**
091             * @deprecated
092             */
093            public static Object invoke(
094                            HttpPrincipal httpPrincipal, MethodWrapper methodWrapper)
095                    throws Exception {
096    
097                    HttpURLConnection urlc = _getConnection(httpPrincipal);
098    
099                    ObjectOutputStream oos = new ObjectOutputStream(urlc.getOutputStream());
100    
101                    oos.writeObject(
102                            new ObjectValuePair<HttpPrincipal, MethodWrapper>(
103                                    httpPrincipal, methodWrapper));
104    
105                    oos.flush();
106                    oos.close();
107    
108                    Object returnObj = null;
109    
110                    try {
111                            ObjectInputStream ois = new ObjectInputStream(
112                                    urlc.getInputStream());
113    
114                            returnObj = ois.readObject();
115    
116                            ois.close();
117                    }
118                    catch (EOFException eofe) {
119                    }
120                    catch (IOException ioe) {
121                            String ioeMessage = ioe.getMessage();
122    
123                            if ((ioeMessage != null) &&
124                                            (ioeMessage.indexOf("HTTP response code: 401") != -1)) {
125    
126                                    throw new PrincipalException(ioeMessage);
127                            }
128                            else {
129                                    throw ioe;
130                            }
131                    }
132    
133                    if ((returnObj != null) && returnObj instanceof Exception) {
134                            throw (Exception)returnObj;
135                    }
136    
137                    return returnObj;
138            }
139    
140            private static HttpURLConnection _getConnection(HttpPrincipal httpPrincipal)
141                    throws IOException {
142    
143                    if (httpPrincipal == null || httpPrincipal.getUrl() == null) {
144                            return null;
145                    }
146    
147                    URL url = null;
148    
149                    if (Validator.isNull(httpPrincipal.getLogin()) ||
150                            Validator.isNull(httpPrincipal.getPassword())) {
151    
152                            url = new URL(httpPrincipal.getUrl() + "/tunnel-web/liferay/do");
153                    }
154                    else {
155                            url = new URL(
156                                    httpPrincipal.getUrl() + "/tunnel-web/secure/liferay/do");
157                    }
158    
159                    HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
160    
161                    urlc.setDoInput(true);
162                    urlc.setDoOutput(true);
163                    urlc.setUseCaches(false);
164    
165                    urlc.setRequestMethod("POST");
166    
167                    if (Validator.isNotNull(httpPrincipal.getLogin()) &&
168                            Validator.isNotNull(httpPrincipal.getPassword())) {
169    
170                            String userNameAndPassword =
171                                    httpPrincipal.getLogin() + StringPool.COLON +
172                                            httpPrincipal.getPassword();
173    
174                            urlc.setRequestProperty(
175                                    HttpHeaders.AUTHORIZATION,
176                                    HttpServletRequest.BASIC_AUTH + StringPool.SPACE +
177                                            Base64.encode(userNameAndPassword.getBytes()));
178                    }
179    
180                    return urlc;
181            }
182    
183    }