diff options
author | mcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4> | 2010-05-05 15:29:01 +0000 |
---|---|---|
committer | mcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4> | 2010-05-05 15:29:01 +0000 |
commit | b1c8641a63a67e3c64d948f9e8dce5c01e11e2dd (patch) | |
tree | 0883f08a408f89f758e9a1be629232e3dd055c3a /utils/src/main/java/at/gv/egiz/bku | |
parent | 83a9b613836910f7edc370c2fe60fa2268dc4461 (diff) | |
download | mocca-b1c8641a63a67e3c64d948f9e8dce5c01e11e2dd.tar.gz mocca-b1c8641a63a67e3c64d948f9e8dce5c01e11e2dd.tar.bz2 mocca-b1c8641a63a67e3c64d948f9e8dce5c01e11e2dd.zip |
Merged feature branch mocca-1.2.13-id@r724 back to trunk.
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@725 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
Diffstat (limited to 'utils/src/main/java/at/gv/egiz/bku')
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/ClasspathURLStreamHandler.java (renamed from utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java) | 30 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/DebugInputStream.java | 59 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java | 5 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java | 70 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java | 41 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java | 150 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java | 96 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java | 27 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerImpl.java | 108 | ||||
-rw-r--r-- | utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java | 6 |
10 files changed, 288 insertions, 304 deletions
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java b/utils/src/main/java/at/gv/egiz/bku/utils/ClasspathURLStreamHandler.java index 7272f1bb..1cd46fb5 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/ClasspathURLStreamHandler.java @@ -1,5 +1,5 @@ /* -* Copyright 2008 Federal Chancellery Austria and +* Copyright 2009 Federal Chancellery Austria and * Graz University of Technology * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,13 +14,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package at.gv.egiz.bku.utils.urldereferencer;
-
-import java.io.InputStream;
-
-public interface FormDataURLSupplier {
- public static final String PROPERTY_KEY_NAME = "at.gv.egiz.bku.util.urldereferencer.FormDataURLSupplier";
-
- public InputStream getFormData(String aParameterName);
- public String getFormDataContentType(String aParameterName);
-}
\ No newline at end of file + +package at.gv.egiz.bku.utils; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +public class ClasspathURLStreamHandler extends URLStreamHandler { + + @Override + protected URLConnection openConnection(URL u) throws IOException { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + URL resource = classLoader.getResource(u.getPath()); + return resource.openConnection(); + } + +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/DebugInputStream.java b/utils/src/main/java/at/gv/egiz/bku/utils/DebugInputStream.java new file mode 100644 index 00000000..b65baf5b --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/bku/utils/DebugInputStream.java @@ -0,0 +1,59 @@ +/* +* Copyright 2009 Federal Chancellery Austria and +* Graz University of Technology +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package at.gv.egiz.bku.utils; + +import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class DebugInputStream extends FilterInputStream { + + private ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + public DebugInputStream(InputStream in) { + super(in); + } + + /* (non-Javadoc) + * @see java.io.FilterInputStream#read() + */ + @Override + public int read() throws IOException { + int b = super.read(); + buffer.write(b); + return b; + } + + /* (non-Javadoc) + * @see java.io.FilterInputStream#read(byte[], int, int) + */ + @Override + public int read(byte[] b, int off, int len) throws IOException { + int l = super.read(b, off, len); + if (l > 0) { + buffer.write(b, off, l); + } + return l; + } + + public byte[] getBufferedBytes() { + return buffer.toByteArray(); + } + +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java b/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java index f0504697..574d17a9 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java @@ -17,7 +17,7 @@ package at.gv.egiz.bku.utils.binding;
public enum Protocol {
- HTTP("http"), HTTPS("https");
+ HTTP("http"), HTTPS("https"), SAML("saml");
private String name;
@@ -35,6 +35,9 @@ public enum Protocol { }
if (HTTPS.toString().equalsIgnoreCase(protocol)) {
return HTTPS;
+ } + if (SAML.toString().equalsIgnoreCase(protocol)) { + return SAML; }
return null;
}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java index dfe7d5e6..51288a9f 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java @@ -17,7 +17,6 @@ package at.gv.egiz.bku.utils.urldereferencer; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.InvalidParameterException; @@ -26,86 +25,55 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class HTTPURLProtocolHandlerImpl implements URLProtocolHandler { - private static Log log = LogFactory.getLog(HTTPURLProtocolHandlerImpl.class); + private final Logger log = LoggerFactory.getLogger(HTTPURLProtocolHandlerImpl.class); public final static String HTTP = "http"; public final static String HTTPS = "https"; - public final static String FORMDATA = "formdata"; - public final static String[] PROTOCOLS = { HTTP, HTTPS, FORMDATA }; + + public final static String[] PROTOCOLS = { HTTP, HTTPS }; private HostnameVerifier hostnameVerifier; private SSLSocketFactory sslSocketFactory; - public StreamData dereference(String aUrl, URLDereferencerContext aContext) + public StreamData dereference(String url) throws IOException { - String urlString = aUrl.toLowerCase().trim(); - if (urlString.startsWith(FORMDATA)) { - log.debug("Requested to dereference a formdata url"); - return dereferenceFormData(aUrl, aContext); - } - - URL url = new URL(aUrl); - if ((!HTTP.equalsIgnoreCase(url.getProtocol()) && (!HTTPS - .equalsIgnoreCase(url.getProtocol())))) { - throw new InvalidParameterException("Url " + aUrl + " not supported"); + URL u = new URL(url); + if ((!HTTP.equalsIgnoreCase(u.getProtocol()) && (!HTTPS + .equalsIgnoreCase(u.getProtocol())))) { + throw new InvalidParameterException("Url " + url + " not supported"); } - return dereferenceHTTP(url); + return dereferenceHTTP(u); } protected StreamData dereferenceHTTP(URL url) throws IOException { - log.debug("Dereferencing url: " + url); + log.info("Dereferencing URL: '{}'.", url); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); if (httpConn instanceof HttpsURLConnection) { - log.trace("Detected ssl connection"); + log.trace("Detected ssl connection."); HttpsURLConnection https = (HttpsURLConnection) httpConn; if (sslSocketFactory != null) { - log.debug("Setting custom ssl socket factory for ssl connection"); + log.debug("Setting custom ssl socket factory for ssl connection."); https.setSSLSocketFactory(sslSocketFactory); } else { - log.trace("No custom socket factory set"); + log.trace("No custom socket factory set."); } if (hostnameVerifier != null) { - log.debug("Setting custom hostname verifier"); + log.debug("Setting custom hostname verifier."); https.setHostnameVerifier(hostnameVerifier); } } else { - log.trace("No secure connection with: "+url+ " class="+httpConn.getClass()); + log.trace("No secure connection with: {} class={}.", url, httpConn.getClass()); } - log.trace("Successfully opened connection"); + log.trace("Successfully opened connection."); return new StreamData(url.toString(), httpConn.getContentType(), httpConn .getInputStream()); } - /** - * - * @param aUrl - * @param aContext - * @return - * @throws IOException if the data cannot be found or reading the stream failed. - */ - protected StreamData dereferenceFormData(String aUrl, - URLDereferencerContext aContext) throws IOException { - log.debug("Dereferencing formdata url: " + aUrl); - String[] parts = aUrl.split(":", 2); - FormDataURLSupplier supplier = (FormDataURLSupplier) aContext - .getProperty(FormDataURLSupplier.PROPERTY_KEY_NAME); - if (supplier == null) { - throw new NullPointerException( - "No FormdataUrlSupplier found in provided context"); - } - String contentType = supplier.getFormDataContentType(parts[1]); - InputStream is = supplier.getFormData(parts[1]); - if (is != null) { - return new StreamData(aUrl, contentType, is); - } - throw new IOException("Cannot dereference url: formdata not found"); - } - @Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { this.hostnameVerifier = hostnameVerifier; @@ -116,4 +84,4 @@ public class HTTPURLProtocolHandlerImpl implements URLProtocolHandler { this.sslSocketFactory = socketFactory; } -}
\ No newline at end of file +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java deleted file mode 100644 index e9da9c81..00000000 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java +++ /dev/null @@ -1,41 +0,0 @@ -/* -* Copyright 2008 Federal Chancellery Austria and -* Graz University of Technology -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package at.gv.egiz.bku.utils.urldereferencer;
-
-public class SimpleFormDataContextImpl implements URLDereferencerContext {
- protected FormDataURLSupplier formdata;
-
- /**
- *
- * @param formdata must not be null
- */
- public SimpleFormDataContextImpl(FormDataURLSupplier formdata) {
- if (formdata == null) {
- throw new NullPointerException("FormdataURLSupplier must not be null");
- }
- this.formdata = formdata;
- }
-
- @Override
- public Object getProperty(Object key) {
- if (key.equals(FormDataURLSupplier.PROPERTY_KEY_NAME)) {
- return formdata;
- }
- return null;
- }
-
-}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java index 2d11010e..0dccbe8f 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java @@ -14,83 +14,73 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package at.gv.egiz.bku.utils.urldereferencer;
-
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-
-import javax.xml.transform.Source;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.URIResolver;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- * Adapter to make the Urldereferencer work as URIResolver for
- * Stylesheettransforms.
- *
- * @author wbauer
- *
- */
-public class URIResolverAdapter implements URIResolver {
-
- private static Log log = LogFactory.getLog(URIResolverAdapter.class);
-
- private URLDereferencer urlDereferencer;
- private URLDereferencerContext ctx;
-
- /**
- *
- * @param deferecencer
- * must not be null
- * @param ctx may be null
- */
- public URIResolverAdapter(URLDereferencer deferecencer,
- URLDereferencerContext ctx) {
- if (deferecencer == null) {
- throw new NullPointerException("Urlderefencer must not be set to null");
- }
- this.urlDereferencer = deferecencer;
- this.ctx = ctx;
- }
-
- @Override
- public Source resolve(String href, String base) throws TransformerException {
- log.debug("Resolving href: "+href+" base: "+base);
- try {
- URI baseUri = null;
- URI hrefUri = new URI(href);
- if (base != null) {
- baseUri = new URI(base);
- }
- URI abs;
- if (baseUri != null) {
- abs = baseUri.resolve(hrefUri);
- } else {
- abs = hrefUri;
- }
- if (!abs.isAbsolute()) {
- throw new TransformerException("Only absolute URLs are supported");
- }
- return new StreamSource(urlDereferencer.dereference(abs.toString(), ctx)
- .getStream());
- } catch (URISyntaxException e) {
- throw new TransformerException("Cannot resolve URI: base:" + base
- + " href:" + href, e);
- } catch (IOException iox) {
- throw new TransformerException("Cannot resolve URI: base:" + base
- + " href:" + href, iox);
- }
- }
-
- public URLDereferencerContext getCtx() {
- return ctx;
- }
-
- public void setCtx(URLDereferencerContext ctx) {
- this.ctx = ctx;
- }
-}
+package at.gv.egiz.bku.utils.urldereferencer; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.stream.StreamSource; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Adapter to make the Urldereferencer work as URIResolver for + * Stylesheettransforms. + * + * @author wbauer + * + */ +public class URIResolverAdapter implements URIResolver { + + private final Logger log = LoggerFactory.getLogger(URIResolverAdapter.class); + + private URLDereferencer urlDereferencer; + + /** + * + * @param deferecencer + * must not be null + * @param ctx may be null + */ + public URIResolverAdapter(URLDereferencer deferecencer) { + if (deferecencer == null) { + throw new NullPointerException("Urlderefencer must not be set to null"); + } + this.urlDereferencer = deferecencer; + } + + @Override + public Source resolve(String href, String base) throws TransformerException { + log.debug("Resolving href: {} base: {}", href, base); + try { + URI baseUri = null; + URI hrefUri = new URI(href); + if (base != null) { + baseUri = new URI(base); + } + URI abs; + if (baseUri != null) { + abs = baseUri.resolve(hrefUri); + } else { + abs = hrefUri; + } + if (!abs.isAbsolute()) { + throw new TransformerException("Only absolute URLs are supported"); + } + return new StreamSource(urlDereferencer.dereference(abs.toString()) + .getStream()); + } catch (URISyntaxException e) { + throw new TransformerException("Cannot resolve URI: base:" + base + + " href:" + href, e); + } catch (IOException iox) { + throw new TransformerException("Cannot resolve URI: base:" + base + + " href:" + href, iox); + } + } + +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java index 7361ec26..468610e4 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java @@ -1,5 +1,5 @@ /* -* Copyright 2008 Federal Chancellery Austria and +* Copyright 2009 Federal Chancellery Austria and * Graz University of Technology * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,95 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package at.gv.egiz.bku.utils.urldereferencer;
-
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.util.HashMap;
-import java.util.Map;
-import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLSocketFactory; -
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- * Class used to dereference (external URLs).
- *
- * @author wbauer
- *
- */
-public class URLDereferencer {
-
- private static Log log = LogFactory.getLog(URLDereferencer.class);
-
- private static URLDereferencer instance = new URLDereferencer();
-
- private Map<String, URLProtocolHandler> handlerMap = new HashMap<String, URLProtocolHandler>();
+package at.gv.egiz.bku.utils.urldereferencer; - private HostnameVerifier hostnameVerifier; - private SSLSocketFactory sslSocketFactory; -
- private URLDereferencer() {
- registerHandlers();
- }
-
- /**
- *
- * @param aUrl
- * must not be null
- * @param aContext
- * @throws MalformedURLException
- * if the protocol is not supported - * @throws IOException if the url cannot be dereferenced (e.g. formdata not provided) - *
- */
- public StreamData dereference(String aUrl, URLDereferencerContext aContext)
- throws IOException {
- log.debug("Looking for handler for URL: " + aUrl);
- int i = aUrl.indexOf(":");
- if (i == -1) {
- throw new MalformedURLException("Invalid url: " + aUrl);
- }
- String protocol = aUrl.substring(0, i).toLowerCase().trim();
- URLProtocolHandler handler = handlerMap.get(protocol);
- if (handler == null) {
- throw new MalformedURLException("No handler for protocol: " + protocol
- + " found");
- } - handler.setHostnameVerifier(hostnameVerifier); - handler.setSSLSocketFactory(sslSocketFactory);
- return handler.dereference(aUrl, aContext);
- }
-
- /**
- * Registers a handler for a protocol.
- *
- * @param aProtocol
- * @param aHandler
- * may be set to null to disable this protocol
- */
- public void registerHandler(String aProtocol, URLProtocolHandler aHandler) {
- handlerMap.put(aProtocol.toLowerCase(), aHandler);
- }
-
- public static URLDereferencer getInstance() {
- return instance;
- }
-
- protected void registerHandlers() {
- URLProtocolHandler handler = new HTTPURLProtocolHandlerImpl();
- for (String proto : HTTPURLProtocolHandlerImpl.PROTOCOLS) {
- handlerMap.put(proto, handler);
- }
- } +import java.io.IOException; + +public interface URLDereferencer { - public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { - this.hostnameVerifier = hostnameVerifier; - } + public StreamData dereference(String aUrl) throws IOException; - public void setSSLSocketFactory(SSLSocketFactory socketFactory) { - this.sslSocketFactory = socketFactory; - }
-}
\ No newline at end of file +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java deleted file mode 100644 index 6befd5b3..00000000 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java +++ /dev/null @@ -1,27 +0,0 @@ -/* -* Copyright 2008 Federal Chancellery Austria and -* Graz University of Technology -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package at.gv.egiz.bku.utils.urldereferencer;
-
-public interface URLDereferencerContext {
-
- /**
- *
- * @param key
- * @return may return null
- */
- public Object getProperty(Object key);
-}
\ No newline at end of file diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerImpl.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerImpl.java new file mode 100644 index 00000000..d1a68c46 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerImpl.java @@ -0,0 +1,108 @@ +/* +* Copyright 2008 Federal Chancellery Austria and +* Graz University of Technology +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package at.gv.egiz.bku.utils.urldereferencer; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.Map; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLSocketFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class used to dereference (external URLs). + * + * @author wbauer + * + */ +public class URLDereferencerImpl implements URLDereferencer { + + private final Logger log = LoggerFactory.getLogger(URLDereferencerImpl.class); + + private static URLDereferencerImpl instance = new URLDereferencerImpl(); + + private Map<String, URLProtocolHandler> handlerMap = new HashMap<String, URLProtocolHandler>(); + + private HostnameVerifier hostnameVerifier; + private SSLSocketFactory sslSocketFactory; + + private URLDereferencerImpl() { + registerHandlers(); + } + + /** + * + * @param aUrl + * must not be null + * @throws MalformedURLException + * if the protocol is not supported + * @throws IOException if the url cannot be dereferenced (e.g. formdata not provided) + * + */ + @Override + public StreamData dereference(String aUrl) + throws IOException { + log.debug("Looking for handler for URL: {}.", aUrl); + int i = aUrl.indexOf(":"); + if (i == -1) { + throw new MalformedURLException("Invalid url: " + aUrl); + } + String protocol = aUrl.substring(0, i).toLowerCase().trim(); + URLProtocolHandler handler = handlerMap.get(protocol); + if (handler == null) { + throw new MalformedURLException("No handler for protocol: " + protocol + + " found"); + } + handler.setHostnameVerifier(hostnameVerifier); + handler.setSSLSocketFactory(sslSocketFactory); + return handler.dereference(aUrl); + } + + /** + * Registers a handler for a protocol. + * + * @param aProtocol + * @param aHandler + * may be set to null to disable this protocol + */ + public void registerHandler(String aProtocol, URLProtocolHandler aHandler) { + handlerMap.put(aProtocol.toLowerCase(), aHandler); + } + + public static URLDereferencerImpl getInstance() { + return instance; + } + + protected void registerHandlers() { + URLProtocolHandler handler = new HTTPURLProtocolHandlerImpl(); + for (String proto : HTTPURLProtocolHandlerImpl.PROTOCOLS) { + handlerMap.put(proto, handler); + } + } + + public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + } + + public void setSSLSocketFactory(SSLSocketFactory socketFactory) { + this.sslSocketFactory = socketFactory; + } +} diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java index f886bd4e..1da1ad71 100644 --- a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java +++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java @@ -17,7 +17,6 @@ package at.gv.egiz.bku.utils.urldereferencer;
import java.io.IOException;
-import java.net.MalformedURLException;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; @@ -26,12 +25,11 @@ import javax.net.ssl.SSLSocketFactory; public interface URLProtocolHandler {
/**
*
- * @param aUrl
- * @param aContext
+ * @param url
* @return the streamdata of this url or null if the url cannot be resolved.
* @throws IOException
*/
- public StreamData dereference(String aUrl, URLDereferencerContext aContext) throws IOException; + public StreamData dereference(String url) throws IOException; public void setSSLSocketFactory(SSLSocketFactory socketFactory); |