1 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 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 protected URL url; 101 protected InputStream in; 103 protected URLConnection connection; 104 protected long lastModified; 105 protected long offset; 106 107 public HttpResponse(URL url) { 108 this.url = url; 110 } 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 + "-"); 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 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 + "-"); 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 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); } 192 } 193 return IStatusCodes.HTTP_OK; 194 } 195 196 public String getStatusMessage() { 197 if (connection != null) { 198 try { 199 return ((HttpURLConnection) connection).getResponseMessage(); 200 } catch (IOException e) { 201 UpdateCore.warn("", e); } 203 } 204 return ""; } 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 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 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 range = connection.getHeaderField("Content-Range"); if (range == null) { 267 throw new IOException(Messages.HttpResponse_rangeExpected); 269 } else if (!range.startsWith("bytes " + offset + "-")) { throw new IOException(Messages.HttpResponse_wrongRange); 272 } 273 } 274 } 275 | Popular Tags |