Search in sources :

Example 6 with XMLSecEvent

use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.

the class TransformIdentity method transform.

@Override
public void transform(final InputStream inputStream) throws XMLStreamException {
    if (getOutputStream() != null) {
        // we have an output stream
        try {
            XMLSecurityUtils.copy(inputStream, getOutputStream());
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
    } else {
        // we have a child transformer
        if (childOutputMethod == null) {
            final XMLSecurityConstants.TransformMethod preferredChildTransformMethod = getTransformer().getPreferredTransformMethod(XMLSecurityConstants.TransformMethod.InputStream);
            switch(preferredChildTransformMethod) {
                case XMLSecEvent:
                    {
                        childOutputMethod = new ChildOutputMethod() {

                            private XMLEventReaderInputProcessor xmlEventReaderInputProcessor;

                            @Override
                            public void transform(Object object) throws XMLStreamException {
                                if (xmlEventReaderInputProcessor == null) {
                                    xmlEventReaderInputProcessor = new XMLEventReaderInputProcessor(null, getXmlInputFactory().createXMLStreamReader(inputStream));
                                }
                                try {
                                    XMLSecEvent xmlSecEvent;
                                    do {
                                        xmlSecEvent = xmlEventReaderInputProcessor.processNextEvent(null);
                                        getTransformer().transform(xmlSecEvent);
                                    } while (xmlSecEvent.getEventType() != XMLStreamConstants.END_DOCUMENT);
                                } catch (XMLSecurityException e) {
                                    throw new XMLStreamException(e);
                                }
                            }

                            @Override
                            public void doFinal() throws XMLStreamException {
                                getTransformer().doFinal();
                            }
                        };
                        break;
                    }
                case InputStream:
                    {
                        childOutputMethod = new ChildOutputMethod() {

                            @Override
                            public void transform(Object object) throws XMLStreamException {
                                getTransformer().transform(inputStream);
                            }

                            @Override
                            public void doFinal() throws XMLStreamException {
                                getTransformer().doFinal();
                            }
                        };
                        break;
                    }
            }
        }
        if (childOutputMethod != null) {
            childOutputMethod.transform(inputStream);
        }
    }
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) XMLEventReaderInputProcessor(org.apache.xml.security.stax.impl.processor.input.XMLEventReaderInputProcessor) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Example 7 with XMLSecEvent

use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.

the class XMLSecurityStreamReader method getNamespaceCount.

@SuppressWarnings("unchecked")
@Override
public int getNamespaceCount() {
    XMLSecEvent xmlSecEvent = getCurrentEvent();
    switch(xmlSecEvent.getEventType()) {
        case START_ELEMENT:
            return xmlSecEvent.asStartElement().getOnElementDeclaredNamespaces().size();
        case END_ELEMENT:
            int count = 0;
            Iterator<Namespace> namespaceIterator = xmlSecEvent.asEndElement().getNamespaces();
            while (namespaceIterator.hasNext()) {
                namespaceIterator.next();
                count++;
            }
            return count;
        default:
            throw new IllegalStateException(ERR_STATE_NOT_ELEM);
    }
}
Also used : Namespace(javax.xml.stream.events.Namespace) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Example 8 with XMLSecEvent

use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.

the class XMLSecurityStreamReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    XMLSecEvent xmlSecEvent = getCurrentEvent();
    if (xmlSecEvent.getEventType() != START_ELEMENT) {
        throw new XMLStreamException("Not positioned on a start element");
    }
    StringBuilder stringBuilder = new StringBuilder();
    /**
     * Need to loop to get rid of PIs, comments
     */
    loop: while (true) {
        int type = next();
        switch(type) {
            case END_ELEMENT:
                break loop;
            case COMMENT:
            case PROCESSING_INSTRUCTION:
                continue loop;
            case ENTITY_REFERENCE:
            case SPACE:
            case CDATA:
            case CHARACTERS:
                stringBuilder.append(getText());
                break;
            default:
                throw new XMLStreamException("Expected a text token, got " + type + ".");
        }
    }
    return stringBuilder.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Example 9 with XMLSecEvent

use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.

the class XMLSecurityStreamReader method getAttributeValue.

@Override
public String getAttributeValue(String namespaceURI, String localName) {
    XMLSecEvent xmlSecEvent = getCurrentEvent();
    if (xmlSecEvent.getEventType() != START_ELEMENT) {
        throw new IllegalStateException(ERR_STATE_NOT_STELEM);
    }
    Attribute attribute = xmlSecEvent.asStartElement().getAttributeByName(new QName(namespaceURI, localName));
    if (attribute != null) {
        return attribute.getValue();
    }
    return null;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Example 10 with XMLSecEvent

use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.

the class Canonicalizer20010315Test method c14nAndCompare.

// /**
// * The XPath data model represents data using UCS characters.
// * Implementations MUST use XML processors that support UTF-8 and UTF-16
// * and translate to the UCS character domain. For UTF-16, the leading byte
// * order mark is treated as an artifact of encoding and stripped from the
// * UCS character data (subsequent zero width non-breaking spaces appearing
// * within the UTF-16 data are not removed) [UTF-16, Section 3.2]. Support
// * for ISO-8859-1 encoding is RECOMMENDED, and all other character encodings
// * are OPTIONAL.
// *
// * $todo$ implement the test
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testTranslationFromUTF16toUTF8() throws Exception {
// 
// String val =
// "<UTF16>The german &amp;auml (which is Unicode &amp;#xE4;):  &quot;&#xE4;&quot;</UTF16>";
// byte utf16[] = convertToUTF16(val.getBytes());
// Canonicalizer c14n =
// Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
// byte c14nBytes[] = c14n.canonicalize(utf16);
// org.xml.sax.EntityResolver resolver = new TestVectorResolver();
// InputStream refStream = resolver.resolveEntity(
// null,
// prefix + "/in/testTranslationFromUTF16toUTF8.xml")
// .getByteStream();
// byte refBytes[] = JavaUtils.getBytesFromStream(refStream);
// boolean equal = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
// 
// assertTrue("Parser does not translate to UCS character domain", equal);
// }
// 
// /**
// * Method testXMLAttributes1
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes1() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included                 >"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method testXMLAttributes2
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes2() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='uk'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included                 >"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"uk\">"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method testXMLAttributes3
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes3() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included xml:lang='de'>"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included xml:lang=\"de\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method testXMLAttributes4
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes4() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included                 >"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included>"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method testXMLAttributes5
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes5() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:space='preserve' xml:lang='uk'>"
// + "<included                 >"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included>"
// + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method testXMLAttributes6
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes6() throws Exception {
// //J-
// String input = ""
// + "<included xml:space='preserve'  xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included>"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
// 
// String definedOutput = ""
// + "<included xml:lang=\"de\" xml:space=\"preserve\">"
// + "<included>"
// + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
// 
// /**
// * Method doTestXMLAttributes
// *
// * @param input
// * @param definedOutput
// * @param writeResultsToFile
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// private static boolean doTestXMLAttributes(
// String input, String definedOutput) throws Exception {
// 
// DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
// 
// dfactory.setNamespaceAware(true);
// dfactory.setValidating(true);
// 
// DocumentBuilder db = dfactory.newDocumentBuilder();
// 
// db.setErrorHandler(new org.apache.xml.security.utils
// .IgnoreAllErrorHandler());
// 
// Document doc = db.parse(new ByteArrayInputStream(input.getBytes()));
// Canonicalizer c14nizer =
// Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
// CachedXPathAPI xpathAPI = new CachedXPathAPI();
// 
// //XMLUtils.circumventBug2650(doc);
// 
// NodeList nodes =
// xpathAPI.selectNodeList(doc, "(//*[local-name()='included'] | //@*[parent::node()[local-name()='included']])");
// byte result[] = c14nizer.canonicalizeXPathNodeSet(nodes);
// byte defined[] = definedOutput.getBytes();
// assertEquals(definedOutput, new String(result));
// return java.security.MessageDigest.isEqual(defined, result);
// }
/**
 * Method c14nAndCompare
 */
private void c14nAndCompare(URL fileIn, URL fileRef, boolean ommitComments) throws Exception {
    CanonicalizerBase canonicalizerBase;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (ommitComments) {
        canonicalizerBase = new Canonicalizer20010315_OmitCommentsTransformer();
        canonicalizerBase.setOutputStream(baos);
    } else {
        canonicalizerBase = new Canonicalizer20010315_WithCommentsTransformer();
        canonicalizerBase.setOutputStream(baos);
    }
    XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(fileIn.openStream());
    while (xmlSecEventReader.hasNext()) {
        XMLSecEvent xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
        canonicalizerBase.transform(xmlSecEvent);
    }
    // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
    // byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
    byte[] refBytes = getBytesFromResource(fileRef);
    // if everything is OK, result is true; we do a binary compare, byte by byte
    boolean result = java.security.MessageDigest.isEqual(refBytes, baos.toByteArray());
    if (!result) {
        assertEquals(new String(baos.toByteArray(), StandardCharsets.UTF_8), new String(refBytes, StandardCharsets.UTF_8));
    }
    assertTrue(result);
}
Also used : Canonicalizer20010315_WithCommentsTransformer(org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_WithCommentsTransformer) Canonicalizer20010315_OmitCommentsTransformer(org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CanonicalizerBase(org.apache.xml.security.stax.impl.transformer.canonicalizer.CanonicalizerBase) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Aggregations

XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)46 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Test (org.junit.Test)25 XMLEventReader (javax.xml.stream.XMLEventReader)22 XMLSecurityConstants (org.apache.xml.security.stax.ext.XMLSecurityConstants)16 InputStream (java.io.InputStream)15 OutputStream (java.io.OutputStream)14 Map (java.util.Map)14 QName (javax.xml.namespace.QName)14 Transformer (org.apache.xml.security.stax.ext.Transformer)14 IOException (java.io.IOException)10 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)9 XMLStreamException (javax.xml.stream.XMLStreamException)6 Canonicalizer20010315_OmitCommentsTransformer (org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer)6 ArrayList (java.util.ArrayList)5 TransformBase64Decode (org.apache.xml.security.stax.impl.transformer.TransformBase64Decode)5 TransformIdentity (org.apache.xml.security.stax.impl.transformer.TransformIdentity)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 Canonicalizer20010315_WithCommentsTransformer (org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_WithCommentsTransformer)4 UnixInputStream (org.apache.xml.security.test.stax.utils.UnixInputStream)4