1 11 package org.eclipse.update.internal.core.connection; 12 13 import java.io.FilterInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.HttpURLConnection ; 17 import java.net.URL ; 18 import java.net.URLConnection ; 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 32 private class MonitoringInputStream extends FilterInputStream { 33 private URLConnection connection; 34 35 public MonitoringInputStream(InputStream in, URLConnection connection) { 36 super(in); 37 this.connection = connection; 38 } 39 40 public int available() throws IOException { 41 try { 42 return in!=null?super.available():0; 43 } catch (IOException ioe) { 44 connection = null; 45 throw ioe; 46 } 47 } 48 49 public void close() throws IOException { 50 try { 51 if (in!=null) 52 super.close(); 53 if (connection instanceof HttpURLConnection ) { 54 ((HttpURLConnection )connection).disconnect(); 55 } 56 } catch (IOException ioe) { 57 connection = null; 58 throw ioe; 59 } 60 } 61 62 public int read() throws IOException { 63 try { 64 return in!=null?super.read():-1; 65 } catch (IOException ioe) { 66 connection = null; 67 throw ioe; 68 } 69 } 70 71 public synchronized void reset() throws IOException { 72 try { 73 if (in!=null) 74 super.reset(); 75 } catch (IOException ioe) { 76 connection = null; 77 throw ioe; 78 } 79 } 80 81 public int read(byte[] b) throws IOException { 82 try { 83 return in!=null?super.read(b):-1; 84 } catch (IOException ioe) { 85 connection = null; 86 throw ioe; 87 } 88 } 89 90 public int read(byte[] b, int off, int len) throws IOException { 91 try { 92 return in!=null?super.read(b, off, len):-1; 93 } catch (IOException ioe) { 94 connection = null; 95 throw ioe; 96 } 97 } 98 99 public long skip(long n) throws IOException { 100 try { 101 return in!=null?super.skip(n):0; 102 } catch (IOException ioe) { 103 connection = null; 104 throw ioe; 105 } 106 } 107 108 } 109 110 protected URL url; 111 protected InputStream in; 112 protected long lastModified; 113 protected long offset; 114 115 protected HttpResponse(URL url) { 116 117 this.url = url; 118 } 119 120 public InputStream getInputStream() throws IOException { 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 + "-"); try { 127 in = new MonitoringInputStream(connection.getInputStream(), connection); 128 } catch (IOException 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 e) { 142 } 143 in = null; 144 } 145 if (connection!=null) { 146 ((HttpURLConnection )connection).disconnect(); 147 connection = null; 148 } 149 } 150 151 154 public InputStream getInputStream(IProgressMonitor monitor) 155 throws IOException , 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 + "-"); 162 if (monitor != null) { 163 try { 164 this.in = new MonitoringInputStream(openStreamWithCancel( 165 connection, monitor), connection); 166 } catch (IOException ioe) { 167 connection = null; 168 throw ioe; 169 } 170 } else { 171 try { 172 this.in = new MonitoringInputStream(connection 173 .getInputStream(), connection); 174 } catch (IOException ioe) { 175 connection = null; 176 throw ioe; 177 } 178 } 179 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 e) { 201 } 202 if (connection != null) { 203 try { 204 return ((HttpURLConnection ) connection).getResponseCode(); 205 } catch (IOException e) { 206 UpdateCore.warn("", e); } 208 } 209 return IStatusCodes.HTTP_OK; 210 } 211 212 public String getStatusMessage() { 213 if (connection != null) { 214 try { 215 return ((HttpURLConnection ) connection).getResponseMessage(); 216 } catch (IOException e) { 217 UpdateCore.warn("", e); } 219 } 220 return ""; } 222 223 public long getLastModified() { 224 if (lastModified == 0) { 225 if (connection == null) 226 try { 227 connection = url.openConnection(); 228 } catch (IOException 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 { 240 if (offset == 0) 241 return; 242 String range = connection.getHeaderField("Content-Range"); if (range == null) { 245 throw new IOException (Messages.HttpResponse_rangeExpected); 247 } else if (!range.startsWith("bytes " + offset + "-")) { throw new IOException (Messages.HttpResponse_wrongRange); 250 } 251 } 252 } 253 | Popular Tags |