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 | |
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')
25 files changed, 734 insertions, 502 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); diff --git a/utils/src/main/java/at/gv/egiz/idlink/CompressedIdentityLinkFactory.java b/utils/src/main/java/at/gv/egiz/idlink/CompressedIdentityLinkFactory.java index 31e5163a..5df8657f 100644 --- a/utils/src/main/java/at/gv/egiz/idlink/CompressedIdentityLinkFactory.java +++ b/utils/src/main/java/at/gv/egiz/idlink/CompressedIdentityLinkFactory.java @@ -52,7 +52,6 @@ import at.gv.egiz.idlink.ans1.IdentityLink; import at.gv.egiz.idlink.ans1.PersonData; import at.gv.egiz.idlink.ans1.PhysicalPersonData; import at.gv.egiz.marshal.MarshallerFactory; -import at.gv.egiz.marshal.NamespacePrefixMapperImpl; import at.gv.egiz.xmldsig.KeyTypeNotSupportedException; import at.gv.egiz.xmldsig.KeyValueFactory; diff --git a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkFactory.java b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkFactory.java index 38597446..30fa0777 100644 --- a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkFactory.java +++ b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkFactory.java @@ -70,14 +70,15 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import oasis.names.tc.saml._1_0.assertion.AnyType; import oasis.names.tc.saml._1_0.assertion.AssertionType; import oasis.names.tc.saml._1_0.assertion.AttributeStatementType; import oasis.names.tc.saml._1_0.assertion.AttributeType; import oasis.names.tc.saml._1_0.assertion.SubjectConfirmationType; import oasis.names.tc.saml._1_0.assertion.SubjectType; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -88,14 +89,12 @@ import at.gv.e_government.reference.namespace.persondata._20020228_.PhysicalPers import at.gv.e_government.reference.namespace.persondata._20020228_.IdentificationType.Value; import at.gv.e_government.reference.namespace.persondata._20020228_.PersonNameType.FamilyName; import at.gv.egiz.marshal.MarshallerFactory; -import at.gv.egiz.marshal.NamespacePrefixMapperImpl; import at.gv.egiz.xmldsig.KeyTypeNotSupportedException; import at.gv.egiz.xmldsig.KeyValueFactory; -import oasis.names.tc.saml._1_0.assertion.AnyType; public class IdentityLinkFactory { - private static Log log = LogFactory.getLog(IdentityLinkFactory.class); + private final Logger log = LoggerFactory.getLogger(IdentityLinkFactory.class); /** * The instance returned by {@link #getInstance()}. @@ -380,7 +379,7 @@ public class IdentityLinkFactory { log.debug(writer.toString()); } catch (Exception e) { - log.debug(e); + log.debug("Logging assertion failed.", e); } } diff --git a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java index 8bee5fa4..a5b97a87 100644 --- a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java +++ b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java @@ -14,11 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package at.gv.egiz.idlink;
-
+package at.gv.egiz.idlink; + import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -28,11 +29,12 @@ import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -41,99 +43,94 @@ import org.w3c.dom.Text; import at.gv.egiz.bku.utils.urldereferencer.StreamData; import at.gv.egiz.bku.utils.urldereferencer.URLDereferencer; -
+ public class IdentityLinkTransformer { - protected static Log log = LogFactory.getLog(IdentityLinkTransformer.class);
-
- /**
- * The transformer factory.
- */
- private static SAXTransformerFactory factory;
-
- /**
- * The instance to be returned by {@link #getInstance()}.
- */
- private static IdentityLinkTransformer instance;
-
- /**
- * Returns an instance of this <code>IdentityLinkTransfomer</code>.
- *
- * @return an instance of this <code>IdentityLinkTransformer</code>
- */
- public static IdentityLinkTransformer getInstance() {
- if (instance == null) {
- instance = new IdentityLinkTransformer();
- factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
- }
- return instance;
- }
-
- /**
- * Sets the given <code>domainIdentifier</code> on the corresponding
- * node of the given <code>idLink</code>.
- * <p>This method may be used to cope with a flaw in the IssuerTemplate-Stylesheets
- * used to transform a <code>CompressedIdentitiyLink</code> into an
- * <code>IdentityLink</code>. Some IssuerTemplate-Stylesheets do not
- * consider the <code>pr:Type</code> element value of the
- * <code>CompressedIdentityLink</code> and render a <code>pr:Type</code>
- * element value of <code>urn:publicid:gv.at:baseid</code>
- * into the <code>IdentityLink</code> structure. This method allows to
- * set the <code>pr:Type</code> element value on the given <code>idLink</code>
- * after the transformation.
- * </p>
- *
- * @param idLink the <code>IdentityLink</code> element or one of it's ancestors.
- * Must not be <code>null</code>.
- *
- * @param domainIdentifier the value to be set for the <code>pr:Type</code> element
- *
- * @throws NullPointerException if <code>idLink</code> is <code>null</code>.
- */
- public static void setDomainIdentifier(Node idLink, String domainIdentifier) {
-
- Element element;
- if (idLink instanceof Element) {
- element = (Element) idLink;
- } else if (idLink instanceof Document) {
- element = ((Document) idLink).getDocumentElement();
- } else if (idLink != null) {
- Document document = idLink.getOwnerDocument();
- element = document.getDocumentElement();
- } else {
- throw new NullPointerException("Parameter 'idLink' must no be null.");
- }
-
- NodeList nodeList = element.getElementsByTagNameNS(
- "http://reference.e-government.gv.at/namespace/persondata/20020228#",
- "Type");
-
- for (int i = 0; i < nodeList.getLength(); i++) {
- if (nodeList.item(i) instanceof Element) {
- Element typeElement = (Element) nodeList.item(i);
- NodeList children = typeElement.getChildNodes();
- for (int j = 0; j < children.getLength(); j++) {
- if (children.item(j) instanceof Text) {
- ((Text) children.item(j)).setNodeValue(domainIdentifier);
- }
- }
- }
- }
-
- }
-
+ private final Logger log = LoggerFactory.getLogger(IdentityLinkTransformer.class); + /** - * Mapping of issuer template URIs to transformation templates. + * The transformer factory. + */ + private TransformerFactory factory = SAXTransformerFactory.newInstance(); + + /** + * The URLDereferencer used to dereference style-sheet URLs. + */ + private URLDereferencer urlDereferencer; + + /** + * @return the urlDereferencer + */ + public URLDereferencer getUrlDereferencer() { + return urlDereferencer; + } + + /** + * @param urlDereferencer the urlDereferencer to set */ - private Map<String, Templates> templates = new HashMap<String, Templates>();
-
- /**
- * Private constructor.
- */
- private IdentityLinkTransformer() {
+ public void setUrlDereferencer(URLDereferencer urlDereferencer) { + this.urlDereferencer = urlDereferencer; } /** + * Sets the given <code>domainIdentifier</code> on the corresponding + * node of the given <code>idLink</code>. + * <p>This method may be used to cope with a flaw in the IssuerTemplate-Stylesheets + * used to transform a <code>CompressedIdentitiyLink</code> into an + * <code>IdentityLink</code>. Some IssuerTemplate-Stylesheets do not + * consider the <code>pr:Type</code> element value of the + * <code>CompressedIdentityLink</code> and render a <code>pr:Type</code> + * element value of <code>urn:publicid:gv.at:baseid</code> + * into the <code>IdentityLink</code> structure. This method allows to + * set the <code>pr:Type</code> element value on the given <code>idLink</code> + * after the transformation. + * </p> + * + * @param idLink the <code>IdentityLink</code> element or one of it's ancestors. + * Must not be <code>null</code>. + * + * @param domainIdentifier the value to be set for the <code>pr:Type</code> element + * + * @throws NullPointerException if <code>idLink</code> is <code>null</code>. + */ + public static void setDomainIdentifier(Node idLink, String domainIdentifier) { + + Element element; + if (idLink instanceof Element) { + element = (Element) idLink; + } else if (idLink instanceof Document) { + element = ((Document) idLink).getDocumentElement(); + } else if (idLink != null) { + Document document = idLink.getOwnerDocument(); + element = document.getDocumentElement(); + } else { + throw new NullPointerException("Parameter 'idLink' must no be null."); + } + + NodeList nodeList = element.getElementsByTagNameNS( + "http://reference.e-government.gv.at/namespace/persondata/20020228#", + "Type"); + + for (int i = 0; i < nodeList.getLength(); i++) { + if (nodeList.item(i) instanceof Element) { + Element typeElement = (Element) nodeList.item(i); + NodeList children = typeElement.getChildNodes(); + for (int j = 0; j < children.getLength(); j++) { + if (children.item(j) instanceof Text) { + ((Text) children.item(j)).setNodeValue(domainIdentifier); + } + } + } + } + + } + + /** + * Mapping of issuer template URIs to transformation templates. + */ + private Map<String, Templates> templates = Collections.synchronizedMap(new HashMap<String, Templates>()); + + /** * Transforms an identity link <code>source</code> to <code>result</code> with * the given issuer template from the <code>stylesheetURL</code>. * @@ -154,7 +151,7 @@ public class IdentityLinkTransformer { * stylesheet fails. * @throws TransformerException * if transforming the identity link fails. - */
+ */ public void transformIdLink(String stylesheetURL, Source source, Result result) throws IOException, TransformerException { Templates templ = templates.get(stylesheetURL); @@ -168,8 +165,7 @@ public class IdentityLinkTransformer { throw new MalformedURLException("Protocol " + url.getProtocol() + " not supported for IssuerTemplate URL."); } - URLDereferencer dereferencer = URLDereferencer.getInstance(); - StreamData data = dereferencer.dereference(url.toExternalForm(), null); + StreamData data = urlDereferencer.dereference(url.toExternalForm()); log.trace("Trying to create issuer template."); templ = factory.newTemplates(new StreamSource(data.getStream())); @@ -183,6 +179,6 @@ public class IdentityLinkTransformer { transformer.transform(source, result); - }
-
-}
+ } + +} diff --git a/utils/src/main/java/at/gv/egiz/marshal/MarshallerFactory.java b/utils/src/main/java/at/gv/egiz/marshal/MarshallerFactory.java index 3ac0a86e..6cc79ae5 100644 --- a/utils/src/main/java/at/gv/egiz/marshal/MarshallerFactory.java +++ b/utils/src/main/java/at/gv/egiz/marshal/MarshallerFactory.java @@ -20,8 +20,8 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.PropertyException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -29,23 +29,22 @@ import org.apache.commons.logging.LogFactory; */ public class MarshallerFactory { - private static final Log log = LogFactory.getLog(MarshallerFactory.class); - public static Marshaller createMarshaller(JAXBContext ctx, boolean formattedOutput, boolean fragment) throws JAXBException { + Logger log = LoggerFactory.getLogger(MarshallerFactory.class); Marshaller m = ctx.createMarshaller(); try { if (formattedOutput) { - log.trace("setting marshaller property FORMATTED_OUTPUT"); + log.trace("Setting marshaller property FORMATTED_OUTPUT."); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); } if (fragment) { - log.trace("setting marshaller property FRAGMENT"); + log.trace("Setting marshaller property FRAGMENT."); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); } - log.trace("setting marshaller property NamespacePrefixMapper"); + log.trace("Setting marshaller property NamespacePrefixMapper."); m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl()); } catch (PropertyException ex) { - log.info("failed to set marshaller property: " + ex.getMessage()); + log.info("Failed to set marshaller property: {}.", ex.getMessage()); } return m; } diff --git a/utils/src/main/java/at/gv/egiz/marshal/NamespacePrefixMapperImpl.java b/utils/src/main/java/at/gv/egiz/marshal/NamespacePrefixMapperImpl.java index e0698977..dc2a5c95 100644 --- a/utils/src/main/java/at/gv/egiz/marshal/NamespacePrefixMapperImpl.java +++ b/utils/src/main/java/at/gv/egiz/marshal/NamespacePrefixMapperImpl.java @@ -21,8 +21,8 @@ import java.util.HashMap; import java.util.Map; import com.sun.xml.bind.marshaller.NamespacePrefixMapper; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -30,7 +30,7 @@ import org.apache.commons.logging.LogFactory; */ public class NamespacePrefixMapperImpl extends NamespacePrefixMapper { - private static final Log log = LogFactory.getLog(NamespacePrefixMapperImpl.class); + private final Logger log = LoggerFactory.getLogger(NamespacePrefixMapperImpl.class); protected static final Map<String, String> prefixMap = new HashMap<String, String>(); @@ -51,9 +51,7 @@ public class NamespacePrefixMapperImpl extends NamespacePrefixMapper { @Override public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { - if (log.isTraceEnabled()) { - log.trace("prefix for namespace " + namespaceUri + " requested"); - } + log.trace("Prefix for namespace {} reqested.", namespaceUri); String prefix = prefixMap.get(namespaceUri); diff --git a/utils/src/main/java/at/gv/egiz/org/apache/tomcat/util/http/AcceptLanguage.java b/utils/src/main/java/at/gv/egiz/org/apache/tomcat/util/http/AcceptLanguage.java index 231966ac..396fb6d9 100644 --- a/utils/src/main/java/at/gv/egiz/org/apache/tomcat/util/http/AcceptLanguage.java +++ b/utils/src/main/java/at/gv/egiz/org/apache/tomcat/util/http/AcceptLanguage.java @@ -35,6 +35,7 @@ import java.util.Vector; * @author Harish Prabandham * @author costin@eng.sun.com */ +@SuppressWarnings("unchecked") public class AcceptLanguage { public static Locale getLocale(String acceptLanguage) { diff --git a/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java b/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java index 5fe84aae..d3bc4f39 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java @@ -30,8 +30,6 @@ import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /* * TODO: don't set redirect stream from caller (caller does not know whether redirection will be triggered) @@ -40,7 +38,6 @@ import org.apache.commons.logging.LogFactory; public class RedirectEventFilter implements EventFilter { public static final String DEFAULT_ENCODING = "UTF-8"; - private static Log log = LogFactory.getLog(RedirectEventFilter.class); protected XMLEventWriter redirectWriter = null; protected Set<QName> redirectTriggers = null; private int depth = -1; diff --git a/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java b/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java index 08c12146..7670d024 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java @@ -23,8 +23,8 @@ package at.gv.egiz.slbinding; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLStreamException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Enables event redirection before marshalling a target of type RedirectCallback. @@ -34,7 +34,7 @@ import org.apache.commons.logging.LogFactory; */ public class RedirectUnmarshallerListener extends Unmarshaller.Listener { - private static Log log = LogFactory.getLog(RedirectUnmarshallerListener.class); + private final Logger log = LoggerFactory.getLogger(RedirectUnmarshallerListener.class); protected RedirectEventFilter eventFilter; public RedirectUnmarshallerListener(RedirectEventFilter eventFilter) { diff --git a/utils/src/main/java/at/gv/egiz/slbinding/SLUnmarshaller.java b/utils/src/main/java/at/gv/egiz/slbinding/SLUnmarshaller.java new file mode 100644 index 00000000..0a4ee9f3 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/slbinding/SLUnmarshaller.java @@ -0,0 +1,263 @@ +/* +* 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.slbinding; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; + +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.UnmarshalException; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.ValidationEvent; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import at.gv.egiz.bku.utils.ClasspathURLStreamHandler; +import at.gv.egiz.validation.ReportingValidationEventHandler; + +public class SLUnmarshaller { + + /** + * Logging facility. + */ + private final Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + + private static class DefaultSchema { + + /** + * Schema files required for Security Layer command validation. + */ + public static final String[] SCHEMA_FILES = new String[] { + "classpath:at/gv/egiz/bku/slschema/xml.xsd", + "classpath:at/gv/egiz/bku/slschema/xmldsig-core-schema.xsd", + "classpath:at/gv/egiz/bku/slschema/Core-1.2.xsd", + "classpath:at/gv/egiz/bku/slschema/Core.20020225.xsd", + "classpath:at/gv/egiz/bku/slschema/Core.20020831.xsd" }; + + private static final Schema SCHEMA; + + static { + try { + SCHEMA = createSchema(Arrays.asList(SCHEMA_FILES)); + } catch (IOException e) { + Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + log.error("Failed to load security layer schema.", e); + throw new RuntimeException(e); + } catch (SAXException e) { + Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + log.error("Failed to load security layer schema.", e); + throw new RuntimeException(e); + } + + } + } + + public static Collection<String> getDefaultSchemaUrls() { + return Collections.unmodifiableList(Arrays.asList(DefaultSchema.SCHEMA_FILES)); + } + + private static Schema createSchema(Collection<String> schemaUrls) throws SAXException, IOException { + Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + Source[] sources = new Source[schemaUrls.size()]; + Iterator<String> urls = schemaUrls.iterator(); + StringBuilder sb = null; + if (log.isDebugEnabled()) { + sb = new StringBuilder(); + sb.append("Created schema using URLs: "); + } + for (int i = 0; i < sources.length && urls.hasNext(); i++) { + String url = urls.next(); + if (url != null && url.startsWith("classpath:")) { + URL schemaUrl = new URL(null, url, new ClasspathURLStreamHandler()); + sources[i] = new StreamSource(schemaUrl.openStream()); + } else { + sources[i] = new StreamSource(url); + } + if (sb != null) { + sb.append(url); + if (urls.hasNext()) { + sb.append(", "); + } + } + } + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Schema schema = schemaFactory.newSchema(sources); + if (sb != null) { + log.debug(sb.toString()); + } + return schema; + } + + private static class DefaultContext { + + private static final String[] packageNames = { + at.buergerkarte.namespaces.securitylayer._1.ObjectFactory.class.getPackage().getName(), + org.w3._2000._09.xmldsig_.ObjectFactory.class.getPackage().getName(), + at.buergerkarte.namespaces.cardchannel.ObjectFactory.class.getPackage().getName(), + at.buergerkarte.namespaces.securitylayer._20020225_.ObjectFactory.class.getPackage().getName(), + at.buergerkarte.namespaces.securitylayer._20020831_.ObjectFactory.class.getPackage().getName() + }; + + private static final JAXBContext CONTEXT; + + static { + try { + CONTEXT = createJAXBContext(Arrays.asList(packageNames)); + } catch (JAXBException e) { + Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + log.error("Failed to setup JAXBContext security layer request/response.", e); + throw new RuntimeException(e); + } + } + + } + + public static Collection<String> getDefaultJAXBContextPackageNames() { + return Collections.unmodifiableList(Arrays.asList(DefaultContext.packageNames)); + } + + private static JAXBContext createJAXBContext(Collection<String> packageNames) throws JAXBException { + StringBuilder contextPath = new StringBuilder(); + for (String pkg : packageNames) { + if (contextPath.length() > 0) { + contextPath.append(':'); + } + contextPath.append(pkg); + } + return JAXBContext.newInstance(contextPath.toString()); + } + + /** + * Schema for Security Layer command validation. + */ + protected Schema slSchema = DefaultSchema.SCHEMA; + + /** + * The JAXBContext. + */ + protected JAXBContext jaxbContext = DefaultContext.CONTEXT; + + /** + * Returns the schema used for validation. + * + * @return the slSchema + */ + public Schema getSlSchema() { + return slSchema; + } + + /** + * Sets the schema for validation. + * + * @param slSchema the slSchema to set + */ + public void setSlSchema(Schema slSchema) { + this.slSchema = slSchema; + } + + /** + * Sets the schema created from the given {@code schemaUrls}. + * + * @param schemaUrls a collection of URLs of schema files (supports {@code classpath:} URLs) + * @throws SAXException if schema creation fails + * @throws IOException if an error occurs upon dereferencing the given {@code schemaUrls} + */ + public void setSchemaUrls(Collection<String> schemaUrls) throws SAXException, IOException { + slSchema = createSchema(schemaUrls); + } + + /** + * @return the jaxbContext + */ + public JAXBContext getJaxbContext() { + return jaxbContext; + } + + /** + * @param jaxbContext the jaxbContext to set + */ + public void setJaxbContext(JAXBContext jaxbContext) { + this.jaxbContext = jaxbContext; + } + + /** + * Sets the JAXBContext for unmarshalling using the given {@code packageNames}. + * + * @param packageNames a collection of java package names + * @throws JAXBException if creating the JAXBContext with the given {@code packageNames} fails + */ + public void setJaxbContextPackageNames(Collection<String> packageNames) throws JAXBException { + this.jaxbContext = createJAXBContext(packageNames); + } + + public Object unmarshal(Source source) throws XMLStreamException, JAXBException { + + ReportingValidationEventHandler validationEventHandler = new ReportingValidationEventHandler(); + + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLEventReader eventReader = inputFactory.createXMLEventReader(source); + RedirectEventFilter redirectEventFilter = new RedirectEventFilter(); + XMLEventReader filteredReader = inputFactory.createFilteredReader(eventReader, redirectEventFilter); + + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + unmarshaller.setEventHandler(validationEventHandler); + + unmarshaller.setListener(new RedirectUnmarshallerListener(redirectEventFilter)); + unmarshaller.setSchema(slSchema); + + Object object; + try { + log.trace("Before unmarshal()."); + object = unmarshaller.unmarshal(filteredReader); + log.trace("After unmarshal()."); + } catch (UnmarshalException e) { + if (log.isDebugEnabled()) { + log.debug("Failed to unmarshall security layer message.", e); + } else { + log.info("Failed to unmarshall security layer message." + e.getMessage()); + } + + if (validationEventHandler.getErrorEvent() != null) { + ValidationEvent errorEvent = validationEventHandler.getErrorEvent(); + if (e.getLinkedException() == null) { + e.setLinkedException(errorEvent.getLinkedException()); + } + } + throw e; + } + + return object; + + } + +} diff --git a/utils/src/main/java/at/gv/egiz/slbinding/impl/CreateXMLSignatureResponseType.java b/utils/src/main/java/at/gv/egiz/slbinding/impl/CreateXMLSignatureResponseType.java index 47c00a84..dd107c3e 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/impl/CreateXMLSignatureResponseType.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/CreateXMLSignatureResponseType.java @@ -1,18 +1,22 @@ /* - * 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. +* 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. +*/ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. */ package at.gv.egiz.slbinding.impl; @@ -23,8 +27,8 @@ import java.io.UnsupportedEncodingException; import javax.xml.bind.annotation.XmlTransient; import javax.xml.stream.XMLStreamException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -32,40 +36,34 @@ import org.apache.commons.logging.LogFactory; */ public class CreateXMLSignatureResponseType extends at.buergerkarte.namespaces.securitylayer._1.CreateXMLSignatureResponseType implements RedirectCallback { - @XmlTransient - private static Log log = LogFactory.getLog(CreateXMLSignatureResponseType.class); - @XmlTransient - protected ByteArrayOutputStream redirectOS = null; + @XmlTransient + private final Logger log = LoggerFactory.getLogger(CreateXMLSignatureResponseType.class); + @XmlTransient + protected ByteArrayOutputStream redirectOS = null; - @Override - public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException { - if (log.isDebugEnabled()) { - log.debug("enabling event redirection for " + CreateXMLSignatureResponseType.class.getName()); + @Override + public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException { + log.trace("enabling event redirection for XMLContentType"); + redirectOS = new ByteArrayOutputStream(); + filter.setRedirectStream(redirectOS); } - redirectOS = new ByteArrayOutputStream(); - filter.setRedirectStream(redirectOS); - } - @Override - public void disableRedirect(RedirectEventFilter filter) throws XMLStreamException { - if (log.isDebugEnabled()) { - log.debug("disabling event redirection for " + CreateXMLSignatureResponseType.class.getName()); + @Override + public void disableRedirect(RedirectEventFilter filter) throws XMLStreamException { + log.trace("disabling event redirection for XMLContentType"); + filter.flushRedirectStream(); + filter.setRedirectStream(null); + if (log.isTraceEnabled()) { + try { + log.trace("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); + } catch (UnsupportedEncodingException ex) { + log.error("failed to log redirected events", ex); + } + } } - filter.flushRedirectStream(); - filter.setRedirectStream(null); - - if (log.isDebugEnabled()) { - try { - log.debug("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); - } catch (UnsupportedEncodingException ex) { - log.debug("failed to log redirected events", ex); - } + @Override + public ByteArrayOutputStream getRedirectedStream() { + return redirectOS; } - } - - @Override - public ByteArrayOutputStream getRedirectedStream() { - return redirectOS; - } } diff --git a/utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java b/utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java index 494e6972..bde9b22a 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java @@ -23,8 +23,8 @@ package at.gv.egiz.slbinding.impl; import at.gv.egiz.slbinding.*; import javax.xml.bind.annotation.XmlTransient; import javax.xml.namespace.NamespaceContext; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -33,7 +33,7 @@ import org.apache.commons.logging.LogFactory; public class SignatureLocationType extends at.buergerkarte.namespaces.securitylayer._1.SignatureLocationType implements NamespaceContextCallback { @XmlTransient - private static Log log = LogFactory.getLog(SignatureLocationType.class); + private final Logger log = LoggerFactory.getLogger(SignatureLocationType.class); @XmlTransient protected NamespaceContext namespaceContext; @@ -44,7 +44,7 @@ public class SignatureLocationType extends at.buergerkarte.namespaces.securityla @Override public void preserveNamespaceContext(RedirectEventFilter filter) { - log.debug("preserving namespace context for SignatureLocationType"); + log.trace("preserving namespace context for SignatureLocationType"); namespaceContext = filter.getCurrentNamespaceContext(); } } diff --git a/utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java b/utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java index b1de9406..4b3a6f79 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java @@ -26,11 +26,10 @@ import java.io.UnsupportedEncodingException; import java.util.HashSet; import java.util.Set; import javax.xml.bind.annotation.XmlTransient; -import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -39,7 +38,7 @@ import org.apache.commons.logging.LogFactory; public class TransformsInfoType extends at.buergerkarte.namespaces.securitylayer._1.TransformsInfoType implements RedirectCallback { @XmlTransient - private static Log log = LogFactory.getLog(TransformsInfoType.class); + private final Logger log = LoggerFactory.getLogger(TransformsInfoType.class); @XmlTransient private static final Set<QName> redirectTriggers = initRedirectTriggers(); @XmlTransient @@ -53,21 +52,21 @@ public class TransformsInfoType extends at.buergerkarte.namespaces.securitylayer @Override public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException { - log.debug("enabling event redirection for TransformsInfoType"); + log.trace("enabling event redirection for TransformsInfoType"); redirectOS = new ByteArrayOutputStream(); filter.setRedirectStream(redirectOS, redirectTriggers); } @Override public void disableRedirect(RedirectEventFilter filter) throws XMLStreamException { - log.debug("disabling event redirection for TransformsInfoType"); + log.trace("disabling event redirection for TransformsInfoType"); filter.flushRedirectStream(); filter.setRedirectStream(null); - if (log.isDebugEnabled()) { + if (log.isTraceEnabled()) { try { - log.debug("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); + log.trace("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); } catch (UnsupportedEncodingException ex) { - log.debug("failed to log redirected events", ex); + log.error("failed to log redirected events", ex); } } } diff --git a/utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java b/utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java index fd52e378..88515067 100644 --- a/utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java +++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java @@ -27,8 +27,8 @@ import java.io.UnsupportedEncodingException; import javax.xml.bind.annotation.XmlTransient; import javax.xml.stream.XMLStreamException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -37,27 +37,27 @@ import org.apache.commons.logging.LogFactory; public class XMLContentType extends at.buergerkarte.namespaces.securitylayer._1.XMLContentType implements RedirectCallback { @XmlTransient - private static Log log = LogFactory.getLog(XMLContentType.class); + private final Logger log = LoggerFactory.getLogger(XMLContentType.class); @XmlTransient protected ByteArrayOutputStream redirectOS = null; @Override public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException { - log.debug("enabling event redirection for XMLContentType"); + log.trace("enabling event redirection for XMLContentType"); redirectOS = new ByteArrayOutputStream(); filter.setRedirectStream(redirectOS); } @Override public void disableRedirect(RedirectEventFilter filter) throws XMLStreamException { - log.debug("disabling event redirection for XMLContentType"); + log.trace("disabling event redirection for XMLContentType"); filter.flushRedirectStream(); filter.setRedirectStream(null); - if (log.isDebugEnabled()) { + if (log.isTraceEnabled()) { try { - log.debug("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); + log.trace("redirected events (UTF-8): " + redirectOS.toString("UTF-8")); } catch (UnsupportedEncodingException ex) { - log.debug("failed to log redirected events", ex); + log.error("failed to log redirected events", ex); } } } diff --git a/utils/src/main/java/at/gv/egiz/validation/ReportingValidationEventHandler.java b/utils/src/main/java/at/gv/egiz/validation/ReportingValidationEventHandler.java index 6543c333..32826b16 100644 --- a/utils/src/main/java/at/gv/egiz/validation/ReportingValidationEventHandler.java +++ b/utils/src/main/java/at/gv/egiz/validation/ReportingValidationEventHandler.java @@ -18,8 +18,8 @@ package at.gv.egiz.validation; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -27,7 +27,7 @@ import org.apache.commons.logging.LogFactory; */ public class ReportingValidationEventHandler implements ValidationEventHandler { - protected static final Log log = LogFactory.getLog(ReportingValidationEventHandler.class); + private final Logger log = LoggerFactory.getLogger(ReportingValidationEventHandler.class); protected ValidationEvent errorEvent; diff --git a/utils/src/main/java/at/gv/egiz/xades/QualifyingPropertiesException.java b/utils/src/main/java/at/gv/egiz/xades/QualifyingPropertiesException.java index e892a13b..3aba7cd2 100644 --- a/utils/src/main/java/at/gv/egiz/xades/QualifyingPropertiesException.java +++ b/utils/src/main/java/at/gv/egiz/xades/QualifyingPropertiesException.java @@ -18,23 +18,22 @@ package at.gv.egiz.xades; public class QualifyingPropertiesException extends Exception {
- public QualifyingPropertiesException() {
- // TODO Auto-generated constructor stub
+ private static final long serialVersionUID = 1L; + + public QualifyingPropertiesException() { + super();
}
public QualifyingPropertiesException(String message) {
super(message);
- // TODO Auto-generated constructor stub
}
public QualifyingPropertiesException(Throwable cause) {
super(cause);
- // TODO Auto-generated constructor stub
}
public QualifyingPropertiesException(String message, Throwable cause) {
super(message, cause);
- // TODO Auto-generated constructor stub
}
}
|