diff options
author | mcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4> | 2008-09-24 13:56:53 +0000 |
---|---|---|
committer | mcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4> | 2008-09-24 13:56:53 +0000 |
commit | 3edfbe631f24d73324bc4dd0d182ca7737c4d5b5 (patch) | |
tree | 0f325ee264c93ec75b177583f485ca3ad4f0d1c7 /bkucommon/src/main/java/at/gv/egiz/bku/slcommands | |
parent | fbaf0232de8db3f51c97162b484e2bc17f465999 (diff) | |
download | mocca-3edfbe631f24d73324bc4dd0d182ca7737c4d5b5.tar.gz mocca-3edfbe631f24d73324bc4dd0d182ca7737c4d5b5.tar.bz2 mocca-3edfbe631f24d73324bc4dd0d182ca7737c4d5b5.zip |
Improved SLResult marshalling.
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@66 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
Diffstat (limited to 'bkucommon/src/main/java/at/gv/egiz/bku/slcommands')
8 files changed, 591 insertions, 332 deletions
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/SLResult.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/SLResult.java index 7cf43fda..7989a771 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/SLResult.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/SLResult.java @@ -16,9 +16,8 @@ */ package at.gv.egiz.bku.slcommands;
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
+import javax.xml.transform.Result; +import javax.xml.transform.Templates; public interface SLResult {
@@ -40,5 +39,5 @@ public interface SLResult { * @param result
* @param transformer may be null.
*/
- public void writeTo(Result result, Transformer transformer) throws TransformerException;
+ public void writeTo(Result result, Templates templates);
}
\ No newline at end of file diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/CreateXMLSignatureResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/CreateXMLSignatureResultImpl.java index d2d2e678..092a13c4 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/CreateXMLSignatureResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/CreateXMLSignatureResultImpl.java @@ -16,28 +16,24 @@ */ package at.gv.egiz.bku.slcommands.impl;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-import at.buergerkarte.namespaces.securitylayer._1.CreateXMLSignatureResponseType;
-import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory;
-import at.gv.egiz.bku.slcommands.SLCommandFactory;
-import at.gv.egiz.bku.slexceptions.SLRuntimeException;
+import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.transform.Result; +import javax.xml.transform.Templates; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import at.buergerkarte.namespaces.securitylayer._1.CreateXMLSignatureResponseType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; +import at.gv.egiz.bku.slcommands.SLCommandFactory; +import at.gv.egiz.bku.slexceptions.SLRuntimeException; /**
* This calls implements the result of the security layer command <code>CreateXMLSignature</code>.
@@ -104,35 +100,10 @@ public class CreateXMLSignatureResultImpl extends SLResultImpl { }
}
-
- @Override
- public void writeTo(Result result) {
-
- try {
- writeTo(result, null);
- } catch (TransformerException e) {
- log.error(e);
- }
-
- }
-
- /* (non-Javadoc)
- * @see at.gv.egiz.bku.slcommands.impl.SLResultImpl#writeTo(javax.xml.transform.Result, javax.xml.transform.Transformer)
- */
- @Override
- public void writeTo(Result result, Transformer transformer) throws TransformerException {
-
- if (transformer == null) {
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- try {
- transformer = transformerFactory.newTransformer();
- } catch (TransformerConfigurationException e) {
- log.error("Failed to create Transformer.", e);
- throw new SLRuntimeException(e);
- }
- }
- transformer.transform(new DOMSource(doc), result);
-
+ + @Override + public void writeTo(Result result, Templates templates) { + writeTo(doc, result, templates); }
}
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java index fb624211..176ba001 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java @@ -16,12 +16,9 @@ */ package at.gv.egiz.bku.slcommands.impl;
-import java.util.Locale; - import javax.xml.transform.Result; +import javax.xml.transform.Templates; -import at.buergerkarte.namespaces.securitylayer._1.ErrorResponseType; -import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; import at.gv.egiz.bku.slcommands.ErrorResult; import at.gv.egiz.bku.slexceptions.SLException; @@ -48,15 +45,9 @@ public class ErrorResultImpl extends SLResultImpl implements ErrorResult { this.slException = slException;
}
- @Override
- public void writeTo(Result result) {
-
- ObjectFactory factory = new ObjectFactory();
- ErrorResponseType responseType = factory.createErrorResponseType();
- responseType.setErrorCode(slException.getErrorCode());
- responseType.setInfo(slException.getDetailedMsg());
-
- writeTo(factory.createErrorResponse(responseType), result);
-
- }
+ @Override + public void writeTo(Result result, Templates templates) { + writeErrorTo(slException, result, templates); + } +
}
\ No newline at end of file diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java index b6745e1f..4d64ae36 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java @@ -16,55 +16,66 @@ */ package at.gv.egiz.bku.slcommands.impl;
-import iaik.asn1.CodingException;
-import iaik.asn1.DerCoder;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-
-import at.buergerkarte.namespaces.personenbindung._20020506_.CompressedIdentityLinkType;
-import at.buergerkarte.namespaces.securitylayer._1.AnyChildrenType;
-import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsBinaryFileType;
-import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadRequestType;
-import at.gv.egiz.bku.slcommands.InfoboxReadCommand;
-import at.gv.egiz.bku.slcommands.SLCommand;
-import at.gv.egiz.bku.slcommands.SLCommandContext;
-import at.gv.egiz.bku.slcommands.SLResult;
-import at.gv.egiz.bku.slexceptions.SLCommandException;
-import at.gv.egiz.bku.slexceptions.SLExceptionMessages;
-import at.gv.egiz.bku.slexceptions.SLRuntimeException;
-import at.gv.egiz.idlink.CompressedIdentityLinkFactory;
-import at.gv.egiz.idlink.IdentityLinkTransformer;
-import at.gv.egiz.idlink.ans1.IdentityLink;
-import at.gv.egiz.stal.InfoboxReadRequest;
-import at.gv.egiz.stal.InfoboxReadResponse;
-import at.gv.egiz.stal.STALRequest;
+import iaik.asn1.CodingException; +import iaik.asn1.DerCoder; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; + +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Result; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import at.buergerkarte.namespaces.personenbindung._20020506_.CompressedIdentityLinkType; +import at.buergerkarte.namespaces.securitylayer._1.AnyChildrenType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxAssocArrayPairType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadDataAssocArrayType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsBinaryFileType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadRequestType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadKeys; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadPairs; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadValue; +import at.gv.egiz.bku.slcommands.InfoboxReadCommand; +import at.gv.egiz.bku.slcommands.SLCommand; +import at.gv.egiz.bku.slcommands.SLCommandContext; +import at.gv.egiz.bku.slcommands.SLResult; +import at.gv.egiz.bku.slexceptions.SLCommandException; +import at.gv.egiz.bku.slexceptions.SLExceptionMessages; +import at.gv.egiz.bku.slexceptions.SLRuntimeException; +import at.gv.egiz.idlink.CompressedIdentityLinkFactory; +import at.gv.egiz.idlink.IdentityLinkTransformer; +import at.gv.egiz.idlink.ans1.IdentityLink; +import at.gv.egiz.stal.InfoboxReadRequest; +import at.gv.egiz.stal.InfoboxReadResponse; +import at.gv.egiz.stal.STALRequest; /**
* This class implements the security layer command
@@ -82,23 +93,45 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType /**
* Logging facility.
*/
- protected static Log log = LogFactory.getLog(InfoboxReadCommandImpl.class);
-
- public static final String INFOBOX_IDENTIFIER_CERTIFICATES = "Certificates";
-
- public static final String BOX_SPECIFIC_PARAMETER_IDENTITY_LINK_DOMAIN_IDENTIFIER = "IdentityLinkDomainIdentifier";
-
- public static final String INFOBOX_IDENTIFIER_IDENTITY_LINK = "IdentityLink";
+ protected static Log log = LogFactory.getLog(InfoboxReadCommandImpl.class); + + public static final String SEARCH_STRING_PATTERN = ".&&[^/](/.&&[^/])*";
+ public static final String INFOBOX_IDENTIFIER_CERTIFICATES = "Certificates"; + + public static final String BOX_SPECIFIC_PARAMETER_IDENTITY_LINK_DOMAIN_IDENTIFIER = "IdentityLinkDomainIdentifier"; + + public static final String INFOBOX_IDENTIFIER_IDENTITY_LINK = "IdentityLink"; + + public static final String[] INFOXBOX_CERTIFICATES_KEYS = new String[] { + "SecureSignatureKeypair", + "CertifiedKeypair" }; + + private static final int ASSOC_ARRAY_READ_KEYS = 1; + + private static final int ASSOC_ARRAY_READ_PAIRS = 2; + + private static final int ASSOC_ARRAY_READ_VALUE = 3;
+ /**
* The <code>InfoboxIdentifier</code>
*/
protected String infoboxIdentifier;
/**
- * The <code>IdentityLinkDomainIdentifier</code> value of an IdentyLink infobox.
+ * The <code>IdentityLinkDomainIdentifier</code> value of an <code>IdentyLink</code> infobox.
*/
- protected String identityLinkDomainIdentifier;
+ protected String identityLinkDomainIdentifier; + + /** + * The list of certificates to be read from an <code>Certificates</code> infobox. + */ + protected List<String> certificates; + + /** + * The result type. + */ + protected int assocArrayResult;
/**
* Is content XML entity?
@@ -125,12 +158,6 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType infoboxIdentifier = req.getInfoboxIdentifier();
- InfoboxReadParamsBinaryFileType binaryFileParameters = req.getBinaryFileParameters();
- if (binaryFileParameters != null) {
- isXMLEntity = binaryFileParameters.isContentIsXMLEntity();
- log.debug("Got ContentIsXMLEntity=" + isXMLEntity + ".");
- }
-
if (INFOBOX_IDENTIFIER_IDENTITY_LINK.equals(infoboxIdentifier)) {
if (req.getAssocArrayParameters() != null) {
@@ -138,6 +165,11 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType throw new SLCommandException(4010);
}
+ InfoboxReadParamsBinaryFileType binaryFileParameters = req.getBinaryFileParameters(); + if (binaryFileParameters != null) { + isXMLEntity = binaryFileParameters.isContentIsXMLEntity(); + log.debug("Got ContentIsXMLEntity=" + isXMLEntity + "."); + } AnyChildrenType boxSpecificParameters = req.getBoxSpecificParameters();
@@ -158,6 +190,74 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType throw new SLCommandException(4010);
}
}
+ + } else if (INFOBOX_IDENTIFIER_CERTIFICATES.equals(infoboxIdentifier)) { + + if (req.getBinaryFileParameters() != null) { + log.info("Got BinaryFileParameters but Infobox type is AssocArray."); + throw new SLCommandException(4010); + } + + if (req.getBoxSpecificParameters() != null) { + log.info("Got invalid BoxSpecificParameters."); + throw new SLCommandException(4010); + } + + InfoboxReadParamsAssocArrayType assocArrayParameters = req + .getAssocArrayParameters(); + if (assocArrayParameters == null) { + log.info("Infobox type is AssocArray but got no AssocArrayParameters."); + throw new SLCommandException(4010); + } + + // RreadKeys? + if (assocArrayParameters.getReadKeys() != null) { + assocArrayResult = ASSOC_ARRAY_READ_KEYS; + ReadKeys readKeys = assocArrayParameters.getReadKeys(); + certificates = findCertificates(readKeys.getSearchString()); + if (readKeys.isUserMakesUnique() && certificates.size() > 1) { + log.info("UserMakesUnique not supported"); + // TODO: give more specific error message + throw new SLCommandException(4010); + } + } + + // ReadPairs? + if (assocArrayParameters.getReadPairs() != null) { + assocArrayResult = ASSOC_ARRAY_READ_PAIRS; + ReadPairs readPairs = assocArrayParameters.getReadPairs(); + if (readPairs.isValuesAreXMLEntities()) { + log.info("Got valuesAreXMLEntities but infobox type is binary."); + throw new SLCommandException(4010); + } + certificates = findCertificates(readPairs.getSearchString()); + if (readPairs.isUserMakesUnique() && certificates.size() > 1) { + log.info("UserMakesUnique not supported"); + // TODO: give more specific error message + throw new SLCommandException(4010); + } + } + + // ReadValue + if (assocArrayParameters.getReadValue() != null) { + assocArrayResult = ASSOC_ARRAY_READ_VALUE; + ReadValue readValue = assocArrayParameters.getReadValue(); + if (readValue.isValueIsXMLEntity()) { + log.info("Got valuesAreXMLEntities but infobox type is binary."); + throw new SLCommandException(4010); + } + String key = readValue.getKey(); + if (Arrays.asList(INFOXBOX_CERTIFICATES_KEYS).contains(key)) { + certificates = Collections.singletonList(key); + } else { + certificates = Collections.emptyList(); + } + } + + if (assocArrayResult == 0) { + log.info("Infobox type is AssocArray but got invalid AssocArrayParameters."); + throw new SLCommandException(4010); + } } else {
throw new SLCommandException(4002,
@@ -168,9 +268,15 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType }
@Override
- public SLResult execute() {
+ public SLResult execute() { try {
- return readIdentityLink();
+ if (INFOBOX_IDENTIFIER_IDENTITY_LINK.equals(infoboxIdentifier)) { + return readIdentityLink(); + } else if (INFOBOX_IDENTIFIER_CERTIFICATES.equals(infoboxIdentifier)) { + return readCertificates(); + } else { + throw new SLCommandException(4000); + } } catch (SLCommandException e) {
return new ErrorResultImpl(e);
}
@@ -302,7 +408,7 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType new Object[] { INFOBOX_IDENTIFIER_IDENTITY_LINK });
}
- InfoboxReadResultImpl result = new InfoboxReadResultImpl();
+ InfoboxReadResultFileImpl result = new InfoboxReadResultFileImpl();
ByteArrayOutputStream resultBytes = null;
Result xmlResult = (isXMLEntity || identityLinkDomainIdentifier != null)
? result.getXmlResult(true)
@@ -406,9 +512,81 @@ public class InfoboxReadCommandImpl extends SLCommandImpl<InfoboxReadRequestType return result;
} + + protected List<String> findCertificates(String searchString) throws SLCommandException { + + if ("*".equals(searchString) || "**".equals(searchString)) { + return Arrays.asList(INFOXBOX_CERTIFICATES_KEYS); + } + + if (Pattern.matches(SEARCH_STRING_PATTERN, searchString)) { + +// for (int i = 0; i < searchString.length(); i++) { +// int codePoint = searchString.codePointAt(i); +// +// } + + // TODO : build pattern + return Collections.emptyList(); + } else { + log.info("Got invalid search string '" + searchString + "'"); + throw new SLCommandException(4010); + } + + } + + private SLResult readCertificates() throws SLCommandException { + + ObjectFactory objectFactory = new ObjectFactory(); + + InfoboxReadDataAssocArrayType infoboxReadDataAssocArrayType = objectFactory + .createInfoboxReadDataAssocArrayType(); + + if (assocArrayResult == ASSOC_ARRAY_READ_KEYS) { - @Override - public String getIdentityLinkDomainId() { - return identityLinkDomainIdentifier; - }
+ List<String> keys = infoboxReadDataAssocArrayType.getKey(); + keys.addAll(certificates); + + } else { + + if (certificates != null && !certificates.isEmpty()) { + + List<STALRequest> stalRequests = new ArrayList<STALRequest>(); + + // get certificates + InfoboxReadRequest infoboxReadRequest; + for (int i = 0; i < certificates.size(); i++) { + infoboxReadRequest = new InfoboxReadRequest(); + infoboxReadRequest.setInfoboxIdentifier(certificates.get(i)); + stalRequests.add(infoboxReadRequest); + } + + requestSTAL(stalRequests); + + List<X509Certificate> x509Certs = getCertificatesFromResponses(); + + for (int i = 0; i < certificates.size(); i++) { + InfoboxAssocArrayPairType infoboxAssocArrayPairType = objectFactory.createInfoboxAssocArrayPairType(); + infoboxAssocArrayPairType.setKey(certificates.get(i)); + try { + infoboxAssocArrayPairType.setBase64Content(x509Certs.get(i).getEncoded()); + } catch (CertificateEncodingException e) { + log.error("Failed to encode certificate.", e); + throw new SLCommandException(4000); + } + infoboxReadDataAssocArrayType.getPair().add(infoboxAssocArrayPairType); + } + + } + + } + + return new InfoboxReadResultImpl(infoboxReadDataAssocArrayType); + + } + + @Override + public String getIdentityLinkDomainId() { + return identityLinkDomainIdentifier; + } }
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultFileImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultFileImpl.java new file mode 100644 index 00000000..6f41b562 --- /dev/null +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultFileImpl.java @@ -0,0 +1,141 @@ +/* +* 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.slcommands.impl;
+
+import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Result; +import javax.xml.transform.Templates; +import javax.xml.transform.dom.DOMResult; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import at.buergerkarte.namespaces.securitylayer._1.Base64XMLContentType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadResponseType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; +import at.buergerkarte.namespaces.securitylayer._1.XMLContentType; +import at.gv.egiz.bku.slcommands.InfoboxReadResult; +import at.gv.egiz.bku.slcommands.SLCommand; +import at.gv.egiz.bku.slcommands.SLCommandFactory; +import at.gv.egiz.bku.slexceptions.SLRuntimeException; +
+/**
+ * This class implements the result of the security layer command <code>InfoboxReadRequest</code>.
+ *
+ * @author mcentner
+ */
+public class InfoboxReadResultFileImpl extends SLResultImpl implements
+ InfoboxReadResult {
+
+ /**
+ * Logging facility.
+ */
+ protected static Log log = LogFactory.getLog(InfoboxReadResultFileImpl.class);
+
+ /**
+ * The XML document containing the infobox content.
+ */
+ Document xmlDocument;
+
+ /**
+ * Creates the response document from the given <code>binaryContent</code>.
+ *
+ * @param binaryContent the infobox content
+ * @param preserveSpace the value of the <code>preserveSpace</code> parameter
+ *
+ * @return the created response document
+ */
+ private Document createResponseDocument(byte[] binaryContent, boolean preserveSpace) {
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ Document doc;
+ try {
+ doc = dbf.newDocumentBuilder().newDocument();
+ } catch (ParserConfigurationException e) {
+ // it should always be possible to create a new Document
+ log.error("Failed to create XML document.", e);
+ throw new SLRuntimeException(e);
+ }
+
+ ObjectFactory factory = new ObjectFactory();
+
+ Base64XMLContentType base64XMLContentType = factory.createBase64XMLContentType();
+ if (binaryContent == null) {
+ XMLContentType xmlContentType = factory.createXMLContentType();
+ if (preserveSpace) {
+ xmlContentType.setSpace("preserve");
+ }
+ base64XMLContentType.setXMLContent(xmlContentType);
+ } else {
+ base64XMLContentType.setBase64Content(binaryContent);
+ }
+ InfoboxReadResponseType infoboxReadResponseType = factory.createInfoboxReadResponseType();
+ infoboxReadResponseType.setBinaryFileData(base64XMLContentType);
+
+ JAXBElement<InfoboxReadResponseType> infoboxReadResponse = factory.createInfoboxReadResponse(infoboxReadResponseType);
+
+ JAXBContext context = SLCommandFactory.getJaxbContext();
+ try {
+ Marshaller marshaller = context.createMarshaller();
+ marshaller.marshal(infoboxReadResponse, doc);
+ } catch (JAXBException e) {
+ log.error("Failed to marshal 'InfoboxReadResponse' document.", e);
+ throw new SLRuntimeException(e);
+ }
+
+ return doc;
+
+ }
+
+
+ /**
+ * @return an XMLResult for marshalling the infobox to
+ */
+ Result getXmlResult(boolean preserveSpace) {
+
+ xmlDocument = createResponseDocument(null, preserveSpace);
+
+ NodeList nodeList = xmlDocument.getElementsByTagNameNS(SLCommand.NAMESPACE_URI, "XMLContent");
+ return new DOMResult(nodeList.item(0));
+
+ }
+
+ /**
+ * Creates a new result document for this <code>InfoboxReadResult</code>
+ * and sets the given <code>resultBytes</code> as content.
+ *
+ * @param resultBytes
+ */
+ void setResultBytes(byte[] resultBytes) {
+
+ xmlDocument = createResponseDocument(resultBytes, false);
+
+ }
+
+ @Override + public void writeTo(Result result, Templates templates) { + writeTo(xmlDocument, result, templates); + }
+
+}
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultImpl.java index 6f07338f..8904eac6 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadResultImpl.java @@ -14,158 +14,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package at.gv.egiz.bku.slcommands.impl;
-
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.NodeList;
-
-import at.buergerkarte.namespaces.securitylayer._1.Base64XMLContentType;
-import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadResponseType;
-import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory;
-import at.buergerkarte.namespaces.securitylayer._1.XMLContentType;
-import at.gv.egiz.bku.slcommands.InfoboxReadResult;
-import at.gv.egiz.bku.slcommands.SLCommand;
-import at.gv.egiz.bku.slcommands.SLCommandFactory;
-import at.gv.egiz.bku.slexceptions.SLRuntimeException;
-
-/**
- * This class implements the result of the security layer command <code>InfoboxReadRequest</code>.
- *
- * @author mcentner
- */
-public class InfoboxReadResultImpl extends SLResultImpl implements
- InfoboxReadResult {
-
- /**
- * Logging facility.
- */
- protected static Log log = LogFactory.getLog(InfoboxReadResultImpl.class);
-
- /**
- * The XML document containing the infobox content.
- */
- Document xmlDocument;
-
- /**
- * Creates the response document from the given <code>binaryContent</code>.
- *
- * @param binaryContent the infobox content
- * @param preserveSpace the value of the <code>preserveSpace</code> parameter
- *
- * @return the created response document
- */
- private Document createResponseDocument(byte[] binaryContent, boolean preserveSpace) {
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- Document doc;
- try {
- doc = dbf.newDocumentBuilder().newDocument();
- } catch (ParserConfigurationException e) {
- // it should always be possible to create a new Document
- log.error("Failed to create XML document.", e);
- throw new SLRuntimeException(e);
- }
-
- ObjectFactory factory = new ObjectFactory();
-
- Base64XMLContentType base64XMLContentType = factory.createBase64XMLContentType();
- if (binaryContent == null) {
- XMLContentType xmlContentType = factory.createXMLContentType();
- if (preserveSpace) {
- xmlContentType.setSpace("preserve");
- }
- base64XMLContentType.setXMLContent(xmlContentType);
- } else {
- base64XMLContentType.setBase64Content(binaryContent);
- }
- InfoboxReadResponseType infoboxReadResponseType = factory.createInfoboxReadResponseType();
- infoboxReadResponseType.setBinaryFileData(base64XMLContentType);
-
- JAXBElement<InfoboxReadResponseType> infoboxReadResponse = factory.createInfoboxReadResponse(infoboxReadResponseType);
-
- JAXBContext context = SLCommandFactory.getJaxbContext();
- try {
- Marshaller marshaller = context.createMarshaller();
- marshaller.marshal(infoboxReadResponse, doc);
- } catch (JAXBException e) {
- log.error("Failed to marshal 'InfoboxReadResponse' document.", e);
- throw new SLRuntimeException(e);
- }
-
- return doc;
-
- }
-
-
- /**
- * @return an XMLResult for marshalling the infobox to
- */
- Result getXmlResult(boolean preserveSpace) {
-
- xmlDocument = createResponseDocument(null, preserveSpace);
-
- NodeList nodeList = xmlDocument.getElementsByTagNameNS(SLCommand.NAMESPACE_URI, "XMLContent");
- return new DOMResult(nodeList.item(0));
-
- }
-
- /**
- * Creates a new result document for this <code>InfoboxReadResult</code>
- * and sets the given <code>resultBytes</code> as content.
- *
- * @param resultBytes
- */
- void setResultBytes(byte[] resultBytes) {
-
- xmlDocument = createResponseDocument(resultBytes, false);
-
- }
-
- @Override
- public void writeTo(Result result) {
-
- try {
- writeTo(result, null);
- } catch (TransformerException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- /* (non-Javadoc)
- * @see at.gv.egiz.bku.slcommands.impl.SLResultImpl#writeTo(javax.xml.transform.Result, javax.xml.transform.Transformer)
- */
- @Override
- public void writeTo(Result result, Transformer transformer) throws TransformerException {
-
- if (transformer == null) {
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- try {
- transformer = transformerFactory.newTransformer();
- } catch (TransformerConfigurationException e) {
- log.error("Failed to create Transformer.", e);
- throw new SLRuntimeException(e);
- }
- }
- transformer.transform(new DOMSource(xmlDocument), result);
-
- }
-
-}
+package at.gv.egiz.bku.slcommands.impl; + +import javax.xml.bind.JAXBElement; +import javax.xml.transform.Result; +import javax.xml.transform.Templates; + +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadDataAssocArrayType; +import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadResponseType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; + +public class InfoboxReadResultImpl extends SLResultImpl { + + /** + * The <code>InfoboxReadResponse</code> + */ + protected InfoboxReadResponseType infoboxReadResponse; + + public InfoboxReadResultImpl(InfoboxReadDataAssocArrayType assocArray) { + + ObjectFactory objectFactory = new ObjectFactory(); + InfoboxReadResponseType infoboxReadResponseType = objectFactory.createInfoboxReadResponseType(); + + infoboxReadResponseType.setAssocArrayData(assocArray); + + this.infoboxReadResponse = infoboxReadResponseType; + } + + @Override + public void writeTo(Result result, Templates templates) { + ObjectFactory objectFactory = new ObjectFactory(); + JAXBElement<InfoboxReadResponseType> response = objectFactory.createInfoboxReadResponse(infoboxReadResponse); + writeTo(response, result, templates); + } + +} diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/NullOperationResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/NullOperationResultImpl.java index ae1f91ce..05986f85 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/NullOperationResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/NullOperationResultImpl.java @@ -18,6 +18,7 @@ package at.gv.egiz.bku.slcommands.impl; import javax.xml.bind.JAXBElement; import javax.xml.transform.Result; +import javax.xml.transform.Templates; import at.buergerkarte.namespaces.securitylayer._1.NullOperationResponseType; import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; @@ -40,8 +41,8 @@ public class NullOperationResultImpl extends SLResultImpl implements NullOperati } @Override - public void writeTo(Result result) { - writeTo(RESPONSE, result); + public void writeTo(Result result, Templates templates) { + writeTo(RESPONSE, result, templates); } } diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/SLResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/SLResultImpl.java index a79382b6..57309182 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/SLResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/SLResultImpl.java @@ -16,24 +16,33 @@ */ package at.gv.egiz.bku.slcommands.impl; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.transform.Result; +import javax.xml.transform.Templates; import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Node; +import at.buergerkarte.namespaces.securitylayer._1.ErrorResponseType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; import at.gv.egiz.bku.slcommands.SLCommandFactory; import at.gv.egiz.bku.slcommands.SLResult; +import at.gv.egiz.bku.slexceptions.SLBindingException; +import at.gv.egiz.bku.slexceptions.SLCommandException; +import at.gv.egiz.bku.slexceptions.SLException; +import at.gv.egiz.bku.slexceptions.SLRuntimeException; /** * This class serves as an abstract base class for the implementation of a @@ -72,46 +81,135 @@ public abstract class SLResultImpl implements SLResult { return resultingMimeType; } + private Marshaller getMarshaller() { + try { + JAXBContext context = SLCommandFactory.getJaxbContext(); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + return marshaller; + } catch (JAXBException e) { + log.fatal("Failed to marshall error response.", e); + throw new SLRuntimeException("Failed to marshall error response.", e); + } + } + + private TransformerHandler getTransformerHandler(Templates templates, Result result) throws SLException { + try { + SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(templates); + transformerHandler.setResult(result); + return transformerHandler; + } catch (TransformerFactoryConfigurationError e) { + log.error("Failed to create an instance of SAXTransformerFactory.", e); + throw new SLBindingException(2000); + } catch (IllegalArgumentException e) { + log.error("Failed to set result for transformation.", e); + throw new SLBindingException(2000); + } catch (TransformerConfigurationException e) { + log.info("Failed to create an instance of SAXTransformerFactory.", e); + throw new SLBindingException(2008); + } + } + + @Override + public void writeTo(Result result) { + writeTo(result, null); + } + + /** - * Writes the given <code>response</code> to the <code>result</code>. + * Writes the given <code>response</code> to the SAX <code>result</code> using + * the given transform <code>templates</code>. * - * @param response the security layer response element - * @param result the result to marshal the response to + * @param response + * @param result + * @param templates */ - @SuppressWarnings("unchecked") - public void writeTo(JAXBElement response, Result result) { + protected void writeTo(JAXBElement<?> response, Result result, Templates templates) { + TransformerHandler transformerHandler = null; + if (templates != null) { + try { + transformerHandler = getTransformerHandler(templates, result); + } catch (SLException e) { + writeErrorTo(e, result, templates); + } + } + + Marshaller marshaller = getMarshaller(); try { - JAXBContext context = SLCommandFactory.getJaxbContext(); - Marshaller marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - marshaller.marshal(response, result); + if (transformerHandler != null) { + marshaller.marshal(response, transformerHandler); + } else { + marshaller.marshal(response, result); + } } catch (JAXBException e) { - // TODO Add throws clause to interface - log.fatal("Failed to marshall JAXBElement.", e); - throw new RuntimeException("Failed to marshall JAXBElement.", e); + log.info("Failed to marshall " + response.getName() + " result." , e); + SLCommandException commandException = new SLCommandException(4000); + writeErrorTo(commandException, result, templates); } + + } + + protected void writeTo(Node node, Result result, Templates templates) { + if (templates == null) { + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(new DOMSource(node), result); + } catch (TransformerConfigurationException e) { + log.error("Failed to create Transformer.", e); + writeErrorTo(new SLException(4000), result, null); + } catch (TransformerException e) { + log.error("Failed to transform result.", e); + writeErrorTo(new SLException(4000), result, null); + } + } else { + try { + Transformer transformer = templates.newTransformer(); + transformer.transform(new DOMSource(node), result); + } catch (TransformerConfigurationException e) { + log.info("Failed to create transformer.", e); + writeErrorTo(new SLException(2008), result, templates); + } catch (TransformerException e) { + log.error("Failed to transform result.", e); + writeErrorTo(new SLException(2008), result, templates); + } + } + } + + protected void writeErrorTo(SLException slException, Result result, Templates templates) { + + TransformerHandler transformerHandler = null; + if (templates != null) { + try { + transformerHandler = getTransformerHandler(templates, result); + } catch (SLException e) { + // write the exception thrown instead of the given one + slException = e; + } + } - /* (non-Javadoc) - * @see at.gv.egiz.bku.slcommands.SLResult#writeTo(javax.xml.transform.Result, javax.xml.transform.Transformer) - */ - @Override - public void writeTo(Result result, Transformer transformer) throws TransformerException { - // TODO Auto-generated method stub - // fixxme: wb added for testing purposes to be completed - // begin hack - if (transformer == null) { - writeTo(result); - return; + ObjectFactory factory = new ObjectFactory(); + ErrorResponseType responseType = factory.createErrorResponseType(); + responseType.setErrorCode(slException.getErrorCode()); + responseType.setInfo(slException.getDetailedMsg()); + JAXBElement<ErrorResponseType> response = factory.createErrorResponse(responseType); + + Marshaller marshaller = getMarshaller(); + try { + if (transformerHandler != null) { + marshaller.marshal(response, transformerHandler); + } else { + marshaller.marshal(response, result); + } + } catch (JAXBException e) { + log.fatal("Failed to marshall error result." , e); + throw new SLRuntimeException("Failed to marshall error result."); } - // just a quick hack to proceed with testing - ByteArrayOutputStream os = new ByteArrayOutputStream(); - writeTo(new StreamResult(os)); - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); - transformer.transform(new StreamSource(is), result); - //end hack + } } |