001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.http;
022:
023: import java.io.EOFException;
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.net.HttpURLConnection;
028: import java.net.URL;
029:
030: import com.liferay.portal.kernel.util.Base64;
031: import com.liferay.portal.kernel.util.MethodWrapper;
032: import com.liferay.portal.kernel.util.ObjectValuePair;
033: import com.liferay.portal.security.auth.HttpPrincipal;
034: import com.liferay.portal.security.auth.PrincipalException;
035:
036: /**
037: * <a href="TunnelUtil.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class TunnelUtil {
043:
044: public static Object invoke(HttpPrincipal httpPrincipal,
045: MethodWrapper methodWrapper) throws Exception {
046:
047: HttpURLConnection urlc = _getConnection(httpPrincipal);
048:
049: ObjectOutputStream oos = new ObjectOutputStream(urlc
050: .getOutputStream());
051:
052: oos.writeObject(new ObjectValuePair(httpPrincipal,
053: methodWrapper));
054:
055: oos.flush();
056: oos.close();
057:
058: Object returnObj = null;
059:
060: try {
061: ObjectInputStream ois = new ObjectInputStream(urlc
062: .getInputStream());
063:
064: returnObj = ois.readObject();
065:
066: ois.close();
067: } catch (EOFException eofe) {
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: } else {
076: throw ioe;
077: }
078: }
079:
080: if ((returnObj != null) && returnObj instanceof Exception) {
081: throw (Exception) returnObj;
082: }
083:
084: return returnObj;
085: }
086:
087: private static HttpURLConnection _getConnection(
088: HttpPrincipal httpPrincipal) throws IOException {
089:
090: if (httpPrincipal == null || httpPrincipal.getUrl() == null) {
091: return null;
092: }
093:
094: URL url = null;
095:
096: if ((httpPrincipal.getUserId() <= 0)
097: || (httpPrincipal.getPassword() == null)) {
098:
099: url = new URL(httpPrincipal.getUrl()
100: + "/tunnel-web/liferay/do");
101: } else {
102: url = new URL(httpPrincipal.getUrl()
103: + "/tunnel-web/secure/liferay/do");
104: }
105:
106: HttpURLConnection urlc = (HttpURLConnection) url
107: .openConnection();
108:
109: urlc.setDoInput(true);
110: urlc.setDoOutput(true);
111: urlc.setUseCaches(false);
112:
113: urlc.setRequestMethod("POST");
114:
115: if ((httpPrincipal.getUserId() > 0)
116: && (httpPrincipal.getPassword() != null)) {
117:
118: String userNameAndPassword = httpPrincipal.getUserId()
119: + ":" + httpPrincipal.getPassword();
120:
121: urlc.setRequestProperty("Authorization", "Basic "
122: + Base64.encode(userNameAndPassword.getBytes()));
123: }
124:
125: return urlc;
126: }
127:
128: }
|