Search in sources :

Example 86 with Attribute

use of com.github.zhenwei.core.asn1.x509.Attribute in project kitodo-production by kitodo.

the class ExportXmlLog method startMultipleExport.

/**
 * This method exports the production metadata for al list of processes as a
 * single file to a given stream.
 *
 * @param docketDataList
 *            a list of Docket data
 * @param outputStream
 *            The output stream, to write the docket to.
 */
void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace(NAMESPACE);
    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi);
    root.setAttribute(attSchema);
    for (DocketData docketData : docketDataList) {
        Document doc = createDocument(docketData, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }
    XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
    try {
        outp.output(answer, outputStream);
    } catch (IOException e) {
        logger.error("Generating XML Output failed.", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error("Closing the output stream failed.", e);
            }
        }
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Attribute(org.jdom2.Attribute) DocketData(org.kitodo.api.docket.DocketData) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) Namespace(org.jdom2.Namespace)

Example 87 with Attribute

use of com.github.zhenwei.core.asn1.x509.Attribute in project kitodo-production by kitodo.

the class ExportXmlLog method createDocument.

/**
 * This method creates a new xml document with process metadata.
 *
 * @param docketData
 *            the docketData to export
 * @return a new xml document
 */
private Document createDocument(DocketData docketData, boolean addNamespace) {
    Element processElm = new Element("process");
    final Document doc = new Document(processElm);
    processElm.setAttribute("processID", String.valueOf(docketData.getProcessId()));
    Namespace xmlns = Namespace.getNamespace(NAMESPACE);
    processElm.setNamespace(xmlns);
    // namespace declaration
    if (addNamespace) {
        Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        processElm.addNamespaceDeclaration(xsi);
        Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi);
        processElm.setAttribute(attSchema);
    }
    // process information
    ArrayList<Element> processElements = new ArrayList<>();
    Element processTitle = new Element("title", xmlns);
    processTitle.setText(docketData.getProcessName());
    processElements.add(processTitle);
    Element project = new Element("project", xmlns);
    project.setText(docketData.getProjectName());
    processElements.add(project);
    Element date = new Element("time", xmlns);
    date.setAttribute("type", "creation date");
    date.setText(String.valueOf(docketData.getCreationDate()));
    processElements.add(date);
    Element ruleset = new Element("ruleset", xmlns);
    ruleset.setText(docketData.getRulesetName());
    processElements.add(ruleset);
    Element comment = new Element("comment", xmlns);
    comment.setText(docketData.getComment());
    processElements.add(comment);
    List<Element> processProperties = prepareProperties(docketData.getProcessProperties(), xmlns);
    if (!processProperties.isEmpty()) {
        Element properties = new Element(PROPERTIES, xmlns);
        properties.addContent(processProperties);
        processElements.add(properties);
    }
    // template information
    ArrayList<Element> templateElements = new ArrayList<>();
    Element template = new Element("original", xmlns);
    ArrayList<Element> templateProperties = new ArrayList<>();
    if (docketData.getTemplateProperties() != null) {
        for (Property prop : docketData.getTemplateProperties()) {
            Element property = new Element(PROPERTY, xmlns);
            property.setAttribute(PROPERTY_IDENTIFIER, prop.getTitle());
            if (prop.getValue() != null) {
                property.setAttribute(VALUE, replacer(prop.getValue()));
            } else {
                property.setAttribute(VALUE, "");
            }
            Element label = new Element(LABEL, xmlns);
            label.setText(prop.getTitle());
            property.addContent(label);
            templateProperties.add(property);
            if (prop.getTitle().equals("Signatur")) {
                Element secondProperty = new Element(PROPERTY, xmlns);
                secondProperty.setAttribute(PROPERTY_IDENTIFIER, prop.getTitle() + "Encoded");
                if (prop.getValue() != null) {
                    secondProperty.setAttribute(VALUE, "vorl:" + replacer(prop.getValue()));
                    Element secondLabel = new Element(LABEL, xmlns);
                    secondLabel.setText(prop.getTitle());
                    secondProperty.addContent(secondLabel);
                    templateProperties.add(secondProperty);
                }
            }
        }
    }
    if (!templateProperties.isEmpty()) {
        Element properties = new Element(PROPERTIES, xmlns);
        properties.addContent(templateProperties);
        template.addContent(properties);
    }
    templateElements.add(template);
    Element templates = new Element("originals", xmlns);
    templates.addContent(templateElements);
    processElements.add(templates);
    // digital document information
    ArrayList<Element> docElements = new ArrayList<>();
    Element dd = new Element("digitalDocument", xmlns);
    List<Element> docProperties = prepareProperties(docketData.getWorkpieceProperties(), xmlns);
    if (!docProperties.isEmpty()) {
        Element properties = new Element(PROPERTIES, xmlns);
        properties.addContent(docProperties);
        dd.addContent(properties);
    }
    docElements.add(dd);
    Element digdoc = new Element("digitalDocuments", xmlns);
    digdoc.addContent(docElements);
    processElements.add(digdoc);
    processElm.setContent(processElements);
    return doc;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) Property(org.kitodo.api.docket.Property) Namespace(org.jdom2.Namespace)

