Search in sources :

Example 16 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project phoss-smp by phax.

the class APIExecutorServiceMetadataGet method invokeAPI.

public void invokeAPI(@Nonnull final IAPIDescriptor aAPIDescriptor, @Nonnull @Nonempty final String sPath, @Nonnull final Map<String, String> aPathVariables, @Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws Exception {
    final String sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
    final String sPathDocumentTypeID = aPathVariables.get(SMPRestFilter.PARAM_DOCUMENT_TYPE_ID);
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    // Create the unsigned response document
    final Document aDoc;
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            {
                final com.helger.xsds.peppol.smp1.SignedServiceMetadataType ret = new SMPServerAPI(aDataProvider).getServiceRegistration(sPathServiceGroupID, sPathDocumentTypeID);
                // Convert to DOM document
                // Disable XSD check, because Signature is added later
                final SMPMarshallerSignedServiceMetadataType aMarshaller = new SMPMarshallerSignedServiceMetadataType(false);
                aDoc = aMarshaller.getAsDocument(ret);
                break;
            }
        case OASIS_BDXR_V1:
            {
                final com.helger.xsds.bdxr.smp1.SignedServiceMetadataType ret = new BDXR1ServerAPI(aDataProvider).getServiceRegistration(sPathServiceGroupID, sPathDocumentTypeID);
                // Convert to DOM document
                // Disable XSD check, because Signature is added later
                final BDXR1MarshallerSignedServiceMetadataType aMarshaller = new BDXR1MarshallerSignedServiceMetadataType(false);
                aDoc = aMarshaller.getAsDocument(ret);
                break;
            }
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    if (aDoc == null)
        throw new IllegalStateException("Failed to serialize unsigned node!");
    // Sign the document
    try {
        SMPKeyManager.getInstance().signXML(aDoc.getDocumentElement(), SMPServerConfiguration.getRESTType().isBDXR());
        LOGGER.info("Successfully signed response XML");
    } catch (final Exception ex) {
        throw new SMPInternalErrorException("Error in signing the response XML", ex);
    }
    // Serialize the signed document
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream()) {
        if (false) {
            // IMPORTANT: no indent and no align!
            final IXMLWriterSettings aSettings = XMLWriterSettings.createForCanonicalization();
            // Write the result to a byte array
            if (XMLWriter.writeToStream(aDoc, aBAOS, aSettings).isFailure())
                throw new IllegalStateException("Failed to serialize signed node!");
        } else {
            // for validating the signature!
            try {
                final Transformer aTransformer = XMLTransformerFactory.newTransformer();
                aTransformer.transform(new DOMSource(aDoc), new StreamResult(aBAOS));
            } catch (final TransformerException ex) {
                throw new IllegalStateException("Failed to serialized signed node", ex);
            }
        }
        aUnifiedResponse.setContent(aBAOS.toByteArray()).setMimeType(CMimeType.TEXT_XML).setCharset(XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) SMPMarshallerSignedServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerSignedServiceMetadataType) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI) Document(org.w3c.dom.Document) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) TransformerException(javax.xml.transform.TransformerException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) TransformerException(javax.xml.transform.TransformerException) SMPMarshallerSignedServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerSignedServiceMetadataType)

Example 17 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project phase4 by phax.

the class AS4CEFOneWayFuncTest method testAS4_TA20.

/**
 * Prerequisite:<br>
 * eSENS_TA14.<br>
 * Simulated SMSH sends a signed AS4 User Message with an encrypted first,
 * then compressed payload to the RMSH.<br>
 * <br>
 * Predicate: <br>
 * The SMSH receives a WS-Security SOAP Fault.
 *
 * @throws Exception
 *         In case of error
 */
@Test
public void testAS4_TA20() throws Exception {
    final AS4UserMessage aMsg = MockMessages.createUserMessageNotSigned(m_eSoapVersion, m_aPayload, null);
    final Document aDoc = AS4Encryptor.encryptSoapBodyPayload(m_aCryptoFactory, m_eSoapVersion, aMsg.getAsSoapDocument(m_aPayload), true, m_aCryptParams);
    final NodeList aNL = aDoc.getElementsByTagName("S12:Body");
    final NonBlockingByteArrayOutputStream outputStream = new NonBlockingByteArrayOutputStream();
    final Source xmlSource = new DOMSource(aNL.item(0));
    final Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    final byte[] aSrc = outputStream.toByteArray();
    // Compression
    final NonBlockingByteArrayOutputStream aCompressedOS = new NonBlockingByteArrayOutputStream();
    try (final InputStream aIS = new NonBlockingByteArrayInputStream(aSrc);
        final OutputStream aOS = EAS4CompressionMode.GZIP.getCompressStream(aCompressedOS)) {
        StreamHelper.copyInputStreamToOutputStream(aIS, aOS);
    }
    aNL.item(0).setTextContent(AS4XMLHelper.serializeXML(aDoc));
    sendPlainMessage(new HttpXMLEntity(aDoc, m_eSoapVersion.getMimeType()), false, EEbmsError.EBMS_FAILED_DECRYPTION.getErrorCode());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) OutputStream(java.io.OutputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) Test(org.junit.Test)

