[Mulgara-svn] r1498 - trunk/src/jar/resolver-http/java/org/mulgara/resolver/http

pag at mulgara.org pag at mulgara.org
Fri Feb 13 20:21:07 UTC 2009


Author: pag
Date: 2009-02-13 12:21:05 -0800 (Fri, 13 Feb 2009)
New Revision: 1498

Modified:
   trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java
Log:
Fixed bug where asking for the mimetype would close a stream. Also avoids an extra HEAD method when not needed.

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 20:19:57 UTC (rev 1497)
+++ trunk/src/jar/resolver-http/java/org/mulgara/resolver/http/HttpContent.java	2009-02-13 20:21:05 UTC (rev 1498)
@@ -75,6 +75,9 @@
   /** The URI version of the URL */
   private URI httpUri;
 
+  /** The MIME type of this data */
+  private MimeType contentType = null;
+
   /**
    * A map containing any format-specific blank node mappings from previous
    * parses of this file.
@@ -330,35 +333,21 @@
    * from the HTTP <code>Content-Type</code> header.
    */
   public MimeType getContentType() throws NotModifiedException {
-
-    MimeType mimeType = null;
-    HeadMethod method = null;
-    String contentType = null;
-
-    try {
-
-      // obtain connection and retrieve the headers
-      method = (HeadMethod) establishConnection(HEAD);
-      Header header = method.getResponseHeader("Content-Type");
-      if (header != null) {
-        contentType = header.getValue();
-        mimeType = new MimeType(contentType);
-        if (logger.isInfoEnabled()) {
-          logger.info("Obtain content type " + mimeType + "  from " + httpUri);
-        }
+    // if we don't have the type from the connection already, then establish one
+    if (contentType == null) {
+      HttpMethod method = null;
+      try {
+        method = establishConnection(HEAD);
+        contentType = readMimeType(method);
+      } catch (IOException e) {
+        logger.info("Unable to obtain content type for " + httpUri);
+      } finally {
+        // we're the only one to have needed this connection, so drop it
+        if (method != null) method.releaseConnection();
+        if (connection != null) connection.close();
       }
-    } catch (MimeTypeParseException e) {
-      logger.warn("Unable to parse " + contentType + " as a content type for "
-          + httpUri);
-    } catch (IOException e) {
-      logger.info("Unable to obtain content type for " + httpUri);
-    } catch (java.lang.IllegalStateException e) {
-      logger.info("Unable to obtain content type for " + httpUri);
-    } finally {
-      if (method != null) method.releaseConnection();
-      if (connection != null) connection.close();
     }
-    return mimeType;
+    return contentType;
   }
 
   /**
@@ -367,7 +356,6 @@
    * @return The URI for the actual content
    */
   public URI getURI() {
-
     return httpUri;
   }
 
@@ -387,6 +375,7 @@
 
     // obtain connection and retrieve the headers
     method = (GetMethod) establishConnection(GET);
+    contentType = readMimeType(method);
     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);
@@ -415,4 +404,33 @@
   public String getURIString() {
     return httpUri.toString();
   }
+
+
+  /**
+   * Read the mime type. Should only be done if the Mime type is not already available
+   * as this will close the connection.
+   * @return The MimeType for the URL.
+   * @throws NotModifiedException if the content validates against the cache
+   */
+  private MimeType readMimeType(HttpMethod method) throws NotModifiedException {
+    MimeType result = null;
+    String contentType = null;
+
+    try {
+      // obtain connection and retrieve the headers
+      Header header = method.getResponseHeader("Content-Type");
+      if (header != null) {
+        contentType = header.getValue();
+        result = new MimeType(contentType);
+        if (logger.isInfoEnabled()) {
+          logger.info("Obtain content type " + result + "  from " + httpUri);
+        }
+      }
+    } catch (MimeTypeParseException e) {
+      logger.warn("Unable to parse " + contentType + " as a content type for " + httpUri);
+    } catch (java.lang.IllegalStateException e) {
+      logger.info("Unable to obtain content type for " + httpUri);
+    }
+    return result;
+  }
 }




More information about the Mulgara-svn mailing list