Re: [xep-support] URL resolution

From: Werner Donné (werner.donne@re.be)
Date: Thu May 01 2003 - 08:38:44 PDT

  • Next message: Werner Donné: "Re: [xep-support] URL resolution"

    John,

    You can perhaps use URNs instead of URLs to address the images. In a URN you can
    use your naming scheme. For this to work you have to provide a URLStreamHandler
    and a URLConnection, which produce the streams. In the attached example you can
    see how I added my own HTTP client.

    The next step is exposing your protocol handler to the environment. The JDK
    offers only a clumsy way to do that. You must define the system property
    "java.protocol.handler.pkgs" to be a | separated list of package names in
    which the protocol handler packages reside. Let's say, for example, that we
    want to introduce the protocol "urn". A subdirectory "urn" containing the
    handler and the connection class must exist in one of the packages specified
    in the fore-mentioned system property.

    After this URNs, will be considered valid by java.net.URL and the connection
    manipulations for it will be directed to your classes for the complete JVM.

    I think that implementing URLConnection.getInputStream() might be enough. In there
    you make your connection with the database.

    Regards,

    Werner.

    John Meyer wrote:
    > Does xep provide any hooks to use application url resolution? Due to
    > security and design considerations, our application uses and internal
    > naming system and does not directly expose any of its contents via URL.
    > Unforetunately, I can’t find any way to allow the xep pdf producer to
    > find images in our database using our naming system. For xsl
    > transformations, we use the JAXP URIResolver interface and related
    > methods. I am hoping a similar solution could be used in the H4PDF class.
    >
    >
    >
    > John Meyer
    >
    > Senior Software Engineer
    >
    > Clinician Support Technology
    >
    >
    >

    -- 
    Werner Donné  --  Re BVBA
    Engelbeekstraat 8
    B-3300 Tienen
    tel: (+32) 486 425803	e-mail: werner.donne@re.be
    
    

    package be.re.net.http;

    import be.re.io.PipedInputStream;
    import be.re.io.PipedOutputStream;
    import be.re.net.HTTPClient;
    import be.re.net.Headers;
    import be.re.net.Util;
    import be.re.util.UncheckedCapsule;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Set;

    public class Connection extends URLConnection

    {

      private InputStream in = null;
      private OutputStream out = null;
      private Headers requestHeaders = new Headers();
      private Headers responseHeaders = new Headers();

      public
      Connection(URL url)
      {
        super(url);
      }

      public void
      addRequestProperty(String key, String value)
      {
        requestHeaders.add(key, value);
      }

      public void
      connect() throws IOException
      {
        if (getIfModifiedSince() != -1)
        {
          requestHeaders.
            add("If-Modified-Since", new Date(getIfModifiedSince()).toString());
        }

        if (getDoOutput())
        {
          PipedOutputStream outPipe = new PipedOutputStream();
          final PipedInputStream inPipe = new PipedInputStream(outPipe);

          new Thread
          (
            new Runnable()
            {
              public void
              run()
              {
                try
                {
                  in =
                    HTTPClient.request
                    (
                      HTTPClient.POST,
                      getURL(),
                      inPipe,
                      requestHeaders,
                      responseHeaders,
                      null,
                      responseHeaders
                    );
                }

                catch (Throwable e)
                {
                  throw new UncheckedCapsule(e);
                }
              }
            }
          ).start();

          out = outPipe;
        }
        else
        {
          in =
            HTTPClient.request
            (
              HTTPClient.GET,
              getURL(),
              null,
              requestHeaders,
              responseHeaders,
              null,
              responseHeaders
            );
        }
      }

      public String
      getContentEncoding()
      {
        return getResponseValue("Content-Encoding");
      }

      public int
      getContentLength()
      {
        return
          getResponseValue("Content-Length") == null ?
            -1 : Integer.parseInt(getResponseValue("Content-Length"));
      }

      public String
      getContentType()
      {
        return getResponseValue("Content-Type");
      }

      public long
      getDate()
      {
        return
          getResponseValue("Date") == null ? 0 : getDate(getResponseValue("Date"));
      }

      private long
      getDate(String s)
      {
        return Util.httpDate(s);
      }

      public long
      getExpiration()
      {
        return
          getResponseValue("Expires") == null ?
            0 : getDate(getResponseValue("Expires"));
      }

      public String
      getHeaderField(int n)
      {
        return
          n >= responseHeaders.getAll().length ?
            null : responseHeaders.getAll()[n].getValue();
      }

      public String
      getHeaderField(String name)
      {
        return getResponseValue(name);
      }

      public long
      getHeaderFieldDate(String name, long def)
      {
        return
          getResponseValue(name) == null ?
            def : getDate(getResponseValue(name));
      }

      public int
      getHeaderFieldInt(String name, int def)
      {
        return
          getResponseValue(name) == null ?
            def : Integer.parseInt(getResponseValue(name));
      }

      public String
      getHeaderFieldKey(int n)
      {
        return
          n >= responseHeaders.getAll().length ?
            null : responseHeaders.getAll()[n].getName();
      }

      public Map
      getHeaderFields()
      {
        return getHeaderMap(responseHeaders);
      }

      public Map
      getHeaderMap(Headers headers)
      {
        String[] keys = getKeys(headers);
        Map result = new Hashtable();

        for (int i = 0; i < keys.length; ++i)
        {
          result.put(keys[i], getHeaderValue(headers, keys[i]));
        }

        return result;
      }

      private String
      getHeaderValue(Headers headers, String header)
      {
        String[] value = headers.get(header);
        String result = "";

        for (int i = 0; i < value.length; ++i)
        {
          result += result.equals("") ? value[i] : (", " + value[i]);
        }

        return result.equals("") ? null : result;
      }

      public InputStream
      getInputStream() throws IOException
      {
        connect();

        return in;
      }

      private String[]
      getKeys(Headers headers)
      {
        Headers.Header[] all = headers.getAll();
        Set result = new HashSet();

        for (int i = 0; i < all.length; ++i)
        {
          result.add(all[i].getName());
        }

        return (String[]) result.toArray(new String[result.size()]);
      }

      public long
      getLastModified()
      {
        return
          getResponseValue("Last-Modified") == null ?
            0 : getDate(getResponseValue("Last-Modified"));
      }

      public OutputStream
      getOutputStream() throws IOException
      {
        setDoOutput(true);
        connect();

        return out;
      }

      private String
      getRequestValue(String header)
      {
        return getHeaderValue(requestHeaders, header);
      }

      private String
      getResponseValue(String header)
      {
        return getHeaderValue(responseHeaders, header);
      }

      public Map
      getRequestProperties()
      {
        return getHeaderMap(requestHeaders);
      }

      public String
      getRequestProperty(String key)
      {
        return getRequestValue(key);
      }

      public boolean
      getUseCaches()
      {
        return false;
      }

      public void
      setRequestProperty(String key, String value)
      {
        requestHeaders.set(key, value);
      }

    } // Connection

    package be.re.net.http;

    import java.io.IOException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLStreamHandler;

    public class Handler extends URLStreamHandler

    {

      protected int
      getDefaultPort()
      {
        return 80;
      }

      protected URLConnection
      openConnection(URL url) throws IOException
      {
        return new Connection(url);
      }

    } // Handler

    -------------------
    (*) To unsubscribe, send a message with words 'unsubscribe xep-support'
    in the body of the message to majordomo@renderx.com from the address
    you are subscribed from.
    (*) By using the Service, you expressly agree to these Terms of Service http://www.renderx.com/tos.html



    This archive was generated by hypermail 2.1.5 : Thu May 01 2003 - 08:29:52 PDT