Example 88 with Attribute

use of com.github.zhenwei.core.asn1.x509.Attribute in project acme4j by shred.

the class CSRBuilderTest method csrTest.

/**
 * Checks if the CSR contains the right parameters.
 * <p>
 * This is not supposed to be a Bouncy Castle test. If the
 * {@link PKCS10CertificationRequest} contains the right parameters, we assume that
 * Bouncy Castle encodes it properly.
 */
private void csrTest(PKCS10CertificationRequest csr) {
    X500Name name = csr.getSubject();
    try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
        softly.assertThat(name.getRDNs(BCStyle.CN)).as("CN").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("abc.de");
        softly.assertThat(name.getRDNs(BCStyle.C)).as("C").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("XX");
        softly.assertThat(name.getRDNs(BCStyle.L)).as("L").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testville");
        softly.assertThat(name.getRDNs(BCStyle.O)).as("O").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testing Co");
        softly.assertThat(name.getRDNs(BCStyle.OU)).as("OU").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testunit");
        softly.assertThat(name.getRDNs(BCStyle.ST)).as("ST").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("ABC");
    }
    Attribute[] attr = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest);
    assertThat(attr).hasSize(1);
    ASN1Encodable[] extensions = attr[0].getAttrValues().toArray();
    assertThat(extensions).hasSize(1);
    GeneralNames names = GeneralNames.fromExtensions((Extensions) extensions[0], Extension.subjectAlternativeName);
    assertThat(names.getNames()).filteredOn(gn -> gn.getTagNo() == GeneralName.dNSName).extracting(gn -> ASN1IA5String.getInstance(gn.getName()).getString()).containsExactlyInAnyOrder("abc.de", "fg.hi", "jklm.no", "pqr.st", "uv.wx", "y.z", "*.wild.card", "ide1.nt", "ide2.nt", "ide3.nt");
    assertThat(names.getNames()).filteredOn(gn -> gn.getTagNo() == GeneralName.iPAddress).extracting(gn -> getIP(gn.getName()).getHostAddress()).containsExactlyInAnyOrder("192.168.0.1", "192.168.0.2", "10.0.0.1", "10.0.0.2", "fd00:0:0:0:0:0:0:1", "fd00:0:0:0:0:0:0:2", "192.168.5.5", "192.168.5.6", "192.168.5.7");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Identifier(org.shredzone.acme4j.Identifier) PKCSObjectIdentifiers(org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers) Extension(org.bouncycastle.asn1.x509.Extension) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Security(java.security.Security) DEROctetString(org.bouncycastle.asn1.DEROctetString) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) InetAddress(java.net.InetAddress) X500Name(org.bouncycastle.asn1.x500.X500Name) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ASN1IA5String(org.bouncycastle.asn1.ASN1IA5String) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AutoCloseableSoftAssertions(org.assertj.core.api.AutoCloseableSoftAssertions) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) PEMParser(org.bouncycastle.openssl.PEMParser) StringWriter(java.io.StringWriter) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StandardCharsets(java.nio.charset.StandardCharsets) Extensions(org.bouncycastle.asn1.x509.Extensions) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.junit.jupiter.api.Test) GeneralName(org.bouncycastle.asn1.x509.GeneralName) StringReader(java.io.StringReader) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) Attribute(org.bouncycastle.asn1.pkcs.Attribute) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) Attribute(org.bouncycastle.asn1.pkcs.Attribute) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) AutoCloseableSoftAssertions(org.assertj.core.api.AutoCloseableSoftAssertions)

