[kaffe] urlConnection / media types patch

jrandom jrandom at i2p.net
Wed May 5 15:07:02 PDT 2004


Hi y'all,

Here's a quick patch [1] for URLConnection to handle explicit media types
[2].  It currently only modifies the classname requested for the
Class.forName, and does not support having individual content handlers for
all of the various media types, but it lets this fetch URL.getContent() on
my apache instance.  (almost - we needed a handler for text/html [3])

The sun (1.4.2_04) impl has the URLConnection.getContentType()  return [4]
the full content type (including media type), so this is compatible with
their behavior.

[1] the patch:

Index: URLConnection.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/net/URLConnection.java,v
retrieving revision 1.18
diff -u -r1.18 URLConnection.java
--- URLConnection.java  22 Mar 2004 11:24:50 -0000      1.18
+++ URLConnection.java  5 May 2004 20:21:32 -0000
@@ -428,8 +428,14 @@
     // Then try our default class
     try
       {
+        String typeClass = type.replace('/', '.');
+
+        // deal with "Content-Type: text/html; charset=ISO-8859-1"
+        int mediaTypeBegin = typeClass.indexOf(';');
+        if (mediaTypeBegin >= 1)
+            typeClass = typeClass.substring(0, mediaTypeBegin);
         Class cls = Class.forName("gnu.java.net.content." +
-                                  type.replace('/', '.'));
+                                  typeClass);

         Object obj = cls.newInstance();


[2] http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17

[3] Kaffe doesn't have any handler defined for text/html, so I also had to
copy the one for text/plain to
libraries/javalib/gnu/java/net/content/text/html.java (after modifying the
javadoc to s/plain/html/), which seems reasonable since HTML content just
returns the stream.  Then after updating libraries/javalib/Makefile (line
722), libraries/javalib/profiles/allatonce/all.files, and
profiles/default/rest.files to include it, rebuilding the lib, and running
the test, it worked.

There's probably some mod to be done to Makefile.in or Makefile.am or
Makefile.am.in or something instead of just Makefile, but I don't have
automake installed so I can't test that.

[4] "Type: [text/html; charset=ISO-8859-1]" from:

   import java.net.*;

   public class testType {
    public static void main(String args[]) {
     try {
      URL url = new URL("http://localhost/");
      URLConnection con = url.openConnection();
      con.connect();
      Object o = con.getContent();
      String type = con.getContentType();
      System.out.println("Type: [" + type + "]");
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }






More information about the kaffe mailing list