KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > LocatorURL


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  */

6
7 package com.hp.hpl.jena.util;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.* ;
13
14 import org.apache.commons.logging.*;
15
16 /** Location files named by a URL
17  *
18  * @author Andy Seaborne
19  * @version $Id: LocatorURL.java,v 1.7 2005/02/21 12:18:57 andy_seaborne Exp $
20  */

21
22 public class LocatorURL implements Locator
23 {
24     static Log log = LogFactory.getLog(LocatorURL.class) ;
25     static final String JavaDoc acceptHeader = "application/rdf+xml,application/xml;q=0.9,*/*;q=0.5" ;
26
27     public InputStream JavaDoc open(String JavaDoc filenameOrURI)
28     {
29         if ( ! hasScheme(filenameOrURI, "http:")
30             // && ! hasScheme(filenameOrURI, "file:") // Leave a filelocator to handle this.
31
)
32         {
33             if ( FileManager.logAllLookups && log.isTraceEnabled() )
34                 log.trace("Not found: "+filenameOrURI) ;
35             return null;
36         }
37         
38         try
39         {
40             URL url = new URL(filenameOrURI);
41             URLConnection conn = (HttpURLConnection) url.openConnection();
42             conn.setRequestProperty("Accept", acceptHeader) ;
43             conn.setRequestProperty("Accept-Charset", "utf-8,*") ;
44             conn.setDoInput(true) ;
45             conn.setDoOutput(false) ;
46             // Default is true. See javadoc for HttpURLConnection
47
//((HttpURLConnection)conn).setInstanceFollowRedirects(true) ;
48
conn.connect() ;
49             InputStream JavaDoc in = new BufferedInputStream JavaDoc(conn.getInputStream());
50             
51             if ( in == null )
52             {
53                 if ( FileManager.logAllLookups && log.isTraceEnabled() )
54                     log.trace("Not found: "+filenameOrURI) ;
55                 return null ;
56             }
57             if ( FileManager.logAllLookups && log.isTraceEnabled() )
58                 log.trace("Found: "+filenameOrURI) ;
59             return in;
60         }
61         catch (java.io.FileNotFoundException JavaDoc ex)
62         {
63             if ( FileManager.logAllLookups && log.isTraceEnabled() )
64                 log.trace("LocatorURL: not found: "+filenameOrURI) ;
65             return null ;
66         }
67         catch (MalformedURLException ex)
68         {
69             log.warn("Malformed URL: " + filenameOrURI);
70             return null;
71         }
72         catch (IOException JavaDoc ex)
73         {
74             if ( ex instanceof ConnectException )
75             {
76                 if ( FileManager.logAllLookups && log.isTraceEnabled() )
77                     log.trace("LocatorURL: not found: "+filenameOrURI) ;
78             }
79             else
80                 log.warn("IO Exception opening URL: " + filenameOrURI+" "+ex.getMessage());
81             return null;
82         }
83     }
84     public String JavaDoc getName() { return "LocatorURL" ; }
85     
86     private boolean hasScheme(String JavaDoc uri, String JavaDoc scheme)
87     {
88         String JavaDoc actualScheme = getScheme(uri) ;
89         if ( actualScheme == null )
90             return false ;
91         return actualScheme.equalsIgnoreCase(scheme) ;
92     }
93     
94     // Not perfect - but we support Java 1.3 (as of August 2004)
95
private String JavaDoc getScheme(String JavaDoc uri)
96     {
97         int ch = uri.indexOf(':') ;
98         if ( ch < 0 )
99             return null ;
100         
101         // Includes the :
102
return uri.substring(0,ch+1) ;
103     }
104
105 }
106 /*
107  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
108  * All rights reserved.
109  *
110  * Redistribution and use in source and binary forms, with or without
111  * modification, are permitted provided that the following conditions
112  * are met:
113  * 1. Redistributions of source code must retain the above copyright
114  * notice, this list of conditions and the following disclaimer.
115  * 2. Redistributions in binary form must reproduce the above copyright
116  * notice, this list of conditions and the following disclaimer in the
117  * documentation and/or other materials provided with the distribution.
118  * 3. The name of the author may not be used to endorse or promote products
119  * derived from this software without specific prior written permission.
120  *
121  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
122  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
123  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
124  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
125  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
126  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
127  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
128  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
130  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131  */
Popular Tags