예제 #1
0
  /**
   * Test method readXMLDOM
   *
   * @throws SAXException never
   */
  @Test
  public void testReadXMLDOMInputStream() throws SAXException {
    Document doc =
        DOMReader.readXMLDOM(new StringInputStream("<root/>", CCharset.CHARSET_ISO_8859_1_OBJ));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(
                "<?xml version=\"1.0\"?>\n<root/>", CCharset.CHARSET_ISO_8859_1_OBJ));
    assertNotNull(doc);

    try {
      // null reader not allowed
      DOMReader.readXMLDOM((InputStream) null);
      fail();
    } catch (final NullPointerException ex) {
    }

    try {
      // non-XML
      DOMReader.readXMLDOM(new NonBlockingByteArrayInputStream(new byte[0]));
      fail();
    } catch (final SAXException ex) {
    }

    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(
                "<?xml version=\"1.0\"?>\n<root/>", CCharset.CHARSET_ISO_8859_1_OBJ));
    assertNotNull(doc);
  }
예제 #2
0
 @Test
 public void testOtherSources() throws SAXException {
   assertNotNull(
       DOMReader.readXMLDOM(
           new CachingSAXInputSource(new ClassPathResource("xml/buildinfo.xml"))));
   assertNotNull(
       DOMReader.readXMLDOM(
           new ReadableResourceSAXInputSource(new ClassPathResource("xml/buildinfo.xml"))));
 }
예제 #3
0
  /**
   * Test method readXMLDOM
   *
   * @throws SAXException never
   */
  @Test
  public void testReadXMLDOMString() throws SAXException {
    Document doc = DOMReader.readXMLDOM("<root/>");
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM("<?xml version=\"1.0\"?>\n" + "<root/>");
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM("<?xml version=\"1.0\"?>\n" + "<root></root>");
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM("<?xml version=\"1.0\"?>\n" + "<root><![CDATA[x<>]]></root>");
    assertNotNull(doc);

    try {
      // null reader not allowed
      DOMReader.readXMLDOM((Reader) null);
      fail();
    } catch (final NullPointerException ex) {
    }

    try {
      // null string not allowed
      DOMReader.readXMLDOM((String) null);
      fail();
    } catch (final NullPointerException ex) {
    }

    try {
      // non-XML
      DOMReader.readXMLDOM("");
      fail();
    } catch (final SAXException ex) {
    }
  }
예제 #4
0
  @Test
  public void testReadNotation() throws SAXException {
    // Read file with processing instruction
    final Document doc = DOMReader.readXMLDOM(new ClassPathResource("xml/xml-notation.xml"));
    assertNotNull(doc);

    // Write again
    assertNotNull(XMLWriter.getXMLString(doc));
  }
예제 #5
0
  @Test
  public void testExternalEntityExpansion() throws SAXException, MalformedURLException {
    // Include a dummy file
    final File aFile = new File("src/test/resources/test1.txt");
    assertTrue(aFile.exists());
    final String sFileContent =
        StreamHelper.getAllBytesAsString(
            new FileSystemResource(aFile), CCharset.CHARSET_ISO_8859_1_OBJ);

    // The XML with XXE problem
    final String sXML =
        "<?xml version='1.0' encoding='utf-8'?>"
            + "<!DOCTYPE root ["
            + " <!ELEMENT root ANY >"
            + " <!ENTITY xxe SYSTEM \""
            + aFile.toURI().toURL().toExternalForm()
            + "\" >]>"
            + "<root>&xxe;</root>";
    final DOMReaderSettings aDRS =
        new DOMReaderSettings()
            .setEntityResolver(
                new EntityResolver() {
                  public InputSource resolveEntity(final String publicId, final String systemId)
                      throws SAXException, IOException {
                    // Read as URL
                    return InputSourceFactory.create(new URLResource(systemId));
                  }
                });

    // Read successful - entity expansion!
    final Document aDoc = DOMReader.readXMLDOM(sXML, aDRS);
    assertNotNull(aDoc);
    assertEquals(sFileContent, aDoc.getDocumentElement().getTextContent());

    // Should fail because inline DTD is present
    try {
      DOMReader.readXMLDOM(
          sXML, aDRS.getClone().setFeatureValues(EXMLParserFeature.AVOID_XXE_SETTINGS));
      fail();
    } catch (final SAXParseException ex) {
      // Expected
      assertTrue(ex.getMessage().contains("http://apache.org/xml/features/disallow-doctype-decl"));
    }
  }
