Search in sources :

Example 1 with OctetStreamData

use of javax.xml.crypto.OctetStreamData in project poi by apache.

the class RelationshipTransformService method transform.

public Data transform(Data data, XMLCryptoContext context) throws TransformException {
    LOG.log(POILogger.DEBUG, "transform(data,context)");
    LOG.log(POILogger.DEBUG, "data java type: " + data.getClass().getName());
    OctetStreamData octetStreamData = (OctetStreamData) data;
    LOG.log(POILogger.DEBUG, "URI: " + octetStreamData.getURI());
    InputStream octetStream = octetStreamData.getOctetStream();
    RelationshipsDocument relDoc;
    try {
        relDoc = RelationshipsDocument.Factory.parse(octetStream, DEFAULT_XML_OPTIONS);
    } catch (Exception e) {
        throw new TransformException(e.getMessage(), e);
    }
    LOG.log(POILogger.DEBUG, "relationships document", relDoc);
    CTRelationships rels = relDoc.getRelationships();
    List<CTRelationship> relList = rels.getRelationshipList();
    Iterator<CTRelationship> relIter = rels.getRelationshipList().iterator();
    while (relIter.hasNext()) {
        CTRelationship rel = relIter.next();
        /*
             * See: ISO/IEC 29500-2:2008(E) - 13.2.4.24 Relationships Transform
             * Algorithm.
             */
        if (!this.sourceIds.contains(rel.getId())) {
            LOG.log(POILogger.DEBUG, "removing element: " + rel.getId());
            relIter.remove();
        } else {
            if (!rel.isSetTargetMode()) {
                rel.setTargetMode(STTargetMode.INTERNAL);
            }
        }
    }
    // TODO: remove non element nodes ???
    LOG.log(POILogger.DEBUG, "# Relationship elements", relList.size());
    XmlSort.sort(rels, new Comparator<XmlCursor>() {

        public int compare(XmlCursor c1, XmlCursor c2) {
            String id1 = ((CTRelationship) c1.getObject()).getId();
            String id2 = ((CTRelationship) c2.getObject()).getId();
            return id1.compareTo(id2);
        }
    });
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XmlOptions xo = new XmlOptions();
        xo.setSaveNoXmlDecl();
        relDoc.save(bos, xo);
        return new OctetStreamData(new ByteArrayInputStream(bos.toByteArray()));
    } catch (IOException e) {
        throw new TransformException(e.getMessage(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XmlOptions(org.apache.xmlbeans.XmlOptions) TransformException(javax.xml.crypto.dsig.TransformException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MarshalException(javax.xml.crypto.MarshalException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) XmlCursor(org.apache.xmlbeans.XmlCursor) CTRelationship(org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationship) RelationshipsDocument(org.openxmlformats.schemas.xpackage.x2006.relationships.RelationshipsDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) CTRelationships(org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationships) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 2 with OctetStreamData

use of javax.xml.crypto.OctetStreamData in project poi by apache.

the class OOXMLURIDereferencer method dereference.

public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
    if (baseUriDereferencer == null) {
        baseUriDereferencer = signatureConfig.getSignatureFactory().getURIDereferencer();
    }
    if (null == uriReference) {
        throw new NullPointerException("URIReference cannot be null");
    }
    if (null == context) {
        throw new NullPointerException("XMLCrytoContext cannot be null");
    }
    URI uri;
    try {
        uri = new URI(uriReference.getURI());
    } catch (URISyntaxException e) {
        throw new URIReferenceException("could not URL decode the uri: " + uriReference.getURI(), e);
    }
    PackagePart part = findPart(uri);
    if (part == null) {
        LOG.log(POILogger.DEBUG, "cannot resolve, delegating to base DOM URI dereferencer", uri);
        return this.baseUriDereferencer.dereference(uriReference, context);
    }
    InputStream dataStream;
    try {
        dataStream = part.getInputStream();
        // workaround for office 2007 pretty-printed .rels files
        if (part.getPartName().toString().endsWith(".rels")) {
            // although xmlsec has an option to ignore line breaks, currently this
            // only affects .rels files, so we only modify these
            // http://stackoverflow.com/questions/4728300
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            for (int ch; (ch = dataStream.read()) != -1; ) {
                if (ch == 10 || ch == 13)
                    continue;
                bos.write(ch);
            }
            dataStream = new ByteArrayInputStream(bos.toByteArray());
        }
    } catch (IOException e) {
        throw new URIReferenceException("I/O error: " + e.getMessage(), e);
    }
    return new OctetStreamData(dataStream, uri.toString(), null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) URIReferenceException(javax.xml.crypto.URIReferenceException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 3 with OctetStreamData

use of javax.xml.crypto.OctetStreamData in project syndesis by syndesisio.

the class BaseSwaggerGeneratorExampleTest method c14Xml.

private static String c14Xml(final String xml) {
    if (xml == null) {
        return null;
    }
    try {
        final DocumentBuilder documentBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        final TransformService transformation = TransformService.getInstance(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, "DOM");
        transformation.init(null);
        final NodeList allElements = document.getElementsByTagName("*");
        final List<Node> elements = new ArrayList<>();
        for (int i = 0; i < allElements.getLength(); i++) {
            elements.add(allElements.item(i));
        }
        final OctetStreamData data = (OctetStreamData) transformation.transform((NodeSetData) elements::iterator, null);
        try (final InputStream stream = data.getOctetStream()) {
            final byte[] buffy = new byte[stream.available()];
            stream.read(buffy);
            return new String(buffy, StandardCharsets.UTF_8);
        }
    } catch (GeneralSecurityException | TransformException | SAXException | IOException | ParserConfigurationException e) {
        throw new AssertionError(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) TransformException(javax.xml.crypto.dsig.TransformException) TransformService(javax.xml.crypto.dsig.TransformService) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) NodeSetData(javax.xml.crypto.NodeSetData) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OctetStreamData(javax.xml.crypto.OctetStreamData)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OctetStreamData (javax.xml.crypto.OctetStreamData)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TransformException (javax.xml.crypto.dsig.TransformException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 ArrayList (java.util.ArrayList)1 MarshalException (javax.xml.crypto.MarshalException)1 NodeSetData (javax.xml.crypto.NodeSetData)1 URIReferenceException (javax.xml.crypto.URIReferenceException)1 TransformService (javax.xml.crypto.dsig.TransformService)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1