Source code: com/barteo/cldc/http/Connection.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001,2002 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.cldc.http;
21
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.InputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.net.HttpURLConnection;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.net.URLConnection;
31
32 import javax.microedition.io.HttpConnection;
33
34 import com.barteo.cldc.ClosedConnection;
35
36
37 public class Connection implements HttpConnection, ClosedConnection
38 {
39 URLConnection cn;
40 boolean connected = false;
41
42 public javax.microedition.io.Connection open(String name)
43 throws IOException
44 {
45 URL url;
46 try {
47 url = new URL(name);
48 } catch (MalformedURLException ex) {
49 throw new IOException(ex.toString());
50 }
51 cn = url.openConnection();
52
53 return this;
54 }
55
56
57 public void close()
58 throws IOException
59 {
60 if (cn == null) {
61 return;
62 }
63
64 if (cn instanceof HttpURLConnection) {
65 ((HttpURLConnection) cn).disconnect();
66 }
67
68 cn = null;
69 }
70
71
72 public String getURL()
73 {
74 if (cn == null) {
75 return null;
76 }
77
78 return cn.getURL().toString();
79 }
80
81
82 public String getProtocol()
83 {
84 return "http";
85 }
86
87
88 public String getHost()
89 {
90 if (cn == null) {
91 return null;
92 }
93
94 return cn.getURL().getHost();
95 }
96
97
98 public String getFile()
99 {
100 if (cn == null) {
101 return null;
102 }
103
104 return cn.getURL().getFile();
105 }
106
107
108 public String getRef()
109 {
110 if (cn == null) {
111 return null;
112 }
113
114 return cn.getURL().getRef();
115 }
116
117
118 public String getQuery()
119 {
120 if (cn == null) {
121 return null;
122 }
123
124 // return cn.getURL().getQuery();
125 return null;
126 }
127
128
129 public int getPort()
130 {
131 if (cn == null) {
132 return -1;
133 }
134
135 return cn.getURL().getPort();
136 }
137
138
139 public String getRequestMethod()
140 {
141 if (cn == null) {
142 return null;
143 }
144
145 if (cn instanceof HttpURLConnection) {
146 return ((HttpURLConnection) cn).getRequestMethod();
147 } else {
148 return null;
149 }
150 }
151
152
153 public void setRequestMethod(String method)
154 throws IOException
155 {
156 if (cn == null) {
157 throw new IOException();
158 }
159
160 if (method.equals(HttpConnection.POST)) {
161 cn.setDoOutput(true);
162 }
163
164 if (cn instanceof HttpURLConnection) {
165 ((HttpURLConnection) cn).setRequestMethod(method);
166 }
167 }
168
169
170 public String getRequestProperty(String key)
171 {
172 if (cn == null) {
173 return null;
174 }
175
176 return cn.getRequestProperty(key);
177 }
178
179
180 public void setRequestProperty(String key, String value)
181 throws IOException
182 {
183 if (cn == null || connected) {
184 throw new IOException();
185 }
186
187 cn.setRequestProperty(key, value);
188 }
189
190
191 public int getResponseCode()
192 throws IOException
193 {
194 if (cn == null) {
195 throw new IOException();
196 }
197 if (!connected) {
198 cn.connect();
199 connected = true;
200 }
201
202 if (cn instanceof HttpURLConnection) {
203 return ((HttpURLConnection) cn).getResponseCode();
204 } else {
205 return -1;
206 }
207 }
208
209
210 public String getResponseMessage()
211 throws IOException
212 {
213 if (cn == null) {
214 throw new IOException();
215 }
216 if (!connected) {
217 cn.connect();
218 connected = true;
219 }
220
221 if (cn instanceof HttpURLConnection) {
222 return ((HttpURLConnection) cn).getResponseMessage();
223 } else {
224 return null;
225 }
226 }
227
228
229 public long getExpiration()
230 throws IOException
231 {
232 if (cn == null) {
233 throw new IOException();
234 }
235 if (!connected) {
236 cn.connect();
237 connected = true;
238 }
239
240 return cn.getExpiration();
241 }
242
243
244 public long getDate()
245 throws IOException
246 {
247 if (cn == null) {
248 throw new IOException();
249 }
250 if (!connected) {
251 cn.connect();
252 connected = true;
253 }
254
255 return cn.getDate();
256 }
257
258
259 public long getLastModified()
260 throws IOException
261 {
262 if (cn == null) {
263 throw new IOException();
264 }
265 if (!connected) {
266 cn.connect();
267 connected = true;
268 }
269
270 return cn.getLastModified();
271 }
272
273
274 public String getHeaderField(String name)
275 throws IOException
276 {
277 if (cn == null) {
278 throw new IOException();
279 }
280 if (!connected) {
281 cn.connect();
282 connected = true;
283 }
284
285 return cn.getHeaderField(name);
286 }
287
288
289 public int getHeaderFieldInt(String name, int def)
290 throws IOException
291 {
292 if (cn == null) {
293 throw new IOException();
294 }
295 if (!connected) {
296 cn.connect();
297 connected = true;
298 }
299
300 return cn.getHeaderFieldInt(name, def);
301 }
302
303
304 public long getHeaderFieldDate(String name, long def)
305 throws IOException
306 {
307 if (cn == null) {
308 throw new IOException();
309 }
310 if (!connected) {
311 cn.connect();
312 connected = true;
313 }
314
315 return cn.getHeaderFieldDate(name, def);
316 }
317
318
319 public String getHeaderField(int n)
320 throws IOException
321 {
322 if (cn == null) {
323 throw new IOException();
324 }
325 if (!connected) {
326 cn.connect();
327 connected = true;
328 }
329
330 return cn.getHeaderField(n);
331 }
332
333
334 public String getHeaderFieldKey(int n)
335 throws IOException
336 {
337 if (cn == null) {
338 throw new IOException();
339 }
340 if (!connected) {
341 cn.connect();
342 connected = true;
343 }
344
345 return cn.getHeaderFieldKey(n);
346 }
347
348
349 public InputStream openInputStream()
350 throws IOException
351 {
352 if (cn == null) {
353 throw new IOException();
354 }
355
356 connected = true;
357
358 return cn.getInputStream();
359 }
360
361
362 public DataInputStream openDataInputStream()
363 throws IOException
364 {
365 return new DataInputStream(openInputStream());
366 }
367
368
369 public OutputStream openOutputStream()
370 throws IOException
371 {
372 if (cn == null) {
373 throw new IOException();
374 }
375
376 connected = true;
377
378 return cn.getOutputStream();
379 }
380
381
382 public DataOutputStream openDataOutputStream()
383 throws IOException
384 {
385 return new DataOutputStream(openOutputStream());
386 }
387
388
389 public String getType()
390 {
391 try {
392 return getHeaderField("content-type");
393 } catch (IOException ex) {
394 return null;
395 }
396 }
397
398
399 public String getEncoding()
400 {
401 try {
402 return getHeaderField("content-encoding");
403 } catch (IOException ex) {
404 return null;
405 }
406 }
407
408
409 public long getLength()
410 {
411 try {
412 return getHeaderFieldInt("content-length", -1);
413 } catch (IOException ex) {
414 return -1;
415 }
416 }
417
418 }