KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > url > BasicURLHandler


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * version 1.3.1
5  */

6 package fr.jayasoft.ivy.url;
7
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.net.HttpURLConnection JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLConnection JavaDoc;
16 import java.net.UnknownHostException JavaDoc;
17
18 import fr.jayasoft.ivy.util.CopyProgressListener;
19 import fr.jayasoft.ivy.util.FileUtil;
20 import fr.jayasoft.ivy.util.Message;
21
22 /**
23  * @author Xavier Hanin
24  * @author Christian Riege
25  *
26  */

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()); // do not remove, ensure IvyAuthenticator class loading!
36
}
37
38     public URLInfo getURLInfo(URL JavaDoc url) {
39         return getURLInfo(url, 0);
40     }
41     public URLInfo getURLInfo(URL JavaDoc url, int timeout) {
42         URLConnection JavaDoc con = null;
43         try {
44             con = url.openConnection();
45             if (con instanceof HttpURLConnection JavaDoc) {
46                 int status = ((HttpURLConnection JavaDoc)con).getResponseCode();
47                 if (status == HttpStatus.SC_OK) {
48                     return new URLInfo(
49                             true,
50                             ((HttpURLConnection JavaDoc)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 JavaDoc)con).getResponseMessage()+" url="+url);
58                 }else if (String.valueOf(status).startsWith("5")) {
59                     Message.error("SERVER ERROR: "+((HttpURLConnection JavaDoc)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 JavaDoc 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 JavaDoc e) {
78             Message.error("Server access Error: "+e.getMessage()+" url="+url);
79         } finally {
80             if (con instanceof HttpURLConnection JavaDoc) {
81                 ((HttpURLConnection JavaDoc)con).disconnect();
82             }
83         }
84         return UNAVAILABLE;
85     }
86     
87     public InputStream JavaDoc openStream(URL JavaDoc url) throws IOException JavaDoc {
88         URLConnection JavaDoc conn = null;
89         InputStream JavaDoc inStream = null;
90         try {
91             conn = url.openConnection();
92             inStream = conn.getInputStream();
93             ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
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 JavaDoc(outStream.toByteArray());
101         }
102         finally {
103             if (inStream != null) {
104                 inStream.close();
105             }
106             
107             if (conn != null) {
108                 if (conn instanceof HttpURLConnection JavaDoc) {
109                     //System.out.println("Closing HttpURLConnection");
110
((HttpURLConnection JavaDoc) conn).disconnect();
111                 }
112             }
113         }
114     }
115     public void download(URL JavaDoc src, File JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
116         URLConnection JavaDoc 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 JavaDoc("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 JavaDoc) {
129                     //System.out.println("Closing HttpURLConnection");
130
((HttpURLConnection JavaDoc) srcConn).disconnect();
131                 }
132             }
133         }
134     }
135 }
136
Popular Tags