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);
}
}
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());
}
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);
}
}
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);
}
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();
}
Aggregations