[Mulgara-svn] r1485 - in trunk/src/jar: resolver/java/org/mulgara/resolver resolver-file/java/org/mulgara/resolver/file resolver-http/java/org/mulgara/resolver/http resolver-jar/java/org/mulgara/resolver/jar resolver-spi/java/org/mulgara/content
pag at mulgara.org
pag at mulgara.org
Fri Feb 13 06:55:22 UTC 2009
Author: pag
Date: 2009-02-12 22:55:21 -0800 (Thu, 12 Feb 2009)
New Revision: 1485
Modified:
trunk/src/jar/resolver-file/java/org/mulgara/resolver/file/FileContent.java
trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java
trunk/src/jar/resolver-jar/java/org/mulgara/resolver/jar/JarContent.java
trunk/src/jar/resolver-spi/java/org/mulgara/content/Content.java
trunk/src/jar/resolver/java/org/mulgara/resolver/StreamContent.java
Log:
Added a mechanism to print URIs when the URI may now be set to null due to a stream being present
Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/StreamContent.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/StreamContent.java 2009-02-13 06:51:03 UTC (rev 1484)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/StreamContent.java 2009-02-13 06:55:21 UTC (rev 1485)
@@ -53,24 +53,23 @@
* Technology Inc</a>
* @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
*/
-class StreamContent implements Content
-{
+class StreamContent implements Content {
+
/**
* A map containing any format-specific blank node mappings from previous
* parses of this file.
*/
private final Map<Object,BlankNode> blankNodeMap = new HashMap<Object,BlankNode>();
- /**
- * The wrapped uri to assist with InputStream content detection.
- */
+ /** The wrapped uri to assist with InputStream content detection. */
private URI uri;
- /**
- * The wrapped inputStream containing the source content
- */
+ /** The wrapped inputStream containing the source content. */
private InputStream inputStream;
-
+
+ /** The content type of the stream, if provided. */
+ private MimeType contentType;
+
//
// Constructor
//
@@ -80,8 +79,7 @@
* A URI must be supplied to help determine the content
* of the inputstream.
*/
- StreamContent(InputStream inputStream, URI uri)
- {
+ StreamContent(InputStream inputStream, URI uri) {
// Validate "file" parameter
if (uri == null) {
throw new IllegalArgumentException("Null \"uri\" parameter");
@@ -91,15 +89,33 @@
}
// Initialize fields
this.uri = uri;
+ this.contentType = null;
this.inputStream = inputStream;
}
+ /**
+ * Wrap a {@link InputStream} as {@link Content}.
+ * The content type must be provided.
+ */
+ StreamContent(InputStream inputStream, MimeType contentType) {
+ // Validate "file" parameter
+ if (contentType == null) {
+ throw new IllegalArgumentException("Null \"contentType\" parameter");
+ }
+ if (inputStream == null) {
+ throw new IllegalArgumentException("Null \"inputStream\" parameter");
+ }
+ // Initialize fields
+ this.uri = null;
+ this.contentType = contentType;
+ this.inputStream = inputStream;
+ }
+
//
// Methods implementing Content
//
- public Map<Object,BlankNode> getBlankNodeMap()
- {
+ public Map<Object,BlankNode> getBlankNodeMap() {
return blankNodeMap;
}
@@ -107,18 +123,15 @@
* @return {@inheritDoc}; always returns <code>null</code> because Java
* {@link File}s don't have any inherent MIME type
*/
- public MimeType getContentType()
- {
- return null;
+ public MimeType getContentType() {
+ return contentType;
}
/**
* This URI will help determine the contents of the inputStream.
- *
* @see org.mulgara.content.Content#getURI()
*/
- public URI getURI()
- {
+ public URI getURI() {
return uri;
}
@@ -127,8 +140,7 @@
*
* @see org.mulgara.content.Content#newInputStream()
*/
- public InputStream newInputStream() throws IOException
- {
+ public InputStream newInputStream() throws IOException {
return inputStream;
}
@@ -140,8 +152,12 @@
*
* @throws IOException always
*/
- public OutputStream newOutputStream() throws IOException
- {
+ public OutputStream newOutputStream() throws IOException {
throw new IOException("Stream resolver can't perform output");
}
+
+ /** @see org.mulgara.content.Content#getURIString() */
+ public String getURIString() {
+ return uri == null ? "<<stream>>" : uri.toString();
+ }
}
Modified: trunk/src/jar/resolver-file/java/org/mulgara/resolver/file/FileContent.java
===================================================================
--- trunk/src/jar/resolver-file/java/org/mulgara/resolver/file/FileContent.java 2009-02-13 06:51:03 UTC (rev 1484)
+++ trunk/src/jar/resolver-file/java/org/mulgara/resolver/file/FileContent.java 2009-02-13 06:55:21 UTC (rev 1485)
@@ -30,7 +30,6 @@
// Java 2 standard packages
import java.io.*;
import java.net.URI;
-import java.net.URL;
import java.util.*;
import java.util.zip.*;
@@ -54,8 +53,8 @@
* Technology Inc</a>
* @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
*/
-public class FileContent implements Content
-{
+public class FileContent implements Content {
+
/**
* A map containing any format-specific blank node mappings from previous
* parses of this file.
@@ -79,12 +78,9 @@
/**
* Wrap a {@link File} as {@link Content}.
*/
- public FileContent(File file)
- {
+ public FileContent(File file) {
// Validate "file" parameter
- if (file == null) {
- throw new IllegalArgumentException("Null \"file\" parameter");
- }
+ if (file == null) throw new IllegalArgumentException("Null \"file\" parameter");
// Initialize fields
this.file = file;
@@ -94,8 +90,7 @@
// Methods implementing Content
//
- public Map<Object,BlankNode> getBlankNodeMap()
- {
+ public Map<Object,BlankNode> getBlankNodeMap() {
return blankNodeMap;
}
@@ -103,26 +98,22 @@
* @return {@inheritDoc}; always returns <code>null</code> because Java
* {@link File}s don't have any inherent MIME type
*/
- public MimeType getContentType()
- {
+ public MimeType getContentType() {
return null;
}
- public URI getURI()
- {
+ public URI getURI() {
return file.toURI();
}
- public InputStream newInputStream() throws IOException
- {
+ public InputStream newInputStream() throws IOException {
InputStream inputStream = new FileInputStream(file);
// Guess at transfer encoding (compression scheme) based on file extension
if (file.getName().toLowerCase().endsWith(".gz")) {
// The file name ends with ".gz", so assume it's a gzip'ed file
inputStream = new GZIPInputStream(inputStream);
- }
- else if (file.getName().toLowerCase().endsWith(".zip")) {
+ } else if (file.getName().toLowerCase().endsWith(".zip")) {
// The file name ends with ".zip", so assume it's a zip'ed file
inputStream = new ZipInputStream(inputStream);
@@ -135,16 +126,14 @@
return inputStream;
}
- public OutputStream newOutputStream() throws IOException
- {
+ public OutputStream newOutputStream() throws IOException {
OutputStream outputStream = new FileOutputStream(file);
// Guess at transfer encoding (compression scheme) based on file extension
if (file.getName().toLowerCase().endsWith(".gz")) {
// The file name ends with ".gz", so assume it's a gzip'ed file
outputStream = new GZIPOutputStream(outputStream);
- }
- else if (file.getName().toLowerCase().endsWith(".zip")) {
+ } else if (file.getName().toLowerCase().endsWith(".zip")) {
// The file name ends with ".zip", so assume it's a zip'ed file
outputStream = new ZipOutputStream(outputStream);
@@ -152,10 +141,15 @@
((ZipOutputStream) outputStream).putNextEntry(new ZipEntry(
"dummy-specified-by-" + getClass()
));
-
}
assert outputStream != null;
return outputStream;
}
+
+ /** @see org.mulgara.content.Content#getURIString() */
+ public String getURIString() {
+ return file.toURI().toString();
+ }
+
}
Modified: trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java
===================================================================
--- trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java 2009-02-13 06:51:03 UTC (rev 1484)
+++ trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java 2009-02-13 06:55:21 UTC (rev 1485)
@@ -81,47 +81,31 @@
*/
private Map<Object,BlankNode> blankNodeMap = new HashMap<Object,BlankNode>();
- /**
- * Connection host <code>host</code>
- */
+ /** Connection host <code>host</code> */
private String host;
- /**
- * <code>port</code> to make connection to
- */
+ /** <code>port</code> to make connection to */
private int port;
- /**
- * Schema for connection <code>schema</code>
- */
+ /** Schema for connection <code>schema</code> */
private String schema;
- /**
- * A container for HTTP attributes that may persist from request to request
- */
+ /** A container for HTTP attributes that may persist from request to request */
private HttpState state = new HttpState();
- /**
- * Http connection
- */
+ /** Http connection */
private HttpConnection connection = null;
/** Http connection manager. For setting up and cleaning after connections. */
HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
- /**
- * To obtain the http headers only
- */
+ /** To obtain the http headers only */
private static final int HEAD = 1;
- /**
- * To obtain the response body
- */
+ /** To obtain the response body */
private static final int GET = 2;
- /**
- * Max. number of redirects
- */
+ /** Max. number of redirects */
private static final int MAX_NO_REDIRECTS = 10;
public HttpContent(URI uri) throws URISyntaxException, MalformedURLException {
@@ -136,25 +120,18 @@
* the content of
*/
public HttpContent(URL url) throws URISyntaxException {
-
// Validate "url" parameter
- if (url == null) {
- throw new IllegalArgumentException("Null \"url\" parameter");
- }
-
+ if (url == null) throw new IllegalArgumentException("Null \"url\" parameter");
initialiseSettings(url);
}
/**
* Initialise the basic settings for a connection
*
- * @param url
- * location of source
- * @throws URISyntaxException
- * invalid URI
+ * @param url location of source
+ * @throws URISyntaxException invalid URI
*/
private void initialiseSettings(URL url) throws URISyntaxException {
-
// Convert the URL to a Uri
httpUri = new URI(url.toExternalForm());
@@ -162,7 +139,6 @@
host = httpUri.getHost();
port = httpUri.getPort();
schema = httpUri.getScheme();
-
}
/**
@@ -171,19 +147,16 @@
* @return The node map used to ensure that blank nodes are consistent
*/
public Map<Object,BlankNode> getBlankNodeMap() {
-
return blankNodeMap;
}
/**
* Obtain the approrpriate connection method
*
- * @param methodType
- * can be HEAD or GET
+ * @param methodType can be HEAD or GET
* @return HttpMethodBase method
*/
private HttpMethod getConnectionMethod(int methodType) {
-
if (methodType != GET && methodType != HEAD) {
throw new IllegalArgumentException("Invalid method base supplied for connection");
}
@@ -229,11 +202,6 @@
method = new GetMethod(httpUri.toString());
}
- // No longer a useful operation
-// if (connection.isProxied() && connection.isSecure()) {
-// method = new ConnectMethod(method);
-// }
-
// manually follow redirects due to the
// strictness of http client implementation
@@ -246,18 +214,13 @@
/**
* Obtain a valid connection and follow redirects if necessary.
*
- * @param methodType
- * request the headders (HEAD) or body (GET)
+ * @param methodType request the headders (HEAD) or body (GET)
* @return valid connection method. Can be null.
* @throws NotModifiedException if the content validates against the cache
* @throws IOException if there's difficulty communicating with the web site
*/
- private HttpMethod establishConnection(int methodType)
- throws IOException, NotModifiedException
- {
- if (logger.isDebugEnabled()) {
- logger.debug("Establishing connection");
- }
+ private HttpMethod establishConnection(int methodType) throws IOException, NotModifiedException {
+ if (logger.isDebugEnabled()) logger.debug("Establishing connection");
HttpMethod method = this.getConnectionMethod(methodType);
Header header = null;
@@ -279,9 +242,7 @@
*/
// Make the request
- if (logger.isDebugEnabled()) {
- logger.debug("Executing HTTP request");
- }
+ if (logger.isDebugEnabled()) logger.debug("Executing HTTP request");
connection.open();
method.execute(state, connection);
if (logger.isDebugEnabled()) {
@@ -418,9 +379,7 @@
*/
public InputStream newInputStream() throws IOException, NotModifiedException {
- if (logger.isDebugEnabled()) {
- logger.debug("Getting new input stream for " + httpUri);
- }
+ if (logger.isDebugEnabled()) logger.debug("Getting new input stream for " + httpUri);
// Create an input stream by opening the URL's input stream
GetMethod method = null;
@@ -429,12 +388,8 @@
// obtain connection and retrieve the headers
method = (GetMethod) establishConnection(GET);
inputStream = method.getResponseBodyAsStream();
- if (inputStream == null) {
- throw new IOException("Unable to obtain inputstream from " + httpUri);
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Got new input stream for " + httpUri);
- }
+ if (inputStream == null) throw new IOException("Unable to obtain inputstream from " + httpUri);
+ if (logger.isDebugEnabled()) logger.debug("Got new input stream for " + httpUri);
return inputStream;
}
@@ -456,4 +411,8 @@
|| status == HttpStatus.SC_SEE_OTHER);
}
+ /** @see org.mulgara.content.Content#getURIString() */
+ public String getURIString() {
+ return httpUri.toString();
+ }
}
Modified: trunk/src/jar/resolver-jar/java/org/mulgara/resolver/jar/JarContent.java
===================================================================
--- trunk/src/jar/resolver-jar/java/org/mulgara/resolver/jar/JarContent.java 2009-02-13 06:51:03 UTC (rev 1484)
+++ trunk/src/jar/resolver-jar/java/org/mulgara/resolver/jar/JarContent.java 2009-02-13 06:55:21 UTC (rev 1485)
@@ -66,9 +66,7 @@
*/
private Map<Object,BlankNode> blankNodeMap = new HashMap<Object,BlankNode>();
- /**
- * The wrapped URL.
- */
+ /** The wrapped URL. */
private URL url;
/** The URI version of the URL */
@@ -88,11 +86,8 @@
public JarContent(URL url) throws URISyntaxException {
// Validate "url" parameter
- if (url == null) {
+ if (url == null) throw new IllegalArgumentException("Null \"url\" parameter");
- throw new IllegalArgumentException("Null \"url\" parameter");
- }
-
// Store the URL this class represents
this.url = url;
@@ -106,19 +101,15 @@
* @return The node map used to ensure that blank nodes are consistent
*/
public Map<Object,BlankNode> getBlankNodeMap() {
-
return blankNodeMap;
}
public MimeType getContentType() {
-
try {
return new MimeType(url.openConnection().getContentType());
- }
- catch (IOException e) {
+ } catch (IOException e) {
return null;
- }
- catch (MimeTypeParseException e) {
+ } catch (MimeTypeParseException e) {
return null;
}
}
@@ -129,7 +120,6 @@
* @return The URI for the actual content
*/
public URI getURI() {
-
return urlUri;
}
@@ -141,20 +131,21 @@
* @throws IOException
*/
public InputStream newInputStream() throws IOException {
-
// Create an input stream by opening the URL's input stream
InputStream inputStream = url.openStream();
-
assert inputStream != null;
-
return inputStream;
}
/**
* @throws IOException always (not implemented)
*/
- public OutputStream newOutputStream() throws IOException
- {
+ public OutputStream newOutputStream() throws IOException {
throw new IOException("Output of JAR content not implemented");
}
+
+ /** @see org.mulgara.content.Content#getURIString() */
+ public String getURIString() {
+ return urlUri.toString();
+ }
}
Modified: trunk/src/jar/resolver-spi/java/org/mulgara/content/Content.java
===================================================================
--- trunk/src/jar/resolver-spi/java/org/mulgara/content/Content.java 2009-02-13 06:51:03 UTC (rev 1484)
+++ trunk/src/jar/resolver-spi/java/org/mulgara/content/Content.java 2009-02-13 06:55:21 UTC (rev 1485)
@@ -50,12 +50,10 @@
* @version $Revision: 1.8 $
* @modified $Date: 2005/01/05 04:58:50 $ @maintenanceAuthor $Author: newmana $
* @company <a href="mailto:info at tucanatech.com">Tucana Technology</a>
- * @copyright © 2004 <a href="http://www.tucanatech.com/">Tucana
- * Technology Inc</a>
+ * @copyright © 2004 <a href="http://www.tucanatech.com/">Tucana Technology Inc</a>
* @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
*/
-public interface Content
-{
+public interface Content {
/**
* A map attached to the 'scope' of the content object containing a
* mapping from ContentHandler specific identifiers to blank nodes from
@@ -100,4 +98,10 @@
* @throws IOException if the stream can't be obtained
*/
public OutputStream newOutputStream() throws IOException, ModifiedException;
+
+ /**
+ * Gets a string representation of the URI, or something suitable if no URI is available.
+ * @return A string containing a URI or a description of an alternative.
+ */
+ public String getURIString();
}
More information about the Mulgara-svn
mailing list