Example 89 with Attribute

use of com.github.zhenwei.core.asn1.x509.Attribute in project acme4j by shred.

the class SMIMECSRBuilderTest method smimeCsrTest.

/**
 * Checks if the S/MIME CSR contains the right parameters.
 * <p>
 * This is not supposed to be a Bouncy Castle test. If the
 * {@link PKCS10CertificationRequest} contains the right parameters, we assume that
 * Bouncy Castle encodes it properly.
 */
private void smimeCsrTest(PKCS10CertificationRequest csr) {
    X500Name name = csr.getSubject();
    try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
        softly.assertThat(name.getRDNs(BCStyle.CN)).as("CN").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("mail@example.com");
        softly.assertThat(name.getRDNs(BCStyle.C)).as("C").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("XX");
        softly.assertThat(name.getRDNs(BCStyle.L)).as("L").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testville");
        softly.assertThat(name.getRDNs(BCStyle.O)).as("O").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testing Co");
        softly.assertThat(name.getRDNs(BCStyle.OU)).as("OU").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("Testunit");
        softly.assertThat(name.getRDNs(BCStyle.ST)).as("ST").extracting(rdn -> rdn.getFirst().getValue().toString()).contains("ABC");
    }
    Attribute[] attr = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest);
    assertThat(attr).hasSize(1);
    ASN1Encodable[] extensions = attr[0].getAttrValues().toArray();
    assertThat(extensions).hasSize(1);
    GeneralNames names = GeneralNames.fromExtensions((Extensions) extensions[0], Extension.subjectAlternativeName);
    assertThat(names.getNames()).filteredOn(gn -> gn.getTagNo() == GeneralName.rfc822Name).extracting(gn -> DERIA5String.getInstance(gn.getName()).getString()).containsExactlyInAnyOrder("mail@example.com", "info@example.com", "sales@example.com", "shop@example.com", "support@example.com", "help@example.com");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PKCSObjectIdentifiers(org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers) Extension(org.bouncycastle.asn1.x509.Extension) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Security(java.security.Security) DERBitString(org.bouncycastle.asn1.DERBitString) AddressException(jakarta.mail.internet.AddressException) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) InternetAddress(jakarta.mail.internet.InternetAddress) X500Name(org.bouncycastle.asn1.x500.X500Name) BeforeAll(org.junit.jupiter.api.BeforeAll) AutoCloseableSoftAssertions(org.assertj.core.api.AutoCloseableSoftAssertions) DERIA5String(org.bouncycastle.asn1.DERIA5String) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) UTF_8(java.nio.charset.StandardCharsets.UTF_8) PEMParser(org.bouncycastle.openssl.PEMParser) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Extensions(org.bouncycastle.asn1.x509.Extensions) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Test(org.junit.jupiter.api.Test) GeneralName(org.bouncycastle.asn1.x509.GeneralName) StringReader(java.io.StringReader) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) Attribute(org.bouncycastle.asn1.pkcs.Attribute) KeyUsage(org.bouncycastle.asn1.x509.KeyUsage) KeyPairUtils(org.shredzone.acme4j.util.KeyPairUtils) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) Attribute(org.bouncycastle.asn1.pkcs.Attribute) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) AutoCloseableSoftAssertions(org.assertj.core.api.AutoCloseableSoftAssertions)