Example 18 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project phase4 by phax.

the class EAS4CompressionModeTest method testCompressionModes.

@Test
public void testCompressionModes() throws IOException {
    final byte[] aSrc = StreamHelper.getAllBytes(ClassPathResource.getInputStream("SOAPBodyPayload.xml"));
    assertNotNull(aSrc);
    for (final EAS4CompressionMode eMode : EAS4CompressionMode.values()) {
        // Compression
        final NonBlockingByteArrayOutputStream aCompressedOS = new NonBlockingByteArrayOutputStream();
        try (final InputStream aIS = new NonBlockingByteArrayInputStream(aSrc);
            final OutputStream aOS = eMode.getCompressStream(aCompressedOS)) {
            StreamHelper.copyInputStreamToOutputStream(aIS, aOS);
        }
        final byte[] aCompressed = aCompressedOS.toByteArray();
        // Decompression
        final NonBlockingByteArrayOutputStream aDecompressedOS = new NonBlockingByteArrayOutputStream();
        try (final InputStream aIS = eMode.getDecompressStream(new NonBlockingByteArrayInputStream(aCompressed));
            final OutputStream aOS = aDecompressedOS) {
            StreamHelper.copyInputStreamToOutputStream(aIS, aOS);
        }
        final byte[] aDecompressed = aDecompressedOS.toByteArray();
        assertArrayEquals(aSrc, aDecompressed);
    }
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Test(org.junit.Test)

Example 19 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project phase4 by phax.

the class EAS4CompressionModeTest method testCompressionStandalone.

@Test
public void testCompressionStandalone() throws IOException {
    final byte[] aSrc = StreamHelper.getAllBytes(ClassPathResource.getInputStream("SOAPBodyPayload.xml"));
    assertNotNull(aSrc);
    // Compression
    final NonBlockingByteArrayOutputStream aCompressedOS = new NonBlockingByteArrayOutputStream();
    _compressPayload(new NonBlockingByteArrayInputStream(aSrc), aCompressedOS);
    final byte[] aCompressed = aCompressedOS.toByteArray();
    // DECOMPRESSION
    final NonBlockingByteArrayOutputStream aDecompressedOS = new NonBlockingByteArrayOutputStream();
    _decompressPayload(new NonBlockingByteArrayInputStream(aCompressed), aDecompressedOS);
    final byte[] aDecompressed = aDecompressedOS.toByteArray();
    assertArrayEquals(aSrc, aDecompressed);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Test(org.junit.Test)

Example 20 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project ph-commons by phax.

the class HelpFormatterTest method testAutomaticUsage.

@Test
public void testAutomaticUsage() {
    final HelpFormatter hf = new HelpFormatter();
    Options options = null;
    String expected = "usage: app [-a]";
    final NonBlockingByteArrayOutputStream out = new NonBlockingByteArrayOutputStream();
    final PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1));
    options = new Options().addOption(Option.builder("a").desc("aaaa aaaa aaaa aaaa aaaa"));
    hf.printUsage(pw, 60, "app", options);
    pw.flush();
    assertEquals("simple auto usage", expected, out.getAsString(StandardCharsets.ISO_8859_1).trim());
    out.reset();
    expected = "usage: app [-a] [-b]";
    options = new Options().addOption(Option.builder("a").desc("aaaa aaaa aaaa aaaa aaaa")).addOption(Option.builder("b").desc("bbb"));
    hf.printUsage(pw, 60, "app", options);
    pw.flush();
    assertEquals("simple auto usage", expected, out.getAsString(StandardCharsets.ISO_8859_1).trim());
    out.reset();
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)55 Test (org.junit.Test)30 IOException (java.io.IOException)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)12 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)8 InputStream (java.io.InputStream)7 Nonnull (javax.annotation.Nonnull)7 MessagingException (javax.mail.MessagingException)7 IMicroDocument (com.helger.xml.microdom.IMicroDocument)4 AS2Exception (com.helger.as2lib.exception.AS2Exception)3 IMessageMDN (com.helger.as2lib.message.IMessageMDN)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ByteArrayOutputStreamProvider (com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider)3 MockMarshallerExternal (com.helger.jaxb.mock.MockMarshallerExternal)3 MockJAXBArchive (com.helger.jaxb.mock.external.MockJAXBArchive)3 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)3 Nullable (javax.annotation.Nullable)3 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)3 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)2 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)2