ResolverDirectHTTPpublic 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");
|
Fields Summary |
---|
static Logger | log{@link java.util.logging} logging facility | static final String[] | propertiesField properties[] | private static final int | HttpProxyHostField HttpProxyHost | private static final int | HttpProxyPortField HttpProxyPort | private static final int | HttpProxyUserField HttpProxyUser | private static final int | HttpProxyPassField HttpProxyPass | private static final int | HttpBasicUserField HttpProxyUser | private static final int | HttpBasicPassField HttpProxyPass |
Methods Summary |
---|
public boolean | engineCanResolve(org.w3c.dom.Attr uri, java.lang.String BaseURI)We resolve http URIs without fragment...
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()
return (String[]) ResolverDirectHTTP.properties.clone();
| public com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput | engineResolve(org.w3c.dom.Attr uri, java.lang.String BaseURI)Method resolve
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.URI | getNewURI(java.lang.String uri, java.lang.String BaseURI)
if ((BaseURI == null) || "".equals(BaseURI)) {
return new URI(uri);
}
return new URI(new URI(BaseURI), uri);
|
|