Example 90 with Attribute

use of com.github.zhenwei.core.asn1.x509.Attribute in project vos by opencadc.

the class TransferReader method parseTransfer.

private Transfer parseTransfer(Document document, String targetScheme) throws URISyntaxException {
    Element root = document.getRootElement();
    Namespace vosNS = root.getNamespace();
    Attribute versionAttr = root.getAttribute("version");
    int version;
    if (VOSPACE_NS_20.equals(vosNS.getURI())) {
        version = VOS.VOSPACE_20;
        // Check the minor version attribute
        if (versionAttr != null && VOSPACE_MINOR_VERSION_21.equals(versionAttr.getValue())) {
            version = VOS.VOSPACE_21;
        }
    } else {
        throw new IllegalArgumentException("unexpected VOSpace namespace: " + vosNS.getURI());
    }
    Direction direction = parseDirection(root, vosNS);
    // String serviceUrl; // not in XML yet
    List<URI> targets = parseTargets(root, vosNS, targetScheme);
    View view = null;
    Parameter param = null;
    List views = root.getChildren("view", vosNS);
    if (views != null && views.size() > 0) {
        Element v = (Element) views.get(0);
        view = new View(new URI(v.getAttributeValue("uri")));
        List params = v.getChildren("param", vosNS);
        if (params != null) {
            for (Object o : params) {
                Element p = (Element) o;
                param = new Parameter(new URI(p.getAttributeValue("uri")), p.getText());
                view.getParameters().add(param);
            }
        }
    }
    List<Protocol> protocols = parseProtocols(root, vosNS, version);
    String keepBytesStr = root.getChildText("keepBytes", vosNS);
    boolean keepBytes = true;
    if (keepBytesStr != null)
        keepBytes = keepBytesStr.equalsIgnoreCase("true");
    Transfer ret = new Transfer(direction);
    ret.getTargets().addAll(targets);
    ret.setView(view);
    if (protocols != null) {
        // parseProtocols() above can potentially return null
        ret.getProtocols().addAll(protocols);
    }
    ret.setKeepBytes(keepBytes);
    ret.version = version;
    // optional param(s) added in VOSpace-2.1
    if (version >= VOS.VOSPACE_21) {
        List<Element> params = root.getChildren("param", vosNS);
        for (Element pe : params) {
            String uri = pe.getAttributeValue("uri");
            if (VOS.PROPERTY_URI_CONTENTLENGTH.equals(uri)) {
                try {
                    ret.setContentLength(new Long(pe.getText()));
                } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException("invalid " + VOS.PROPERTY_URI_CONTENTLENGTH + ": " + pe.getText());
                }
            } else {
                log.debug("skip unknown param: " + uri);
            }
        }
    }
    return ret;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) URI(java.net.URI) Namespace(org.jdom2.Namespace) Parameter(ca.nrc.cadc.vos.View.Parameter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Attribute (org.jdom2.Attribute)316 Element (org.jdom2.Element)210 IOException (java.io.IOException)98 ArrayList (java.util.ArrayList)75 Attribute (ucar.nc2.Attribute)65 List (java.util.List)46 Document (org.jdom2.Document)43 Variable (ucar.nc2.Variable)39 HashMap (java.util.HashMap)26 Extensions (org.bouncycastle.asn1.x509.Extensions)26 X509Certificate (java.security.cert.X509Certificate)24 Namespace (org.jdom2.Namespace)24 File (java.io.File)23 Attribute (org.bouncycastle.asn1.pkcs.Attribute)21 GeneralName (org.bouncycastle.asn1.x509.GeneralName)21 Array (ucar.ma2.Array)21 Test (org.junit.Test)20 GeneralNames (org.bouncycastle.asn1.x509.GeneralNames)19 Dimension (ucar.nc2.Dimension)19 Map (java.util.Map)17