예제 #6
0
  @Test
  public void testEntityExpansionLimit() throws SAXException {
    // The XML with XXE problem
    final String sXML =
        "<?xml version='1.0' encoding='utf-8'?>"
            + "<!DOCTYPE root ["
            + " <!ELEMENT root ANY >"
            + " <!ENTITY e1 \"value\" >"
            + " <!ENTITY e2 \"&e1;&e1;&e1;&e1;&e1;&e1;&e1;&e1;&e1;&e1;\" >"
            + " <!ENTITY e3 \"&e2;&e2;&e2;&e2;&e2;&e2;&e2;&e2;&e2;&e2;\" >"
            + " <!ENTITY e4 \"&e3;&e3;&e3;&e3;&e3;&e3;&e3;&e3;&e3;&e3;\" >"
            + " <!ENTITY e5 \"&e4;&e4;&e4;&e4;&e4;&e4;&e4;&e4;&e4;&e4;\" >"
            + " <!ENTITY e6 \"&e5;&e5;&e5;&e5;&e5;&e5;&e5;&e5;&e5;&e5;\" >"
            // +
            // " <!ENTITY e7 \"&e6;&e6;&e6;&e6;&e6;&e6;&e6;&e6;&e6;&e6;\" >"
            // +
            // " <!ENTITY e8 \"&e7;&e7;&e7;&e7;&e7;&e7;&e7;&e7;&e7;&e7;\" >"
            // +
            // " <!ENTITY e9 \"&e8;&e8;&e8;&e8;&e8;&e8;&e8;&e8;&e8;&e8;\" >"
            // +
            // " <!ENTITY e10 \"&e9;&e9;&e9;&e9;&e9;&e9;&e9;&e9;&e9;&e9;\" >"
            + "]>"
            + "<root>&e6;</root>";
    final DOMReaderSettings aDRS = new DOMReaderSettings();

    // Read successful - entity expansion!
    final Document aDoc = DOMReader.readXMLDOM(sXML, aDRS);
    assertNotNull(aDoc);
    assertEquals(
        StringHelper.getRepeated("value", (int) Math.pow(10, 5)),
        aDoc.getDocumentElement().getTextContent());

    // Should fail because too many entity expansions
    try {
      DOMReader.readXMLDOM(
          sXML, aDRS.getClone().setFeatureValues(EXMLParserFeature.AVOID_DOS_SETTINGS));
      fail();
    } catch (final SAXParseException ex) {
      // Expected
      assertTrue(ex.getMessage().contains("entity expansions"));
    }
  }
  @Test
  public void testValidateSchemaPresent() throws SAXException {
    final Schema aSchema1 = XMLSchemaCache.getInstance().getSchema(XSD1);
    final Schema aSchema2 = XMLSchemaCache.getInstance().getSchema(XSD2, XSD1);

    // Different source type
    Document aDoc = DOMReader.readXMLDOM(XML1);
    IResourceErrorGroup aErrors = XMLSchemaValidationHelper.validate(aSchema1, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());
    aErrors = XMLSchemaValidationHelper.validate(aSchema2, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());

    aDoc = DOMReader.readXMLDOM(XML2);
    aErrors = XMLSchemaValidationHelper.validate(aSchema1, XML1);
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());
    aErrors = XMLSchemaValidationHelper.validate(aSchema2, XML1);
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());

    try {
      // null XML not allowed
      XMLSchemaValidationHelper.validate(aSchema1, (IReadableResource) null);
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      // null XML not allowed
      XMLSchemaValidationHelper.validate(aSchema1, (Source) null);
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      // null XML not allowed
      XMLSchemaValidationHelper.validate((Schema) null, new DOMSource(aDoc));
      fail();
    } catch (final NullPointerException ex) {
    }
  }
  @Test
  public void testValidateDOMSource() throws SAXException {
    // Different source type
    Document aDoc = DOMReader.readXMLDOM(XML1);
    IResourceErrorGroup aErrors = XMLSchemaValidationHelper.validate(XSD1, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());
    aErrors =
        XMLSchemaValidationHelper.validate(
            new IReadableResource[] {XSD2, XSD1}, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(0, aErrors.getSize());

    aDoc = DOMReader.readXMLDOM(XML2);
    aErrors = XMLSchemaValidationHelper.validate(XSD1, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(1, aErrors.getSize());
    aErrors =
        XMLSchemaValidationHelper.validate(
            new IReadableResource[] {XSD2, XSD1}, new DOMSource(aDoc));
    assertNotNull(aErrors);
    assertEquals(1, aErrors.getSize());

    try {
      // null XML not allowed
      XMLSchemaValidationHelper.validate(XSD1, (Source) null);
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      // null XML not allowed
      XMLSchemaValidationHelper.validate(new IReadableResource[] {XSD2, XSD1}, (Source) null);
      fail();
    } catch (final NullPointerException ex) {
    }
  }
  /**
   * This is the main sending routine. It performs the following steps:
   *
   * <ol>
   *   <li>Verify that all required parameters are present and valid - {@link #verifyContent()}
   *   <li>The business document is read as XML. In case of an error, an exception is thrown.
   *   <li>The Standard Business Document (SBD) is created, all PEPPOL required fields are set and
   *       the business document is embedded.
   *   <li>The SBD is serialized and send via AS2
   *   <li>The AS2 response incl. the MDN is returned for further evaluation.
   * </ol>
   *
   * @return The AS2 response returned by the AS2 sender. This is never <code>null</code>.
   * @throws AS2ClientBuilderException In case the the business document is invalid XML or in case
   *     {@link #verifyContent()} throws an exception because of invalid or incomplete settings.
   */
  @Nonnull
  public AS2ClientResponse sendSynchronous() throws AS2ClientBuilderException {
    // Perform SMP client lookup
    performSMPClientLookup();

    // Set derivable values
    setDefaultDerivedValues();

    // Verify the whole data set
    verifyContent();

    // Build message

    // 1. read business document
    Element aXML = null;
    if (m_aBusinessDocumentRes != null) {
      try {
        final Document aXMLDocument = DOMReader.readXMLDOM(m_aBusinessDocumentRes);
        if (aXMLDocument == null)
          throw new AS2ClientBuilderException(
              "Failed to read business document '" + m_aBusinessDocumentRes.getPath() + "' as XML");
        aXML = aXMLDocument.getDocumentElement();
      } catch (final SAXException ex) {
        throw new AS2ClientBuilderException(
            "Failed to read business document '" + m_aBusinessDocumentRes.getPath() + "' as XML",
            ex);
      }
    } else {
      aXML = m_aBusinessDocumentElement;
    }
    if (aXML == null) throw new AS2ClientBuilderException("No XML business content present!");

    // 2. validate the business document
    if (m_aValidationKey != null) validateOutgoingBusinessDocument(aXML);

    // 3. build SBD data
    final PeppolSBDHDocument aDD = PeppolSBDHDocument.create(aXML);
    aDD.setSenderWithDefaultScheme(m_aPeppolSenderID.getValue());
    aDD.setReceiver(m_aPeppolReceiverID.getScheme(), m_aPeppolReceiverID.getValue());
    aDD.setDocumentType(m_aPeppolDocumentTypeID.getScheme(), m_aPeppolDocumentTypeID.getValue());
    aDD.setProcess(m_aPeppolProcessID.getScheme(), m_aPeppolProcessID.getValue());

    // 4. build SBD
    final StandardBusinessDocument aSBD =
        new PeppolSBDHDocumentWriter().createStandardBusinessDocument(aDD);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    if (new SBDMarshaller().write(aSBD, new StreamResult(aBAOS)).isFailure())
      throw new AS2ClientBuilderException("Failed to serialize SBD!");
    aBAOS.close();

    // 5. send message
    // Start building the AS2 client settings
    final AS2ClientSettings aAS2ClientSettings = new AS2ClientSettings();
    // Key store
    aAS2ClientSettings.setKeyStore(m_aKeyStoreFile, m_sKeyStorePassword);
    aAS2ClientSettings.setSaveKeyStoreChangesToFile(m_bSaveKeyStoreChangesToFile);

    // Fixed sender
    aAS2ClientSettings.setSenderData(m_sSenderAS2ID, m_sSenderAS2Email, m_sSenderAS2KeyAlias);

    // Dynamic receiver
    aAS2ClientSettings.setReceiverData(m_sReceiverAS2ID, m_sReceiverAS2KeyAlias, m_sReceiverAS2Url);
    aAS2ClientSettings.setReceiverCertificate(m_aReceiverCert);

    // AS2 stuff - no need to change anything in this block
    aAS2ClientSettings.setPartnershipName(
        aAS2ClientSettings.getSenderAS2ID() + "-" + aAS2ClientSettings.getReceiverAS2ID());
    aAS2ClientSettings.setMDNOptions(
        new DispositionOptions()
            .setMICAlg(m_eSigningAlgo)
            .setMICAlgImportance(DispositionOptions.IMPORTANCE_REQUIRED)
            .setProtocol(DispositionOptions.PROTOCOL_PKCS7_SIGNATURE)
            .setProtocolImportance(DispositionOptions.IMPORTANCE_REQUIRED));
    aAS2ClientSettings.setEncryptAndSign(null, m_eSigningAlgo);
    aAS2ClientSettings.setMessageIDFormat(m_sMessageIDFormat);

    final AS2ClientRequest aRequest = new AS2ClientRequest(m_sAS2Subject);
    // Using a String is better when having a
    // com.sun.xml.ws.encoding.XmlDataContentHandler installed!
    aRequest.setData(aBAOS.getAsString(CCharset.CHARSET_UTF_8_OBJ), CCharset.CHARSET_UTF_8_OBJ);

    final AS2Client aAS2Client = m_aAS2ClientFactory.create();
    final AS2ClientResponse aResponse = aAS2Client.sendSynchronous(aAS2ClientSettings, aRequest);
    return aResponse;
  }
예제 #10
0
  @Test
  public void testReadWithSchema() throws SAXException {
    final Schema aSchema =
        XMLSchemaCache.getInstance().getSchema(new ClassPathResource("xml/schema1.xsd"));
    assertNotNull(aSchema);

    // read valid
    final String sValid =
        "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b></root>";
    Document doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            sValid, new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            sValid,
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);

    DOMReader.readXMLDOM(
        new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new NonBlockingStringReader(sValid),
            new DOMReaderSettings().setErrorHandler(new CollectingSAXErrorHandler()));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new NonBlockingStringReader(sValid),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);

    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(sValid, CCharset.CHARSET_ISO_8859_1_OBJ),
            new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(sValid, CCharset.CHARSET_ISO_8859_1_OBJ),
            new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(sValid, CCharset.CHARSET_ISO_8859_1_OBJ),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);

    doc =
        DOMReader.readXMLDOM(
            new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringSAXInputSource(sValid),
            new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringSAXInputSource(sValid),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));

    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new ClassPathResource("xml/schema1-valid.xml"),
            new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new ClassPathResource("xml/schema1-valid.xml"),
            new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new ClassPathResource("xml/schema1-valid.xml"),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);

    // Read invalid (<c> tag is unknown)
    final String sInvalid =
        "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b><c>3</c></root>";
    doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            sInvalid,
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new NonBlockingStringReader(sInvalid),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(sInvalid, CCharset.CHARSET_ISO_8859_1_OBJ),
            new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringInputStream(sInvalid, CCharset.CHARSET_ISO_8859_1_OBJ),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new StringSAXInputSource(sInvalid),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new ClassPathResource("xml/schema1-invalid.xml"),
            new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc =
        DOMReader.readXMLDOM(
            new ClassPathResource("xml/schema1-invalid.xml"),
            new DOMReaderSettings()
                .setSchema(aSchema)
                .setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);

    try {
      DOMReader.readXMLDOM((Reader) null, new DOMReaderSettings());
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      DOMReader.readXMLDOM((InputStream) null, new DOMReaderSettings());
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      DOMReader.readXMLDOM((InputSource) null, new DOMReaderSettings());
      fail();
    } catch (final NullPointerException ex) {
    }
    try {
      DOMReader.readXMLDOM((IReadableResource) null, new DOMReaderSettings());
      fail();
    } catch (final NullPointerException ex) {
    }
  }