FileDocCategorySizeDatePackage
ResolverDirectHTTP.javaAPI DocJava SE 6 API11162Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.utils.resolver.implementations

ResolverDirectHTTP

public class ResolverDirectHTTP extends ResourceResolverSpi
A simple ResourceResolver for HTTP requests. This class handles only 'pure' HTTP URIs which means without a fragment. The Fragment handling is done by the {@link ResolverFragment} class.
If the user has a corporate HTTP proxy which is to be used, the usage can be switched on by setting properties for the resolver:
resourceResolver.setProperty("http.proxy.host", "proxy.company.com");
resourceResolver.setProperty("http.proxy.port", "8080");

// if we need a password for the proxy
resourceResolver.setProperty("http.proxy.username", "proxyuser3");
resourceResolver.setProperty("http.proxy.password", "secretca");
author
$Author: mullan $
see
Java Tip 42: Write Java apps that work with proxy-based firewalls
see
SUN J2SE docs for network properties
see
The JAVA FAQ Question 9.5: How do I make Java work with a proxy server? $todo$ the proxy behaviour seems not to work; if a on-existing proxy is set, it works ?!?

Fields Summary
static Logger
log
{@link java.util.logging} logging facility
static final String[]
properties
Field properties[]
private static final int
HttpProxyHost
Field HttpProxyHost
private static final int
HttpProxyPort
Field HttpProxyPort
private static final int
HttpProxyUser
Field HttpProxyUser
private static final int
HttpProxyPass
Field HttpProxyPass
private static final int
HttpBasicUser
Field HttpProxyUser
private static final int
HttpBasicPass
Field HttpProxyPass
Constructors Summary
Methods Summary
public booleanengineCanResolve(org.w3c.dom.Attr uri, java.lang.String BaseURI)
We resolve http URIs without fragment...

param
uri
param
BaseURI
return
true if can be resolved

      if (uri == null) {
         if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "quick fail, uri == null");

         return false;
      }

      String uriNodeValue = uri.getNodeValue();

      if (uriNodeValue.equals("") || (uriNodeValue.charAt(0)=='#")) {
         if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "quick fail for empty URIs and local ones");

         return false;
      }

      if (true)
      	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "I was asked whether I can resolve " + uriNodeValue);

      if ( uriNodeValue.startsWith("http:") ||
				 BaseURI.startsWith("http:")) {
         if (true)
         	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "I state that I can resolve " + uriNodeValue);

         return true;
      }

      if (true)
      	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "I state that I can't resolve " + uriNodeValue);

      return false;
   
public java.lang.String[]engineGetPropertyKeys()

inheritDoc

      return (String[]) ResolverDirectHTTP.properties.clone();
   
public com.sun.org.apache.xml.internal.security.signature.XMLSignatureInputengineResolve(org.w3c.dom.Attr uri, java.lang.String BaseURI)
Method resolve

param
uri
param
BaseURI
throws
ResourceResolverException
return
$todo$ calculate the correct URI from the attribute and the BaseURI


                            
        
             

      try {
         boolean useProxy = false;
         String proxyHost =
            engineGetProperty(ResolverDirectHTTP
               .properties[ResolverDirectHTTP.HttpProxyHost]);
         String proxyPort =
            engineGetProperty(ResolverDirectHTTP
               .properties[ResolverDirectHTTP.HttpProxyPort]);

         if ((proxyHost != null) && (proxyPort != null)) {
            useProxy = true;
         }

         String oldProxySet =
            (String) System.getProperties().get("http.proxySet");
         String oldProxyHost =
            (String) System.getProperties().get("http.proxyHost");
         String oldProxyPort =
            (String) System.getProperties().get("http.proxyPort");
         boolean switchBackProxy = ((oldProxySet != null)
                                    && (oldProxyHost != null)
                                    && (oldProxyPort != null));

         // switch on proxy usage
         if (useProxy) {
            if (true)
            	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "Use of HTTP proxy enabled: " + proxyHost + ":"
                      + proxyPort);
            System.getProperties().put("http.proxySet", "true");
            System.getProperties().put("http.proxyHost", proxyHost);
            System.getProperties().put("http.proxyPort", proxyPort);
         }

         // calculate new URI
         URI uriNew = getNewURI(uri.getNodeValue(), BaseURI);

         // if the URI contains a fragment, ignore it
         URI uriNewNoFrag = new URI(uriNew);

         uriNewNoFrag.setFragment(null);

         URL url = new URL(uriNewNoFrag.toString());
         URLConnection urlConnection = url.openConnection();

         {

            // set proxy pass
            String proxyUser =
               engineGetProperty(ResolverDirectHTTP
                  .properties[ResolverDirectHTTP.HttpProxyUser]);
            String proxyPass =
               engineGetProperty(ResolverDirectHTTP
                  .properties[ResolverDirectHTTP.HttpProxyPass]);

            if ((proxyUser != null) && (proxyPass != null)) {
               String password = proxyUser + ":" + proxyPass;
               String encodedPassword = Base64.encode(password.getBytes());

               // or was it Proxy-Authenticate ?
               urlConnection.setRequestProperty("Proxy-Authorization",
                                                encodedPassword);
            }
         }

         {

            // check if Basic authentication is required
            String auth = urlConnection.getHeaderField("WWW-Authenticate");

            if (auth != null) {

               // do http basic authentication
               if (auth.startsWith("Basic")) {
                  String user =
                     engineGetProperty(ResolverDirectHTTP
                        .properties[ResolverDirectHTTP.HttpBasicUser]);
                  String pass =
                     engineGetProperty(ResolverDirectHTTP
                        .properties[ResolverDirectHTTP.HttpBasicPass]);

                  if ((user != null) && (pass != null)) {
                     urlConnection = url.openConnection();

                     String password = user + ":" + pass;
                     String encodedPassword =
                        Base64.encode(password.getBytes());

                     // set authentication property in the http header
                     urlConnection.setRequestProperty("Authorization",
                                                      "Basic "
                                                      + encodedPassword);
                  }
               }
            }
         }

         String mimeType = urlConnection.getHeaderField("Content-Type");
         InputStream inputStream = urlConnection.getInputStream();
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte buf[] = new byte[4096];
         int read = 0;
         int summarized = 0;

         while ((read = inputStream.read(buf)) >= 0) {
            baos.write(buf, 0, read);

            summarized += read;
         }

         if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "Fetched " + summarized + " bytes from URI "
                   + uriNew.toString());

         XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());

         // XMLSignatureInput result = new XMLSignatureInput(inputStream);
         result.setSourceURI(uriNew.toString());
         result.setMIMEType(mimeType);

         // switch off proxy usage
         if (switchBackProxy) {
            System.getProperties().put("http.proxySet", oldProxySet);
            System.getProperties().put("http.proxyHost", oldProxyHost);
            System.getProperties().put("http.proxyPort", oldProxyPort);
         }

         return result;
      } catch (MalformedURLException ex) {
         throw new ResourceResolverException("generic.EmptyMessage", ex, uri,
                                             BaseURI);
      } catch (IOException ex) {
         throw new ResourceResolverException("generic.EmptyMessage", ex, uri,
                                             BaseURI);
      }
   
private com.sun.org.apache.xml.internal.utils.URIgetNewURI(java.lang.String uri, java.lang.String BaseURI)


      if ((BaseURI == null) || "".equals(BaseURI)) {
         return new URI(uri);
      }
      return new URI(new URI(BaseURI), uri);