001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.util;
008:
009: import java.io.BufferedInputStream;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.net.*;
013:
014: import org.apache.commons.logging.*;
015:
016: /** Location files named by a URL
017: *
018: * @author Andy Seaborne
019: * @version $Id: LocatorURL.java,v 1.17 2008/01/02 12:07:44 andy_seaborne Exp $
020: */
021:
022: public class LocatorURL implements Locator {
023: static Log log = LogFactory.getLog(LocatorURL.class);
024: static final String acceptHeader = "application/rdf+xml,application/xml;q=0.9,*/*;q=0.5";
025:
026: static final String[] schemeNames = { "http:", "https:" }; // Must be lower case and include the ":"
027:
028: public TypedStream open(String filenameOrURI) {
029: if (!acceptByScheme(filenameOrURI)) {
030: if (FileManager.logAllLookups && log.isTraceEnabled())
031: log.trace("Not found : " + filenameOrURI);
032: return null;
033: }
034:
035: try {
036: URL url = new URL(filenameOrURI);
037: URLConnection conn = (HttpURLConnection) url
038: .openConnection();
039: conn.setRequestProperty("Accept", acceptHeader);
040: conn.setRequestProperty("Accept-Charset", "utf-8,*");
041: conn.setDoInput(true);
042: conn.setDoOutput(false);
043: // Default is true. See javadoc for HttpURLConnection
044: //((HttpURLConnection)conn).setInstanceFollowRedirects(true) ;
045: conn.connect();
046: InputStream in = new BufferedInputStream(conn
047: .getInputStream());
048:
049: if (in == null) {
050: if (FileManager.logAllLookups && log.isTraceEnabled())
051: log.trace("Not found: " + filenameOrURI);
052: return null;
053: }
054: if (FileManager.logAllLookups && log.isTraceEnabled())
055: log.trace("Found: " + filenameOrURI);
056: return new TypedStream(in, conn.getContentType());
057: } catch (java.io.FileNotFoundException ex) {
058: if (FileManager.logAllLookups && log.isTraceEnabled())
059: log.trace("LocatorURL: not found: " + filenameOrURI);
060: return null;
061: } catch (MalformedURLException ex) {
062: log.warn("Malformed URL: " + filenameOrURI);
063: return null;
064: }
065: // IOExceptions that occur sometimes.
066: catch (java.net.UnknownHostException ex) {
067: if (FileManager.logAllLookups && log.isTraceEnabled())
068: log
069: .trace("LocatorURL: not found (UnknownHostException): "
070: + filenameOrURI);
071: return null;
072: } catch (java.net.ConnectException ex) {
073: if (FileManager.logAllLookups && log.isTraceEnabled())
074: log.trace("LocatorURL: not found (ConnectException): "
075: + filenameOrURI);
076: return null;
077: } catch (java.net.SocketException ex) {
078: if (FileManager.logAllLookups && log.isTraceEnabled())
079: log.trace("LocatorURL: not found (SocketException): "
080: + filenameOrURI);
081: return null;
082: }
083: // And IOExceptions we don't expect
084: catch (IOException ex) {
085: log.warn("I/O Exception opening URL: " + filenameOrURI
086: + " " + ex.getMessage(), ex);
087: return null;
088: }
089: }
090:
091: public boolean equals(Object other) {
092: return other instanceof LocatorURL;
093: }
094:
095: public int hashCode() {
096: return LocatorURL.class.hashCode();
097: }
098:
099: public String getName() {
100: return "LocatorURL";
101: }
102:
103: private boolean acceptByScheme(String filenameOrURI) {
104: String uriSchemeName = getScheme(filenameOrURI);
105: if (uriSchemeName == null)
106: return false;
107: uriSchemeName = uriSchemeName.toLowerCase();
108: for (int i = 0; i < schemeNames.length; i++) {
109: if (uriSchemeName.equals(schemeNames[i]))
110: return true;
111: }
112: return false;
113: }
114:
115: private boolean hasScheme(String uri, String scheme) {
116: String actualScheme = getScheme(uri);
117: if (actualScheme == null)
118: return false;
119: return actualScheme.equalsIgnoreCase(scheme);
120: }
121:
122: // Not perfect - but we support Java 1.3 (as of August 2004)
123: private String getScheme(String uri) {
124: int ch = uri.indexOf(':');
125: if (ch < 0)
126: return null;
127:
128: // Includes the :
129: return uri.substring(0, ch + 1);
130: }
131:
132: }
133: /*
134: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
135: * All rights reserved.
136: *
137: * Redistribution and use in source and binary forms, with or without
138: * modification, are permitted provided that the following conditions
139: * are met:
140: * 1. Redistributions of source code must retain the above copyright
141: * notice, this list of conditions and the following disclaimer.
142: * 2. Redistributions in binary form must reproduce the above copyright
143: * notice, this list of conditions and the following disclaimer in the
144: * documentation and/or other materials provided with the distribution.
145: * 3. The name of the author may not be used to endorse or promote products
146: * derived from this software without specific prior written permission.
147: *
148: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
149: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
151: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
152: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
153: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
154: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
155: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
156: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
157: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
158: */
|