From b1c8641a63a67e3c64d948f9e8dce5c01e11e2dd Mon Sep 17 00:00:00 2001 From: mcentner Date: Wed, 5 May 2010 15:29:01 +0000 Subject: 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 --- .../at/gv/egiz/slbinding/RedirectEventFilter.java | 3 - .../slbinding/RedirectUnmarshallerListener.java | 6 +- .../java/at/gv/egiz/slbinding/SLUnmarshaller.java | 263 +++++++++++++++++++++ .../impl/CreateXMLSignatureResponseType.java | 90 ++++--- .../egiz/slbinding/impl/SignatureLocationType.java | 8 +- .../gv/egiz/slbinding/impl/TransformsInfoType.java | 17 +- .../at/gv/egiz/slbinding/impl/XMLContentType.java | 16 +- 7 files changed, 330 insertions(+), 73 deletions(-) create mode 100644 utils/src/main/java/at/gv/egiz/slbinding/SLUnmarshaller.java (limited to 'utils/src/main/java/at/gv/egiz/slbinding') 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 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 getDefaultSchemaUrls() { + return Collections.unmodifiableList(Arrays.asList(DefaultSchema.SCHEMA_FILES)); + } + + private static Schema createSchema(Collection schemaUrls) throws SAXException, IOException { + Logger log = LoggerFactory.getLogger(SLUnmarshaller.class); + Source[] sources = new Source[schemaUrls.size()]; + Iterator 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 getDefaultJAXBContextPackageNames() { + return Collections.unmodifiableList(Arrays.asList(DefaultContext.packageNames)); + } + + private static JAXBContext createJAXBContext(Collection 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 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 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 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); } } } -- cgit v1.2.3