KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import java.io.*;
14 import java.net.*;
15
16 import org.eclipse.core.runtime.*;
17
18 public class HttpResponse implements Response {
19     /**
20      * Monitored InputStream. Upon IOException, discards
21      * connection so it is not resused.
22      *
23      */

24     private class MonitoringInputStream extends FilterInputStream {
25         InputStream in;
26
27         public MonitoringInputStream(InputStream in) {
28             super(in);
29             this.in = in;
30         }
31
32         public int available() throws IOException {
33             try {
34                 return super.available();
35             } catch (IOException ioe) {
36                 connection = null;
37                 throw ioe;
38             }
39         }
40
41         public void close() throws IOException {
42             try {
43                 super.close();
44             } catch (IOException ioe) {
45                 connection = null;
46                 throw ioe;
47             }
48         }
49
50         public int read() throws IOException {
51             try {
52                 return super.read();
53             } catch (IOException ioe) {
54                 connection = null;
55                 throw ioe;
56             }
57         }
58
59         public synchronized void reset() throws IOException {
60             try {
61                 super.reset();
62             } catch (IOException ioe) {
63                 connection = null;
64                 throw ioe;
65             }
66         }
67
68         public int read(byte[] b) throws IOException {
69             try {
70                 return super.read(b);
71             } catch (IOException ioe) {
72                 connection = null;
73                 throw ioe;
74             }
75         }
76
77         public int read(byte[] b, int off, int len) throws IOException {
78             try {
79                 return super.read(b, off, len);
80             } catch (IOException ioe) {
81                 connection = null;
82                 throw ioe;
83             }
84         }
85
86         public long skip(long n) throws IOException {
87             try {
88                 return super.skip(n);
89             } catch (IOException ioe) {
90                 connection = null;
91                 throw ioe;
92             }
93         }
94
95     }
96     
97     private static final long POLLING_INTERVAL = 200;
98 // private static final String ECLIPSE_DOWNLOADS = "http://www.eclipse.org/downloads/download.php?r=1&file=/eclipse";
99
// private static final String ECLIPSE_UPDATES="http://update.eclipse.org";
100
protected URL url;
101 // protected URL originalURL;
102
protected InputStream in;
103     protected URLConnection connection;
104     protected long lastModified;
105     protected long offset;
106
107     public HttpResponse(URL url) {
108 // this.originalURL = url;
109
this.url = url;
110 // String urlstring = url.toExternalForm();
111
// if (urlstring.startsWith(ECLIPSE_UPDATES))
112
// try {
113
// this.url = new URL(ECLIPSE_DOWNLOADS + urlstring.substring(ECLIPSE_UPDATES.length()));
114
// } catch (MalformedURLException e) {
115
// this.url = url;
116
// }
117
}
118
119     public InputStream getInputStream() throws IOException {
120         if (in == null && url != null) {
121             if (connection == null || offset > 0)
122                 connection = url.openConnection();
123             if (offset > 0)
124                 connection.setRequestProperty("Range", "bytes=" + offset + "-"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125
try {
126                 in = new MonitoringInputStream(connection.getInputStream());
127             } catch (IOException ioe) {
128                 connection = null;
129                 throw ioe;
130             }
131             checkOffset();
132         }
133         return in;
134     }
135     /**
136      * @see Response#getInputStream(IProgressMonitor)
137      */

138     public InputStream getInputStream(IProgressMonitor monitor)
139         throws IOException, CoreException {
140         if (in == null && url != null) {
141             if (connection == null || offset > 0)
142                 connection = url.openConnection();
143             if (offset > 0)
144                 connection.setRequestProperty("Range", "bytes=" + offset + "-"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
145

146             if (monitor != null) {
147                 try {
148                     this.in = new MonitoringInputStream(openStreamWithCancel(
149                             connection, monitor));
150                 } catch (IOException ioe) {
151                     connection = null;
152                     throw ioe;
153                 }
154             } else {
155                 try {
156                     this.in = new MonitoringInputStream(connection
157                             .getInputStream());
158                 } catch (IOException ioe) {
159                     connection = null;
160                     throw ioe;
161                 }
162             }
163             // this can also be run inside a monitoring thread, but it is safe
164
// to
165
// just call it now, if the input stream has already been obtained
166
checkOffset();
167             if (in != null) {
168                 this.lastModified = connection.getLastModified();
169             }
170         }
171         return in;
172     }
173
174     public long getContentLength() {
175         if (connection != null)
176             return connection.getContentLength();
177         return 0;
178     }
179
180     public int getStatusCode() {
181         if (connection == null)
182             try {
183                 connection = url.openConnection();
184             } catch (IOException e) {
185             }
186         if (connection != null) {
187             try {
188                 return ((HttpURLConnection) connection).getResponseCode();
189             } catch (IOException e) {
190                 UpdateCore.warn("", e); //$NON-NLS-1$
191
}
192         }
193         return IStatusCodes.HTTP_OK;
194     }
195
196     public String JavaDoc getStatusMessage() {
197         if (connection != null) {
198             try {
199                 return ((HttpURLConnection) connection).getResponseMessage();
200             } catch (IOException e) {
201                 UpdateCore.warn("", e); //$NON-NLS-1$
202
}
203         }
204         return ""; //$NON-NLS-1$
205
}
206
207     public long getLastModified() {
208         if (lastModified == 0) {
209             if (connection == null)
210                 try {
211                     connection = url.openConnection();
212                 } catch (IOException e) {
213                 }
214             if (connection != null)
215                 lastModified = connection.getLastModified();
216         }
217         return lastModified;
218     }
219
220     private InputStream openStreamWithCancel(
221         URLConnection urlConnection,
222         IProgressMonitor monitor)
223         throws IOException, CoreException {
224         ConnectionThreadManager.StreamRunnable runnable =
225             new ConnectionThreadManager.StreamRunnable(urlConnection);
226         Thread JavaDoc t =
227             UpdateCore.getPlugin().getConnectionManager().createThread(
228                 runnable);
229         t.start();
230         InputStream is = null;
231         try {
232             for (;;) {
233                 if (monitor.isCanceled()) {
234                     runnable.disconnect();
235                     connection = null;
236                     break;
237                 }
238                 if (runnable.getInputStream() != null) {
239                     is = runnable.getInputStream();
240                     break;
241                 }
242                 if (runnable.getException() != null) {
243                     if (runnable.getException() instanceof IOException)
244                         throw (IOException) runnable.getException();
245                     else
246                         throw new CoreException(new Status(IStatus.ERROR,
247                                 UpdateCore.getPlugin().getBundle()
248                                         .getSymbolicName(), IStatus.OK,
249                                 runnable.getException().getMessage(), runnable
250                                         .getException()));
251                 }
252                 t.join(POLLING_INTERVAL);
253             }
254         } catch (InterruptedException JavaDoc e) {
255         }
256         return is;
257     }
258     public void setOffset(long offset) {
259         this.offset = offset;
260     }
261     private void checkOffset() throws IOException {
262         if (offset == 0)
263             return;
264         String JavaDoc range = connection.getHeaderField("Content-Range"); //$NON-NLS-1$
265
//System.out.println("Content-Range=" + range);
266
if (range == null) {
267             //System.err.println("Server does not support ranges");
268
throw new IOException(Messages.HttpResponse_rangeExpected);
269         } else if (!range.startsWith("bytes " + offset + "-")) { //$NON-NLS-1$ //$NON-NLS-2$
270
//System.err.println("Server returned wrong range");
271
throw new IOException(Messages.HttpResponse_wrongRange);
272         }
273     }
274 }
275
Popular Tags