KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > url > http > HttpURLConnection


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.url.http;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.ProtocolException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.security.Permission JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.columba.core.connectionstate.ConnectionStateImpl;
29
30 /**
31  * This class acts as a proxy between clients using HTTP URLs and the underlying
32  * URLConnection implementation. Depending on the system's connection state
33  * it throws IOExceptions indicating that there is no internet connection
34  * available.
35  */

36 public class HttpURLConnection extends java.net.HttpURLConnection JavaDoc {
37     private static final Logger JavaDoc LOG = Logger
38             .getLogger("org.columba.core.url.http");
39     
40     protected java.net.HttpURLConnection JavaDoc connection;
41     
42     public HttpURLConnection(URL JavaDoc u, java.net.HttpURLConnection JavaDoc connection) {
43         super(u);
44         this.connection = connection;
45         connection.setUseCaches(false);
46         connection.setDefaultUseCaches(false);
47     }
48     
49     /**
50      * Checks whether a connection is available and throws a IOConnection if not.
51      */

52     protected void ensureConnectionAllowed() throws IOException JavaDoc {
53         if (!ConnectionStateImpl.getInstance().isOnline()) {
54             LOG.fine("Blocking HTTP connection to " + getURL().toString());
55             throw new IOException JavaDoc("Error while connecting.");
56         }
57     }
58     
59     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
60         ensureConnectionAllowed();
61         return connection.getOutputStream();
62     }
63     
64     public void setDefaultUseCaches(boolean param) {}
65     
66     public long getIfModifiedSince() {
67         return connection.getIfModifiedSince();
68     }
69     
70     public String JavaDoc getResponseMessage() throws IOException JavaDoc {
71         if (!ConnectionStateImpl.getInstance().isOnline()) {
72             return "Not Found";
73         } else {
74             return connection.getResponseMessage();
75         }
76     }
77     
78     public Permission JavaDoc getPermission() throws IOException JavaDoc {
79         return connection.getPermission();
80     }
81     
82     public void setUseCaches(boolean param) {}
83     
84     public void addRequestProperty(String JavaDoc str, String JavaDoc str1) {
85         connection.addRequestProperty(str, str1);
86     }
87     
88     public boolean getInstanceFollowRedirects() {
89         return connection.getInstanceFollowRedirects();
90     }
91     
92     public Map JavaDoc getRequestProperties() {
93         return connection.getRequestProperties();
94     }
95     
96     public void setRequestMethod(String JavaDoc str) throws ProtocolException JavaDoc {
97         super.setRequestMethod(str);
98     }
99     
100     public boolean getDefaultUseCaches() {
101         return connection.getDefaultUseCaches();
102     }
103     
104     public String JavaDoc getRequestMethod() {
105         return connection.getRequestMethod();
106     }
107     
108     public boolean getDoOutput() {
109         return connection.getDoOutput();
110     }
111     
112     public long getDate() {
113         return connection.getDate();
114     }
115     
116     public int getResponseCode() throws IOException JavaDoc {
117         if (!ConnectionStateImpl.getInstance().isOnline()) {
118             return HTTP_NOT_FOUND;
119         } else {
120             return connection.getResponseCode();
121         }
122     }
123     
124     public long getHeaderFieldDate(String JavaDoc str, long param) {
125         return connection.getHeaderFieldDate(str, param);
126     }
127     
128     public boolean usingProxy() {
129         return connection.usingProxy();
130     }
131     
132     public void setRequestProperty(String JavaDoc str, String JavaDoc str1) {
133         connection.setRequestProperty(str, str1);
134     }
135     
136     public String JavaDoc getHeaderField(int param) {
137         return connection.getHeaderField(param);
138     }
139     
140     public void setAllowUserInteraction(boolean param) {
141         super.setAllowUserInteraction(param);
142     }
143     
144     public String JavaDoc getHeaderField(String JavaDoc str) {
145         return connection.getHeaderField(str);
146     }
147     
148     public InputStream JavaDoc getErrorStream() {
149         return connection.getErrorStream();
150     }
151     
152     public Object JavaDoc getContent(Class JavaDoc[] clazz) throws IOException JavaDoc {
153         ensureConnectionAllowed();
154         return connection.getContent(clazz);
155     }
156     
157     public void disconnect() {
158         connection.disconnect();
159     }
160     
161     public String JavaDoc getHeaderFieldKey(int param) {
162         return connection.getHeaderFieldKey(param);
163     }
164     
165     public long getExpiration() {
166         return connection.getExpiration();
167     }
168     
169     public void setDoInput(boolean param) {
170         connection.setDoInput(param);
171     }
172     
173     public Object JavaDoc getContent() throws IOException JavaDoc {
174         ensureConnectionAllowed();
175         return connection.getContent();
176     }
177     
178     public boolean getUseCaches() {
179         return connection.getUseCaches();
180     }
181     
182     public long getLastModified() {
183         return connection.getLastModified();
184     }
185     
186     public boolean getDoInput() {
187         return connection.getDoInput();
188     }
189     
190     public String JavaDoc getContentType() {
191         return connection.getContentType();
192     }
193     
194     public Map JavaDoc getHeaderFields() {
195         return connection.getHeaderFields();
196     }
197     
198     public void setDoOutput(boolean param) {
199         connection.setDoOutput(param);
200     }
201     
202     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
203         ensureConnectionAllowed();
204         return connection.getInputStream();
205     }
206     
207     public String JavaDoc getRequestProperty(String JavaDoc str) {
208         return connection.getRequestProperty(str);
209     }
210     
211     public int getHeaderFieldInt(String JavaDoc str, int param) {
212         return connection.getHeaderFieldInt(str, param);
213     }
214     
215     public void setInstanceFollowRedirects(boolean param) {
216         connection.setInstanceFollowRedirects(param);
217     }
218     
219     public void setIfModifiedSince(long param) {
220         connection.setIfModifiedSince(param);
221     }
222     
223     public String JavaDoc getContentEncoding() {
224         return connection.getContentEncoding();
225     }
226     
227     public void connect() throws IOException JavaDoc {
228         ensureConnectionAllowed();
229         connection.connect();
230     }
231     
232     public int getContentLength() {
233         return connection.getContentLength();
234     }
235     
236     public boolean getAllowUserInteraction() {
237         return connection.getAllowUserInteraction();
238     }
239 }
240
Popular Tags