KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > connection > HttpResponse


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.core.connection;
12
13 import java.io.FilterInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.HttpURLConnection JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.net.URLConnection JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.update.internal.core.IStatusCodes;
23 import org.eclipse.update.internal.core.Messages;
24 import org.eclipse.update.internal.core.UpdateCore;
25
26 public class HttpResponse extends AbstractResponse {
27     /**
28      * Monitored InputStream. Upon IOException, discards
29      * connection so it is not reused.
30      *
31      */

32     private class MonitoringInputStream extends FilterInputStream JavaDoc {
33         private URLConnection JavaDoc connection;
34
35         public MonitoringInputStream(InputStream JavaDoc in, URLConnection JavaDoc connection) {
36             super(in);
37             this.connection = connection;
38         }
39
40         public int available() throws IOException JavaDoc {
41             try {
42                 return in!=null?super.available():0;
43             } catch (IOException JavaDoc ioe) {
44                 connection = null;
45                 throw ioe;
46             }
47         }
48
49         public void close() throws IOException JavaDoc {
50             try {
51                 if (in!=null)
52                     super.close();
53                 if (connection instanceof HttpURLConnection JavaDoc) {
54                     ((HttpURLConnection JavaDoc)connection).disconnect();
55                 }
56             } catch (IOException JavaDoc ioe) {
57                 connection = null;
58                 throw ioe;
59             }
60         }
61
62         public int read() throws IOException JavaDoc {
63             try {
64                 return in!=null?super.read():-1;
65             } catch (IOException JavaDoc ioe) {
66                 connection = null;
67                 throw ioe;
68             }
69         }
70
71         public synchronized void reset() throws IOException JavaDoc {
72             try {
73                 if (in!=null)
74                     super.reset();
75             } catch (IOException JavaDoc ioe) {
76                 connection = null;
77                 throw ioe;
78             }
79         }
80
81         public int read(byte[] b) throws IOException JavaDoc {
82             try {
83                 return in!=null?super.read(b):-1;
84             } catch (IOException JavaDoc ioe) {
85                 connection = null;
86                 throw ioe;
87             }
88         }
89
90         public int read(byte[] b, int off, int len) throws IOException JavaDoc {
91             try {
92                 return in!=null?super.read(b, off, len):-1;
93             } catch (IOException JavaDoc ioe) {
94                 connection = null;
95                 throw ioe;
96             }
97         }
98
99         public long skip(long n) throws IOException JavaDoc {
100             try {
101                 return in!=null?super.skip(n):0;
102             } catch (IOException JavaDoc ioe) {
103                 connection = null;
104                 throw ioe;
105             }
106         }
107
108     }
109     
110     protected URL JavaDoc url;
111     protected InputStream JavaDoc in;
112     protected long lastModified;
113     protected long offset;
114
115     protected HttpResponse(URL JavaDoc url) {
116         
117         this.url = url;
118     }
119
120     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
121         if (in == null && url != null) {
122             if (connection == null || offset > 0)
123                 connection = url.openConnection();
124             if (offset > 0)
125                 connection.setRequestProperty("Range", "bytes=" + offset + "-"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
126
try {
127                 in = new MonitoringInputStream(connection.getInputStream(), connection);
128             } catch (IOException JavaDoc ioe) {
129                 connection = null;
130                 throw ioe;
131             }
132             checkOffset();
133         }
134         return in;
135     }
136     
137     public void close() {
138         if( null != in ) {
139                 try {
140                     in.close();
141                 } catch (IOException JavaDoc e) {
142                 }
143                 in = null;
144         }
145         if (connection!=null) {
146             ((HttpURLConnection JavaDoc)connection).disconnect();
147             connection = null;
148         }
149     }
150     
151     /**
152      * @see IResponse#getInputStream(IProgressMonitor)
153      */

154     public InputStream JavaDoc getInputStream(IProgressMonitor monitor)
155         throws IOException JavaDoc, CoreException, TooManyOpenConnectionsException {
156         if (in == null && url != null) {
157             if (connection == null || offset > 0)
158                 connection = url.openConnection();
159             if (offset > 0)
160                 connection.setRequestProperty("Range", "bytes=" + offset + "-"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
161

162             if (monitor != null) {
163                 try {
164                     this.in = new MonitoringInputStream(openStreamWithCancel(
165                             connection, monitor), connection);
166                 } catch (IOException JavaDoc ioe) {
167                     connection = null;
168                     throw ioe;
169                 }
170             } else {
171                 try {
172                     this.in = new MonitoringInputStream(connection
173                             .getInputStream(), connection);
174                 } catch (IOException JavaDoc ioe) {
175                     connection = null;
176                     throw ioe;
177                 }
178             }
179             // this can also be run inside a monitoring thread, but it is safe
180
// to
181
// just call it now, if the input stream has already been obtained
182
checkOffset();
183             if (connection != null) {
184                 this.lastModified = connection.getLastModified();
185             }
186         }
187         return in;
188     }
189
190     public long getContentLength() {
191         if (connection != null)
192             return connection.getContentLength();
193         return 0;
194     }
195
196     public int getStatusCode() {
197         if (connection == null)
198             try {
199                 connection = url.openConnection();
200             } catch (IOException JavaDoc e) {
201             }
202         if (connection != null) {
203             try {
204                 return ((HttpURLConnection JavaDoc) connection).getResponseCode();
205             } catch (IOException JavaDoc e) {
206                 UpdateCore.warn("", e); //$NON-NLS-1$
207
}
208         }
209         return IStatusCodes.HTTP_OK;
210     }
211
212     public String JavaDoc getStatusMessage() {
213         if (connection != null) {
214             try {
215                 return ((HttpURLConnection JavaDoc) connection).getResponseMessage();
216             } catch (IOException JavaDoc e) {
217                 UpdateCore.warn("", e); //$NON-NLS-1$
218
}
219         }
220         return ""; //$NON-NLS-1$
221
}
222
223     public long getLastModified() {
224         if (lastModified == 0) {
225             if (connection == null)
226                 try {
227                     connection = url.openConnection();
228                 } catch (IOException JavaDoc e) {
229                 }
230             if (connection != null)
231                 lastModified = connection.getLastModified();
232         }
233         return lastModified;
234     }
235
236     public void setOffset(long offset) {
237         this.offset = offset;
238     }
239     private void checkOffset() throws IOException JavaDoc {
240         if (offset == 0)
241             return;
242         String JavaDoc range = connection.getHeaderField("Content-Range"); //$NON-NLS-1$
243
//System.out.println("Content-Range=" + range);
244
if (range == null) {
245             //System.err.println("Server does not support ranges");
246
throw new IOException JavaDoc(Messages.HttpResponse_rangeExpected);
247         } else if (!range.startsWith("bytes " + offset + "-")) { //$NON-NLS-1$ //$NON-NLS-2$
248
//System.err.println("Server returned wrong range");
249
throw new IOException JavaDoc(Messages.HttpResponse_wrongRange);
250         }
251     }
252 }
253
Popular Tags