Source code: javatools/net/HttpSerialClient.java
1 /*
2 * HttpSerialClient.java
3 *
4 * Created on 2 aprile 2003, 19.25
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.net;
26
27 import java.util.HashMap;
28 import java.io.IOException;
29 import HTTPClient.*;
30 import javatools.util.*;
31
32 /**
33 *
34 * @author antonio
35 * @version 0.2.0
36 */
37 public class HttpSerialClient {
38
39 /** Creates a new instance of HttpSerialClient */
40 HttpSerialClient(String host) {
41 conn = new HTTPConnection(host);
42 prefix = "";
43 CookieModule.setCookiePolicyHandler(null);
44 }
45
46 HttpSerialClient(String host, String prefix) {
47 conn = new HTTPConnection(host);
48 this.prefix = prefix;
49 CookieModule.setCookiePolicyHandler(null);
50 }
51
52 HttpSerialClient(String host, int port) {
53 conn = new HTTPConnection(host, port);
54 prefix = "";
55 CookieModule.setCookiePolicyHandler(null);
56 }
57
58 HttpSerialClient(String host, String prefix, int port) {
59 conn = new HTTPConnection(host, port);
60 this.prefix = prefix;
61 CookieModule.setCookiePolicyHandler(null);
62 }
63
64 public static void clearAllClients() {
65 if (host2conn.size() > 0) {
66 host2conn = new HashMap();
67 CookieModule.discardAllCookies();
68 }
69 }
70
71 public static HttpSerialClient getClient(String host) {
72 HttpSerialClient conn;
73
74 conn = (HttpSerialClient) host2conn.get(host);
75 if (conn == null || conn.getPort() != 80) {
76 conn = new HttpSerialClient(host);
77 host2conn.put(host, conn);
78 }
79 else
80 conn.setPrefix("");
81 return conn;
82 }
83
84 public static HttpSerialClient getClient(String host, String prefix) {
85 HttpSerialClient conn;
86
87 conn = (HttpSerialClient) host2conn.get(host);
88 if (conn == null || conn.getPort() != 80) {
89 conn = new HttpSerialClient(host, prefix);
90 host2conn.put(host, conn);
91 }
92 else
93 conn.setPrefix(prefix);
94 return conn;
95 }
96
97 public static HttpSerialClient getClient(String host, int port) {
98 HttpSerialClient conn;
99
100 conn = (HttpSerialClient) host2conn.get(host);
101 if (conn == null || conn.getPort() != port) {
102 conn = new HttpSerialClient(host, port);
103 host2conn.put(host, conn);
104 }
105 else
106 conn.setPrefix("");
107 return conn;
108 }
109
110 public static HttpSerialClient getClient(String host, String prefix, int port) {
111 HttpSerialClient conn;
112
113 conn = (HttpSerialClient) host2conn.get(host);
114 if (conn == null || conn.getPort() != port) {
115 conn = new HttpSerialClient(host, prefix, port);
116 host2conn.put(host, conn);
117 }
118 else
119 conn.setPrefix(prefix);
120 return conn;
121 }
122
123 public static void setProxyConfig(String props) {
124 Props proxyProps;
125 String host, dontProxyForString;
126 String[] dontProxyFor;
127 int port;
128
129 try {
130 proxyProps = Props.singleton(props);
131 host = proxyProps.getProperty("host");
132 if (host != null) {
133 port = Integer.valueOf(proxyProps.getProperty("port")).intValue();
134 HTTPConnection.setProxyServer(host, port);
135 dontProxyForString = proxyProps.getProperty("dontProxyFor");
136 if (dontProxyForString != null) {
137 dontProxyFor = StringUtils.toTokenArray(dontProxyForString);
138 HTTPConnection.dontProxyFor(dontProxyFor);
139 }
140 }
141 }
142 catch (IOException e) {
143 }
144 }
145
146 public void setPrefix(String prefix) {
147 this.prefix = prefix;
148 }
149
150 public int getPort() {
151 return conn.getPort();
152 }
153
154 public synchronized HTTPResponse Head(String file) throws IOException {
155 try {
156 return conn.Head(prefix + file);
157 }
158 catch(ModuleException e) {
159 throw new IOException(e.getMessage());
160 }
161 }
162
163 public synchronized HTTPResponse Head(String file, NVPair form_data[])
164 throws IOException {
165 try {
166 return conn.Head(prefix + file, form_data);
167 }
168 catch(ModuleException e) {
169 throw new IOException(e.getMessage());
170 }
171 }
172
173 public synchronized HTTPResponse Head(String file, NVPair[] form_data, NVPair[] headers)
174 throws IOException {
175 try {
176 return conn.Head(prefix + file, form_data, headers);
177 }
178 catch(ModuleException e) {
179 throw new IOException(e.getMessage());
180 }
181 }
182
183 public synchronized HTTPResponse Head(String file, String query)
184 throws IOException {
185 try {
186 return conn.Head(prefix + file, query);
187 }
188 catch(ModuleException e) {
189 throw new IOException(e.getMessage());
190 }
191 }
192
193 public synchronized HTTPResponse Head(String file, String query, NVPair[] headers)
194 throws IOException {
195 try {
196 return conn.Head(prefix + file, query, headers);
197 }
198 catch(ModuleException e) {
199 throw new IOException(e.getMessage());
200 }
201 }
202
203 public synchronized HTTPResponse Get(String file) throws IOException {
204 try {
205 return conn.Get(prefix + file);
206 }
207 catch(ModuleException e) {
208 throw new IOException(e.getMessage());
209 }
210 }
211
212 public synchronized HTTPResponse Get(String file, NVPair form_data[])
213 throws IOException {
214 try {
215 return conn.Get(prefix + file, form_data);
216 }
217 catch(ModuleException e) {
218 throw new IOException(e.getMessage());
219 }
220 }
221
222 public synchronized HTTPResponse Get(String file, NVPair[] form_data, NVPair[] headers)
223 throws IOException {
224 try {
225 return conn.Get(prefix + file, form_data, headers);
226 }
227 catch(ModuleException e) {
228 throw new IOException(e.getMessage());
229 }
230 }
231
232 public synchronized HTTPResponse Get(String file, String query)
233 throws IOException {
234 try {
235 return conn.Get(prefix + file, query);
236 }
237 catch(ModuleException e) {
238 throw new IOException(e.getMessage());
239 }
240 }
241
242 public synchronized HTTPResponse Get(String file, String query, NVPair[] headers)
243 throws IOException {
244 try {
245 return conn.Get(prefix + file, query, headers);
246 }
247 catch(ModuleException e) {
248 throw new IOException(e.getMessage());
249 }
250 }
251
252 public synchronized HTTPResponse Post(String file) throws IOException {
253 try {
254 return conn.Post(prefix + file);
255 }
256 catch(ModuleException e) {
257 throw new IOException(e.getMessage());
258 }
259 }
260
261 public synchronized HTTPResponse Post(String file, NVPair form_data[])
262 throws IOException {
263 try {
264 return conn.Post(prefix + file, form_data);
265 }
266 catch(ModuleException e) {
267 throw new IOException(e.getMessage());
268 }
269 }
270
271 public synchronized HTTPResponse Post(String file, NVPair form_data[], NVPair headers[])
272 throws IOException {
273 try {
274 return conn.Post(prefix + file, form_data, headers);
275 }
276 catch(ModuleException e) {
277 throw new IOException(e.getMessage());
278 }
279 }
280
281 public synchronized HTTPResponse Post(String file, String data)
282 throws IOException {
283 try {
284 return conn.Post(prefix + file, data);
285 }
286 catch(ModuleException e) {
287 throw new IOException(e.getMessage());
288 }
289 }
290
291 public synchronized HTTPResponse Post(String file, String data, NVPair[] headers)
292 throws IOException {
293 try {
294 return conn.Post(prefix + file, data, headers);
295 }
296 catch(ModuleException e) {
297 throw new IOException(e.getMessage());
298 }
299 }
300
301 public synchronized HTTPResponse Post(String file, byte data[])
302 throws IOException {
303 try {
304 return conn.Post(prefix + file, data);
305 }
306 catch(ModuleException e) {
307 throw new IOException(e.getMessage());
308 }
309 }
310
311 public synchronized HTTPResponse Post(String file, byte data[], NVPair[] headers)
312 throws IOException {
313 try {
314 return conn.Post(prefix + file, data, headers);
315 }
316 catch(ModuleException e) {
317 throw new IOException(e.getMessage());
318 }
319 }
320
321 public synchronized HTTPResponse Post(String file, HttpOutputStream stream)
322 throws IOException {
323 try {
324 return conn.Post(prefix + file, stream);
325 }
326 catch(ModuleException e) {
327 throw new IOException(e.getMessage());
328 }
329 }
330
331 public synchronized HTTPResponse Post(String file, HttpOutputStream stream,
332 NVPair[] headers) throws IOException {
333 try {
334 return conn.Post(prefix + file, stream, headers);
335 }
336 catch(ModuleException e) {
337 throw new IOException(e.getMessage());
338 }
339 }
340
341 static {
342 host2conn = new HashMap();
343 }
344
345 private static HashMap host2conn;
346
347 private HTTPConnection conn;
348 private String prefix;
349 }