1 6 package fr.jayasoft.ivy.url; 7 8 import java.io.ByteArrayInputStream ; 9 import java.io.ByteArrayOutputStream ; 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.net.HttpURLConnection ; 14 import java.net.URL ; 15 import java.net.URLConnection ; 16 import java.net.UnknownHostException ; 17 18 import fr.jayasoft.ivy.util.CopyProgressListener; 19 import fr.jayasoft.ivy.util.FileUtil; 20 import fr.jayasoft.ivy.util.Message; 21 22 27 public class BasicURLHandler extends AbstractURLHandler { 28 29 private static interface HttpStatus { 30 static final int SC_OK = 200; 31 static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407; 32 } 33 34 public BasicURLHandler() { 35 Message.debug("installing "+IvyAuthenticator.INSTANCE.getClass()); } 37 38 public URLInfo getURLInfo(URL url) { 39 return getURLInfo(url, 0); 40 } 41 public URLInfo getURLInfo(URL url, int timeout) { 42 URLConnection con = null; 43 try { 44 con = url.openConnection(); 45 if (con instanceof HttpURLConnection ) { 46 int status = ((HttpURLConnection )con).getResponseCode(); 47 if (status == HttpStatus.SC_OK) { 48 return new URLInfo( 49 true, 50 ((HttpURLConnection )con).getContentLength(), 51 con.getLastModified() 52 ); 53 } 54 if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { 55 Message.warn("Your proxy requires authentication."); 56 }else if (String.valueOf(status).startsWith("4")) { 57 Message.verbose("CLIENT ERROR: "+((HttpURLConnection )con).getResponseMessage()+" url="+url); 58 }else if (String.valueOf(status).startsWith("5")) { 59 Message.error("SERVER ERROR: "+((HttpURLConnection )con).getResponseMessage()+" url="+url); 60 } 61 Message.debug("HTTP response status: "+status+" url="+url); 62 } else { 63 int contentLength = con.getContentLength(); 64 if (contentLength <= 0) { 65 return UNAVAILABLE; 66 } else { 67 return new URLInfo( 68 true, 69 contentLength, 70 con.getLastModified() 71 ); 72 } 73 } 74 } catch (UnknownHostException e) { 75 Message.warn("Host " + e.getMessage() +" not found. url="+url); 76 Message.info("You probably access the destination server through a proxy server that is not well configured."); 77 } catch (IOException e) { 78 Message.error("Server access Error: "+e.getMessage()+" url="+url); 79 } finally { 80 if (con instanceof HttpURLConnection ) { 81 ((HttpURLConnection )con).disconnect(); 82 } 83 } 84 return UNAVAILABLE; 85 } 86 87 public InputStream openStream(URL url) throws IOException { 88 URLConnection conn = null; 89 InputStream inStream = null; 90 try { 91 conn = url.openConnection(); 92 inStream = conn.getInputStream(); 93 ByteArrayOutputStream outStream = new ByteArrayOutputStream (); 94 95 byte[] buffer = new byte[4096]; 96 int len; 97 while ((len = inStream.read(buffer)) > 0) { 98 outStream.write(buffer, 0, len); 99 } 100 return new ByteArrayInputStream (outStream.toByteArray()); 101 } 102 finally { 103 if (inStream != null) { 104 inStream.close(); 105 } 106 107 if (conn != null) { 108 if (conn instanceof HttpURLConnection ) { 109 ((HttpURLConnection ) conn).disconnect(); 111 } 112 } 113 } 114 } 115 public void download(URL src, File dest, CopyProgressListener l) throws IOException { 116 URLConnection srcConn = null; 117 try { 118 srcConn = src.openConnection(); 119 int contentLength = srcConn.getContentLength(); 120 FileUtil.copy(srcConn.getInputStream(), dest, l); 121 if (dest.length() != contentLength && contentLength != -1) { 122 dest.delete(); 123 throw new IOException ("Downloaded file size doesn't match expected Content Length for "+src+". Please retry."); 124 } 125 } 126 finally { 127 if (srcConn != null) { 128 if (srcConn instanceof HttpURLConnection ) { 129 ((HttpURLConnection ) srcConn).disconnect(); 131 } 132 } 133 } 134 } 135 } 136 | Popular Tags |