1
14
15 package com.liferay.portal.service.http;
16
17 import com.liferay.portal.kernel.servlet.HttpHeaders;
18 import com.liferay.portal.kernel.util.Base64;
19 import com.liferay.portal.kernel.util.MethodHandler;
20 import com.liferay.portal.kernel.util.MethodWrapper;
21 import com.liferay.portal.kernel.util.ObjectValuePair;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.security.auth.HttpPrincipal;
25 import com.liferay.portal.security.auth.PrincipalException;
26
27 import java.io.EOFException;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
31
32 import java.net.HttpURLConnection;
33 import java.net.URL;
34
35 import javax.servlet.http.HttpServletRequest;
36
37
42 @SuppressWarnings("deprecation")
43 public class TunnelUtil {
44
45 public static Object invoke(
46 HttpPrincipal httpPrincipal, MethodHandler methodHandler)
47 throws Exception {
48
49 HttpURLConnection urlc = _getConnection(httpPrincipal);
50
51 ObjectOutputStream oos = new ObjectOutputStream(urlc.getOutputStream());
52
53 oos.writeObject(
54 new ObjectValuePair<HttpPrincipal, MethodHandler>(
55 httpPrincipal, methodHandler));
56
57 oos.flush();
58 oos.close();
59
60 Object returnObj = null;
61
62 try {
63 ObjectInputStream ois = new ObjectInputStream(
64 urlc.getInputStream());
65
66 returnObj = ois.readObject();
67
68 ois.close();
69 }
70 catch (EOFException eofe) {
71 }
72 catch (IOException ioe) {
73 String ioeMessage = ioe.getMessage();
74
75 if ((ioeMessage != null) &&
76 (ioeMessage.indexOf("HTTP response code: 401") != -1)) {
77
78 throw new PrincipalException(ioeMessage);
79 }
80 else {
81 throw ioe;
82 }
83 }
84
85 if ((returnObj != null) && returnObj instanceof Exception) {
86 throw (Exception)returnObj;
87 }
88
89 return returnObj;
90 }
91
92
95 public static Object invoke(
96 HttpPrincipal httpPrincipal, MethodWrapper methodWrapper)
97 throws Exception {
98
99 HttpURLConnection urlc = _getConnection(httpPrincipal);
100
101 ObjectOutputStream oos = new ObjectOutputStream(urlc.getOutputStream());
102
103 oos.writeObject(
104 new ObjectValuePair<HttpPrincipal, MethodWrapper>(
105 httpPrincipal, methodWrapper));
106
107 oos.flush();
108 oos.close();
109
110 Object returnObj = null;
111
112 try {
113 ObjectInputStream ois = new ObjectInputStream(
114 urlc.getInputStream());
115
116 returnObj = ois.readObject();
117
118 ois.close();
119 }
120 catch (EOFException eofe) {
121 }
122 catch (IOException ioe) {
123 String ioeMessage = ioe.getMessage();
124
125 if ((ioeMessage != null) &&
126 (ioeMessage.indexOf("HTTP response code: 401") != -1)) {
127
128 throw new PrincipalException(ioeMessage);
129 }
130 else {
131 throw ioe;
132 }
133 }
134
135 if ((returnObj != null) && returnObj instanceof Exception) {
136 throw (Exception)returnObj;
137 }
138
139 return returnObj;
140 }
141
142 private static HttpURLConnection _getConnection(HttpPrincipal httpPrincipal)
143 throws IOException {
144
145 if (httpPrincipal == null || httpPrincipal.getUrl() == null) {
146 return null;
147 }
148
149 URL url = null;
150
151 if (Validator.isNull(httpPrincipal.getLogin()) ||
152 Validator.isNull(httpPrincipal.getPassword())) {
153
154 url = new URL(httpPrincipal.getUrl() + "/tunnel-web/liferay/do");
155 }
156 else {
157 url = new URL(
158 httpPrincipal.getUrl() + "/tunnel-web/secure/liferay/do");
159 }
160
161 HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
162
163 urlc.setDoInput(true);
164 urlc.setDoOutput(true);
165 urlc.setUseCaches(false);
166
167 urlc.setRequestMethod("POST");
168
169 if (Validator.isNotNull(httpPrincipal.getLogin()) &&
170 Validator.isNotNull(httpPrincipal.getPassword())) {
171
172 String userNameAndPassword =
173 httpPrincipal.getLogin() + StringPool.COLON +
174 httpPrincipal.getPassword();
175
176 urlc.setRequestProperty(
177 HttpHeaders.AUTHORIZATION,
178 HttpServletRequest.BASIC_AUTH + StringPool.SPACE +
179 Base64.encode(userNameAndPassword.getBytes()));
180 }
181
182 return urlc;
183 }
184
185 }