summaryrefslogtreecommitdiff
path: root/utils/src/main/java/at/gv/egiz/slbinding
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/main/java/at/gv/egiz/slbinding')
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/NamespaceContextCallback.java41
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/RedirectCallback.java42
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java259
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java68
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java50
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java70
-rw-r--r--utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java60
7 files changed, 590 insertions, 0 deletions
diff --git a/utils/src/main/java/at/gv/egiz/slbinding/NamespaceContextCallback.java b/utils/src/main/java/at/gv/egiz/slbinding/NamespaceContextCallback.java
new file mode 100644
index 00000000..08c075ac
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/NamespaceContextCallback.java
@@ -0,0 +1,41 @@
+/*
+* 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;
+
+import javax.xml.namespace.NamespaceContext;
+
+/**
+ *
+ * @author clemens
+ */
+public interface NamespaceContextCallback {
+
+ /**
+ * preserves the current namespace context from the XMLEventFilter
+ * @param filter
+ */
+ void preserveNamespaceContext(RedirectEventFilter filter);
+
+ /**
+ * @return the namespace context if preserveNamespaceContext() was called on this object before, null otherwise
+ */
+ NamespaceContext getNamespaceContext();
+}
diff --git a/utils/src/main/java/at/gv/egiz/slbinding/RedirectCallback.java b/utils/src/main/java/at/gv/egiz/slbinding/RedirectCallback.java
new file mode 100644
index 00000000..80fb56dc
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/RedirectCallback.java
@@ -0,0 +1,42 @@
+/*
+* 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;
+
+import java.io.ByteArrayOutputStream;
+import javax.xml.stream.XMLStreamException;
+
+/**
+ *
+ * The beforeUnmarshal(Unmarshaller um, Object parent) methods don't allow to pass the RedirectEventFilter,
+ * so we implement a callback interface common to all generated classes
+ * @author clemens
+ */
+public interface RedirectCallback {
+
+ void enableRedirect(RedirectEventFilter filter) throws XMLStreamException;
+
+ void disableRedirect(RedirectEventFilter filter) throws XMLStreamException;
+
+ /**
+ * @return the redirected stream or null if enableRedirect() was not called before
+ */
+ ByteArrayOutputStream getRedirectedStream();
+}
diff --git a/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java b/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java
new file mode 100644
index 00000000..d2a7fb30
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/RedirectEventFilter.java
@@ -0,0 +1,259 @@
+/*
+* 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;
+
+import java.io.OutputStream;
+import java.util.Set;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.EventFilter;
+import javax.xml.stream.XMLEventWriter;
+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;
+
+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;
+ protected NamespaceContext currentNamespaceContext = null;
+
+ /**
+ * Event redirection is disabled, set a redirect stream to enable.
+ */
+ public RedirectEventFilter() {
+ redirectWriter = null;
+ // redirectTriggers = null;
+ }
+
+ /**
+ *
+ * @param redirectStream
+ * if null, no events are redirected
+ * @param redirectTriggers
+ * if null, all events are redirected
+ */
+ public RedirectEventFilter(OutputStream redirectStream, String encoding)
+ throws XMLStreamException { // , List<QName> redirectTriggers
+ if (redirectStream != null) {
+ XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+ if (encoding == null) {
+ encoding = DEFAULT_ENCODING;
+ }
+ this.redirectWriter = outputFactory.createXMLEventWriter(redirectStream,
+ encoding);
+ }
+ // this.redirectTriggers = redirectTriggers;
+ }
+
+ /**
+ * All startElement events occuring in the redirectTriggers list will trigger
+ * redirection of the entire (sub-)fragment.
+ *
+ * @param event
+ * @return false if an event is redirected
+ */
+ @Override
+ public boolean accept(XMLEvent event) {
+ int eventType = event.getEventType();
+
+ if (eventType == XMLStreamConstants.START_ELEMENT) {
+ currentNamespaceContext = event.asStartElement().getNamespaceContext();
+ }
+ if (redirectWriter == null) {
+ return true;
+ }
+ if (eventType == XMLStreamConstants.START_ELEMENT) {
+ if (depth >= 0 || triggersRedirect(event.asStartElement().getName())) {
+ depth++;
+ }
+ } else if (eventType == XMLStreamConstants.END_ELEMENT) {
+ if (depth >= 0 && --depth < 0) {
+ // redirect the end element of the trigger,
+ // but do not redirect the end element of the calling type
+ if (redirectTriggers != null) {
+ redirectEvent(event);
+ return false;
+ }
+ }
+ }
+ if (depth >= 0) { //|| (depth == 0 && redirectTriggers == null)) {
+ redirectEvent(event);
+ return false;
+ }
+ return true; // depth < 0;
+
+// switch (event.getEventType()) {
+// case XMLStreamConstants.START_ELEMENT:
+// StartElement startElt = event.asStartElement();
+// if (depth >= 0 || triggersRedirect(startElt.getName())) {
+// depth++;
+// }
+// // namespace context changes only on start elements
+// // (first event might not be startElement, but we don't need CDATA's
+// // namespace context)
+// currentNamespaceContext = startElt.getNamespaceContext();
+// break;
+// case XMLStreamConstants.END_ELEMENT:
+// // if depth switches from positive to negative, this is the closing tag of
+// // the trigger (redirect as well!)
+// if (depth >= 0 && --depth < 0) {
+// redirectEvent(event);
+// return false;
+// }
+// break;
+// }
+// if (depth >= 0) {
+// redirectEvent(event);
+// return false;
+// }
+// return true; // depth < 0;
+ }
+
+ /**
+ * @param startElt
+ * @return true if the set of triggers contains startElement
+ * (or no triggers are registered, i.e. everything is redirected)
+ */
+ private boolean triggersRedirect(QName startElement) {
+ if (redirectTriggers != null) {
+ return redirectTriggers.contains(startElement);
+ }
+ return true;
+ }
+
+ private void redirectEvent(XMLEvent event) {
+ try {
+ if (log.isTraceEnabled()) {
+ log.trace("redirecting StAX event " + event);
+ }
+ redirectWriter.add(event);
+ } catch (XMLStreamException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * Enable/disable redirection of <em>all</em> events from now on.
+ * The redirected events will be UTF-8 encoded and written to the stream.
+ *
+ * @param redirectstream
+ * if null, redirection is disabled
+ */
+ public void setRedirectStream(OutputStream redirectStream) throws XMLStreamException {
+ setRedirectStream(redirectStream, DEFAULT_ENCODING, null);
+ }
+
+ /**
+ * Enable/disable redirection of <em>all</em> events from now on.
+ *
+ * @param redirectStream if null, redirection is disabled
+ * @param encoding The encoding for the redirect stream
+ * @throws javax.xml.stream.XMLStreamException
+ */
+ public void setRedirectStream(OutputStream redirectStream, String encoding) throws XMLStreamException {
+ setRedirectStream(redirectStream, encoding, null);
+ }
+
+ /**
+ * Enable/disable redirection of all (child) elements contained in redirect triggers.
+ * The redirected events will be UTF-8 encoded and written to the stream.
+ *
+ * @param redirectstream
+ * if null, redirection is disabled
+ * @param redirectTriggers elements that trigger the redirection
+ */
+ public void setRedirectStream(OutputStream redirectStream, Set<QName> redirectTriggers) throws XMLStreamException {
+ setRedirectStream(redirectStream, DEFAULT_ENCODING, redirectTriggers);
+ }
+
+ /**
+ * Enable/disable redirection of all (child) elements contained in redirect triggers.
+ *
+ * @param redirectstream
+ * if null, redirection is disabled
+ * @param encoding The encoding for the redirect stream
+ * @param redirectTriggers elements that trigger the redirection
+ */
+ public void setRedirectStream(OutputStream redirectStream, String encoding, Set<QName> redirectTriggers) throws XMLStreamException {
+ if (redirectStream != null) {
+ XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+ if (encoding == null) {
+ encoding = DEFAULT_ENCODING;
+ }
+ redirectWriter = outputFactory.createXMLEventWriter(redirectStream,
+ encoding);
+ if (redirectTriggers == null) {
+ // start redirecting
+ depth = 0;
+ }
+ this.redirectTriggers = redirectTriggers;
+ } else {
+ redirectWriter = null;
+ this.redirectTriggers = null;
+ }
+ }
+
+ /**
+ * Enable/disable redirection of fragments (defined by elements in
+ * redirectTriggers)
+ *
+ * @param redirectStream
+ * if null, redirection is disabled
+ * @param redirectTriggers
+ * All startElement events occuring in this list will trigger
+ * redirection of the entire fragment. If null, all events are
+ * redirected
+ */
+ // public void setRedirectStream(OutputStream redirectStream, List<QName>
+ // redirectTriggers) throws XMLStreamException {
+ // if (redirectStream != null) {
+ // XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+ // redirectWriter = outputFactory.createXMLEventWriter(redirectStream);
+ // } else {
+ // redirectWriter = null;
+ // }
+ // this.redirectTriggers = (redirectStream == null) ? null : redirectTriggers;
+ // }
+ /**
+ * flushes the internal EventWriter
+ *
+ * @throws javax.xml.stream.XMLStreamException
+ */
+ public void flushRedirectStream() throws XMLStreamException {
+ redirectWriter.flush();
+ }
+
+ /**
+ * the namespaceContext of the last startelement event read
+ *
+ * @return
+ */
+ public NamespaceContext getCurrentNamespaceContext() {
+ return currentNamespaceContext;
+ }
+}
diff --git a/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java b/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java
new file mode 100644
index 00000000..08c12146
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/RedirectUnmarshallerListener.java
@@ -0,0 +1,68 @@
+/*
+* 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;
+
+import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Enables event redirection before marshalling a target of type RedirectCallback.
+ * It is up to the target class to implement the redirection (default implementation in RedirectCallback).
+ * Disables event redirection after marshalling (when the closing tag occurs).
+ * @author clemens
+ */
+public class RedirectUnmarshallerListener extends Unmarshaller.Listener {
+
+ private static Log log = LogFactory.getLog(RedirectUnmarshallerListener.class);
+ protected RedirectEventFilter eventFilter;
+
+ public RedirectUnmarshallerListener(RedirectEventFilter eventFilter) {
+ this.eventFilter = eventFilter;
+ }
+
+ @Override
+ public void beforeUnmarshal(Object target, Object parent) {
+ if (target instanceof RedirectCallback) {
+ try {
+ ((RedirectCallback) target).enableRedirect(eventFilter);
+ } catch (XMLStreamException ex) {
+ log.error("failed to enable event redirection for " + target.getClass().getName() + ": " + ex.getMessage(), ex);
+ }
+ }
+ if (target instanceof NamespaceContextCallback) {
+ ((NamespaceContextCallback) target).preserveNamespaceContext(eventFilter);
+ }
+ }
+
+ @Override
+ public void afterUnmarshal(Object target, Object parent) {
+ if (target instanceof RedirectCallback) {
+ try {
+ ((RedirectCallback) target).disableRedirect(eventFilter);
+ } catch (XMLStreamException ex) {
+ log.error("failed to disable event redirection for " + target.getClass().getName() + ": " + ex.getMessage(), ex);
+ }
+ }
+ }
+}
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
new file mode 100644
index 00000000..494e6972
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/SignatureLocationType.java
@@ -0,0 +1,50 @@
+/*
+* 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;
+
+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;
+
+/**
+ *
+ * @author clemens
+ */
+public class SignatureLocationType extends at.buergerkarte.namespaces.securitylayer._1.SignatureLocationType implements NamespaceContextCallback {
+
+ @XmlTransient
+ private static Log log = LogFactory.getLog(SignatureLocationType.class);
+ @XmlTransient
+ protected NamespaceContext namespaceContext;
+
+ @Override
+ public NamespaceContext getNamespaceContext() {
+ return namespaceContext;
+ }
+
+ @Override
+ public void preserveNamespaceContext(RedirectEventFilter filter) {
+ log.debug("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
new file mode 100644
index 00000000..b4e988f0
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/TransformsInfoType.java
@@ -0,0 +1,70 @@
+/*
+* 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;
+
+import at.gv.egiz.slbinding.*;
+import java.io.ByteArrayOutputStream;
+import java.util.HashSet;
+import java.util.Set;
+import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * @author clemens
+ */
+public class TransformsInfoType extends at.buergerkarte.namespaces.securitylayer._1.TransformsInfoType implements RedirectCallback {
+
+ @XmlTransient
+ private static Log log = LogFactory.getLog(TransformsInfoType.class);
+ @XmlTransient
+ private static final Set<QName> redirectTriggers = initRedirectTriggers();
+ @XmlTransient
+ protected ByteArrayOutputStream redirectOS = null;
+
+ private static Set<QName> initRedirectTriggers() {
+ HashSet<QName> dsigTransforms = new HashSet<QName>();
+ dsigTransforms.add(new QName("http://www.w3.org/2000/09/xmldsig#", "Transforms"));
+ return dsigTransforms;
+ }
+
+ @Override
+ public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException {
+ log.debug("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");
+ filter.flushRedirectStream();
+ filter.setRedirectStream(null);
+ }
+
+ @Override
+ public ByteArrayOutputStream getRedirectedStream() {
+ return redirectOS;
+ }
+}
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
new file mode 100644
index 00000000..c32542aa
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/slbinding/impl/XMLContentType.java
@@ -0,0 +1,60 @@
+/*
+* 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;
+
+import at.gv.egiz.slbinding.RedirectCallback;
+import at.gv.egiz.slbinding.RedirectEventFilter;
+import java.io.ByteArrayOutputStream;
+import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.stream.XMLStreamException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * @author clemens
+ */
+public class XMLContentType extends at.buergerkarte.namespaces.securitylayer._1.XMLContentType implements RedirectCallback {
+
+ @XmlTransient
+ private static Log log = LogFactory.getLog(TransformsInfoType.class);
+ @XmlTransient
+ protected ByteArrayOutputStream redirectOS = null;
+
+ @Override
+ public void enableRedirect(RedirectEventFilter filter) throws XMLStreamException {
+ log.debug("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");
+ filter.flushRedirectStream();
+ filter.setRedirectStream(null);
+ }
+
+ @Override
+ public ByteArrayOutputStream getRedirectedStream() {
+ return redirectOS;
+ }
+}