KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > URLResourceRetriever


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * URLResourceRetriever.java
22  *
23  * Created on January 9, 2006, 10:47 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.HttpURLConnection JavaDoc;
34 import java.net.Proxy JavaDoc;
35 import java.net.ProxySelector JavaDoc;
36 import java.net.URI JavaDoc;
37 import java.net.URISyntaxException JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.net.URLConnection JavaDoc;
40 import java.util.HashMap JavaDoc;
41
42 /**
43  *
44  * @author girix
45  */

46 public class URLResourceRetriever implements ResourceRetriever{
47     
48     private static final String JavaDoc URI_SCHEME = "http"; //NOI18N
49
/** Creates a new instance of FileResourceRetriever */
50     public URLResourceRetriever() {
51     }
52     
53     public boolean accept(String JavaDoc baseAddr, String JavaDoc currentAddr) throws URISyntaxException JavaDoc {
54         URI JavaDoc currURI = new URI JavaDoc(currentAddr);
55         if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase(URI_SCHEME)))
56             return true;
57         if(baseAddr != null){
58             if(!currURI.isAbsolute()){
59                 URI JavaDoc baseURI = new URI JavaDoc(baseAddr);
60                 if(baseURI.getScheme().equalsIgnoreCase(URI_SCHEME))
61                     return true;
62             }
63         }
64         return false;
65     }
66     
67     public HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> retrieveDocument(String JavaDoc baseAddress,
68             String JavaDoc documentAddress) throws IOException JavaDoc,URISyntaxException JavaDoc{
69         
70         String JavaDoc effAddr = getEffectiveAddress(baseAddress, documentAddress);
71         if(effAddr == null)
72             return null;
73         URI JavaDoc currURI = new URI JavaDoc(effAddr);
74         HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> result = null;
75         
76         InputStream JavaDoc is = getInputStreamOfURL(currURI.toURL(), ProxySelector.
77                 getDefault().select(currURI).get(0));
78         result = new HashMap JavaDoc<String JavaDoc, InputStream JavaDoc>();
79         result.put(effectiveURL.toString(), is);
80         return result;
81         
82     }
83     
84     long streamLength = 0;
85     URL JavaDoc effectiveURL = null;
86     public InputStream JavaDoc getInputStreamOfURL(URL JavaDoc downloadURL, Proxy JavaDoc proxy) throws IOException JavaDoc{
87         
88         URLConnection JavaDoc ucn = null;
89         
90         if(Thread.currentThread().isInterrupted())
91             return null;
92         if(proxy != null)
93             ucn = downloadURL.openConnection(proxy);
94         else
95             ucn = downloadURL.openConnection();
96         HttpURLConnection JavaDoc hucn = null;
97         if(ucn instanceof HttpURLConnection JavaDoc){
98             hucn = ((HttpURLConnection JavaDoc)ucn);
99             hucn.setFollowRedirects(false);
100         }
101         if(Thread.currentThread().isInterrupted())
102             return null;
103         ucn.connect();
104         //follow HTTP redirects
105
while( (hucn.getResponseCode() == hucn.HTTP_MOVED_TEMP) ||
106                 (hucn.getResponseCode() == hucn.HTTP_MOVED_PERM) ) {
107             String JavaDoc addr = hucn.getHeaderField("Location");
108             downloadURL = new URL JavaDoc(addr);
109             if(proxy != null)
110                 ucn = downloadURL.openConnection(proxy);
111             else
112                 ucn = downloadURL.openConnection();
113             if(ucn instanceof HttpURLConnection JavaDoc){
114                 hucn = ((HttpURLConnection JavaDoc)ucn);
115                 hucn.setFollowRedirects(false);
116             }
117             if(Thread.currentThread().isInterrupted())
118                 return null;
119             ucn.connect();
120         }
121         ucn.setReadTimeout(10000);
122         InputStream JavaDoc is = ucn.getInputStream();
123         streamLength = ucn.getContentLength();
124         effectiveURL = ucn.getURL();
125         return is;
126         
127     }
128     
129     public long getStreamLength() {
130         return streamLength;
131     }
132     
133     public String JavaDoc getEffectiveAddress(String JavaDoc baseAddress, String JavaDoc documentAddress) throws IOException JavaDoc, URISyntaxException JavaDoc {
134         return resolveURL(baseAddress, documentAddress);
135     }
136     
137     public static String JavaDoc resolveURL(String JavaDoc baseAddress, String JavaDoc documentAddress) throws URISyntaxException JavaDoc{
138         URI JavaDoc currURI = new URI JavaDoc(documentAddress);
139         String JavaDoc result = null;
140         if(currURI.isAbsolute()){
141             result = currURI.toString();
142             return result;
143         }else{
144             //relative URI
145
if(baseAddress != null){
146                 URI JavaDoc baseURI = new URI JavaDoc(baseAddress);
147                 URI JavaDoc finalURI = baseURI.resolve(currURI);
148                 result = finalURI.toString();
149                 return result;
150             }else{
151                 //neither the current URI nor the base URI are absoulte. So, can not resolve this
152
//path
153
return null;
154             }
155         }
156     }
157 }
158
Popular Tags