001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.url.http;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.net.ProtocolException;
023: import java.net.URL;
024: import java.security.Permission;
025: import java.util.Map;
026: import java.util.logging.Logger;
027:
028: import org.columba.core.connectionstate.ConnectionStateImpl;
029:
030: /**
031: * This class acts as a proxy between clients using HTTP URLs and the underlying
032: * URLConnection implementation. Depending on the system's connection state
033: * it throws IOExceptions indicating that there is no internet connection
034: * available.
035: */
036: public class HttpURLConnection extends java.net.HttpURLConnection {
037: private static final Logger LOG = Logger
038: .getLogger("org.columba.core.url.http");
039:
040: protected java.net.HttpURLConnection connection;
041:
042: public HttpURLConnection(URL u,
043: java.net.HttpURLConnection connection) {
044: super (u);
045: this .connection = connection;
046: connection.setUseCaches(false);
047: connection.setDefaultUseCaches(false);
048: }
049:
050: /**
051: * Checks whether a connection is available and throws a IOConnection if not.
052: */
053: protected void ensureConnectionAllowed() throws IOException {
054: if (!ConnectionStateImpl.getInstance().isOnline()) {
055: LOG.fine("Blocking HTTP connection to "
056: + getURL().toString());
057: throw new IOException("Error while connecting.");
058: }
059: }
060:
061: public OutputStream getOutputStream() throws IOException {
062: ensureConnectionAllowed();
063: return connection.getOutputStream();
064: }
065:
066: public void setDefaultUseCaches(boolean param) {
067: }
068:
069: public long getIfModifiedSince() {
070: return connection.getIfModifiedSince();
071: }
072:
073: public String getResponseMessage() throws IOException {
074: if (!ConnectionStateImpl.getInstance().isOnline()) {
075: return "Not Found";
076: } else {
077: return connection.getResponseMessage();
078: }
079: }
080:
081: public Permission getPermission() throws IOException {
082: return connection.getPermission();
083: }
084:
085: public void setUseCaches(boolean param) {
086: }
087:
088: public void addRequestProperty(String str, String str1) {
089: connection.addRequestProperty(str, str1);
090: }
091:
092: public boolean getInstanceFollowRedirects() {
093: return connection.getInstanceFollowRedirects();
094: }
095:
096: public Map getRequestProperties() {
097: return connection.getRequestProperties();
098: }
099:
100: public void setRequestMethod(String str) throws ProtocolException {
101: super .setRequestMethod(str);
102: }
103:
104: public boolean getDefaultUseCaches() {
105: return connection.getDefaultUseCaches();
106: }
107:
108: public String getRequestMethod() {
109: return connection.getRequestMethod();
110: }
111:
112: public boolean getDoOutput() {
113: return connection.getDoOutput();
114: }
115:
116: public long getDate() {
117: return connection.getDate();
118: }
119:
120: public int getResponseCode() throws IOException {
121: if (!ConnectionStateImpl.getInstance().isOnline()) {
122: return HTTP_NOT_FOUND;
123: } else {
124: return connection.getResponseCode();
125: }
126: }
127:
128: public long getHeaderFieldDate(String str, long param) {
129: return connection.getHeaderFieldDate(str, param);
130: }
131:
132: public boolean usingProxy() {
133: return connection.usingProxy();
134: }
135:
136: public void setRequestProperty(String str, String str1) {
137: connection.setRequestProperty(str, str1);
138: }
139:
140: public String getHeaderField(int param) {
141: return connection.getHeaderField(param);
142: }
143:
144: public void setAllowUserInteraction(boolean param) {
145: super .setAllowUserInteraction(param);
146: }
147:
148: public String getHeaderField(String str) {
149: return connection.getHeaderField(str);
150: }
151:
152: public InputStream getErrorStream() {
153: return connection.getErrorStream();
154: }
155:
156: public Object getContent(Class[] clazz) throws IOException {
157: ensureConnectionAllowed();
158: return connection.getContent(clazz);
159: }
160:
161: public void disconnect() {
162: connection.disconnect();
163: }
164:
165: public String getHeaderFieldKey(int param) {
166: return connection.getHeaderFieldKey(param);
167: }
168:
169: public long getExpiration() {
170: return connection.getExpiration();
171: }
172:
173: public void setDoInput(boolean param) {
174: connection.setDoInput(param);
175: }
176:
177: public Object getContent() throws IOException {
178: ensureConnectionAllowed();
179: return connection.getContent();
180: }
181:
182: public boolean getUseCaches() {
183: return connection.getUseCaches();
184: }
185:
186: public long getLastModified() {
187: return connection.getLastModified();
188: }
189:
190: public boolean getDoInput() {
191: return connection.getDoInput();
192: }
193:
194: public String getContentType() {
195: return connection.getContentType();
196: }
197:
198: public Map getHeaderFields() {
199: return connection.getHeaderFields();
200: }
201:
202: public void setDoOutput(boolean param) {
203: connection.setDoOutput(param);
204: }
205:
206: public InputStream getInputStream() throws IOException {
207: ensureConnectionAllowed();
208: return connection.getInputStream();
209: }
210:
211: public String getRequestProperty(String str) {
212: return connection.getRequestProperty(str);
213: }
214:
215: public int getHeaderFieldInt(String str, int param) {
216: return connection.getHeaderFieldInt(str, param);
217: }
218:
219: public void setInstanceFollowRedirects(boolean param) {
220: connection.setInstanceFollowRedirects(param);
221: }
222:
223: public void setIfModifiedSince(long param) {
224: connection.setIfModifiedSince(param);
225: }
226:
227: public String getContentEncoding() {
228: return connection.getContentEncoding();
229: }
230:
231: public void connect() throws IOException {
232: ensureConnectionAllowed();
233: connection.connect();
234: }
235:
236: public int getContentLength() {
237: return connection.getContentLength();
238: }
239:
240: public boolean getAllowUserInteraction() {
241: return connection.getAllowUserInteraction();
242: }
243: }
|