use of com.helger.phase4.ebms3header.Ebms3SignalMessage in project phase4 by phax.
the class MainPhase4PeppolSenderHelgerXRechnung12CII method send.
@SuppressWarnings("deprecation")
public static void send() {
try {
final Element aPayloadElement = DOMReader.readXMLDOM(new File("src/test/resources/examples/xrechnung-1.2-cii-example1.xml")).getDocumentElement();
if (aPayloadElement == null)
throw new IllegalStateException("Failed to read XML file to be send");
// Start configuring here
final IParticipantIdentifier aReceiverID = Phase4PeppolSender.IF.createParticipantIdentifierWithDefaultScheme("9915:helger");
final IAS4ClientBuildMessageCallback aBuildMessageCallback = new IAS4ClientBuildMessageCallback() {
public void onAS4Message(final AbstractAS4Message<?> aMsg) {
final AS4UserMessage aUserMsg = (AS4UserMessage) aMsg;
LOGGER.info("Sending out AS4 message with message ID '" + aUserMsg.getEbms3UserMessage().getMessageInfo().getMessageId() + "'");
LOGGER.info("Sending out AS4 message with conversation ID '" + aUserMsg.getEbms3UserMessage().getCollaborationInfo().getConversationId() + "'");
}
};
final Wrapper<Ebms3SignalMessage> aSignalMsgWrapper = new Wrapper<>();
final IAS4RawResponseConsumer aRRC = new AS4RawResponseConsumerWriteToFile().setHandleStatusLine(true).setHandleHttpHeaders(true);
// Add XRechnung rulesets
final IValidationExecutorSetRegistry<IValidationSourceXML> aVESRegistry = Phase4PeppolValidation.createDefaultRegistry();
EN16931Validation.initEN16931(aVESRegistry);
XRechnungValidation.initXRechnung(aVESRegistry);
final ESimpleUserMessageSendResult eResult;
eResult = Phase4PeppolSender.builder().documentTypeID(Phase4PeppolSender.IF.createDocumentTypeIdentifierWithDefaultScheme("urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100::CrossIndustryInvoice##urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_1.2::D16B")).processID(Phase4PeppolSender.IF.createProcessIdentifierWithDefaultScheme("urn:fdc:peppol.eu:2017:poacc:billing:01:1.0")).senderParticipantID(Phase4PeppolSender.IF.createParticipantIdentifierWithDefaultScheme("9915:phase4-test-sender")).receiverParticipantID(aReceiverID).senderPartyID("POP000306").payload(aPayloadElement).smpClient(new SMPClientReadOnly(Phase4PeppolSender.URL_PROVIDER, aReceiverID, ESML.DIGIT_TEST)).validationRegistry(aVESRegistry).validationConfiguration(XRechnungValidation.VID_XRECHNUNG_CII_122, new Phase4PeppolValidatonResultHandler()).buildMessageCallback(aBuildMessageCallback).rawResponseConsumer(aRRC).signalMsgConsumer(aSignalMsgWrapper::set).sendMessageAndCheckForReceipt();
LOGGER.info("Peppol send result: " + eResult);
} catch (final Exception ex) {
LOGGER.error("Error sending Peppol message via AS4", ex);
}
}
use of com.helger.phase4.ebms3header.Ebms3SignalMessage in project phase4 by phax.
the class AS4ReceiptMessage method create.
/**
* This method creates a receipt message.
*
* @param eSoapVersion
* SOAP Version which should be used
* @param sMessageID
* Message ID to use. May neither be <code>null</code> nor empty.
* @param aEbms3UserMessage
* The received usermessage which should be responded too
* @param aSoapDocument
* If the SOAPDocument has WSS4j elements and the following parameter
* is true NonRepudiation will be used if the message is signed
* @param bShouldUseNonRepudiation
* If NonRepudiation should be used or not
* @return AS4ReceiptMessage
*/
@Nonnull
public static AS4ReceiptMessage create(@Nonnull final ESoapVersion eSoapVersion, @Nonnull @Nonempty final String sMessageID, @Nullable final Ebms3UserMessage aEbms3UserMessage, @Nullable final Node aSoapDocument, @Nonnull final boolean bShouldUseNonRepudiation) {
// Only for signed messages
final ICommonsList<Node> aDSRefs = _getAllReferences(aSoapDocument);
final Ebms3SignalMessage aSignalMessage = new Ebms3SignalMessage();
// Message Info
{
// Always use "now" as date time
final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo(sMessageID, aEbms3UserMessage != null ? aEbms3UserMessage.getMessageInfo().getMessageId() : null);
aSignalMessage.setMessageInfo(aEbms3MessageInfo);
}
final Ebms3Receipt aEbms3Receipt = new Ebms3Receipt();
if (aDSRefs.isNotEmpty() && bShouldUseNonRepudiation) {
final NonRepudiationInformation aNonRepudiationInformation = new NonRepudiationInformation();
for (final Node aRef : aDSRefs) {
// Read XMLDsig Reference
final ReferenceType aRefObj = XMLDSigReaderBuilder.dsigReference().read(aRef);
// Add to NR response
final MessagePartNRInformation aMessagePartNRInformation = new MessagePartNRInformation();
aMessagePartNRInformation.setReference(aRefObj);
aNonRepudiationInformation.addMessagePartNRInformation(aMessagePartNRInformation);
}
aEbms3Receipt.addAny(Ebms3WriterBuilder.nonRepudiationInformation().getAsDocument(aNonRepudiationInformation).getDocumentElement());
} else {
if (aDSRefs.isEmpty())
LOGGER.info("Found no ds:Reference elements in the source message, hence returning the source UserMessage in the Receipt");
else
LOGGER.info("Non-repudiation is disabled, hence returning the source UserMessage in the Receipt");
// If the original usermessage is not signed, the receipt will contain the
// original message part without wss4j security
aEbms3Receipt.addAny(AS4UserMessage.create(eSoapVersion, aEbms3UserMessage).getAsSoapDocument().getDocumentElement());
}
aSignalMessage.setReceipt(aEbms3Receipt);
return new AS4ReceiptMessage(eSoapVersion, aSignalMessage);
}
use of com.helger.phase4.ebms3header.Ebms3SignalMessage in project phase4 by phax.
the class AS4DumpReader method decryptAS4In.
/**
* Utility method to decrypt dumped .as4in message late.<br>
* Note: this method was mainly created for internal use and does not win the
* prize for the most sexy piece of software in the world ;-)
*
* @param aAS4InData
* The byte array with the dumped data.
* @param aCF
* The Crypto factory to be used. This crypto factory must use use the
* private key that can be used to decrypt this particular message. May
* not be <code>null</code>.
* @param aHttpHeaderConsumer
* An optional HTTP Header map consumer. May be <code>null</code>.
* @param aDecryptedConsumer
* The consumer for the decrypted payload - whatever that is :). May
* not be <code>null</code>.
* @throws WSSecurityException
* In case of error
* @throws Phase4Exception
* In case of error
* @throws IOException
* In case of error
* @throws MessagingException
* In case of error
*/
public static void decryptAS4In(@Nonnull final byte[] aAS4InData, final IAS4CryptoFactory aCF, @Nullable final Consumer<HttpHeaderMap> aHttpHeaderConsumer, @Nonnull final Consumer<byte[]> aDecryptedConsumer) throws WSSecurityException, Phase4Exception, IOException, MessagingException {
final HttpHeaderMap hm = new HttpHeaderMap();
int nHttpStart = 0;
int nHttpEnd = -1;
boolean bLastWasCR = false;
for (int i = 0; i < aAS4InData.length; ++i) {
final byte b = aAS4InData[i];
if (b == '\n') {
if (bLastWasCR) {
nHttpEnd = i;
break;
}
bLastWasCR = true;
final String sLine = new String(aAS4InData, nHttpStart, i - nHttpStart, StandardCharsets.ISO_8859_1);
final String[] aParts = StringHelper.getExplodedArray(':', sLine, 2);
hm.addHeader(aParts[0].trim(), aParts[1].trim());
nHttpStart = i + 1;
} else {
if (b != '\r')
bLastWasCR = false;
}
}
if (aHttpHeaderConsumer != null)
aHttpHeaderConsumer.accept(hm);
LOGGER.info("Now at byte " + nHttpEnd + " having " + hm.getCount() + " HTTP headers");
WebScopeManager.onGlobalBegin(MockServletContext.create());
try (final WebScoped w = new WebScoped();
final AS4RequestHandler rh = new AS4RequestHandler(aCF, DefaultPModeResolver.DEFAULT_PMODE_RESOLVER, IAS4IncomingAttachmentFactory.DEFAULT_INSTANCE, new AS4IncomingMessageMetadata(EAS4MessageMode.REQUEST))) {
final IAS4ServletMessageProcessorSPI aSPI = new IAS4ServletMessageProcessorSPI() {
public AS4MessageProcessorResult processAS4UserMessage(final IAS4IncomingMessageMetadata aMessageMetadata, final HttpHeaderMap aHttpHeaders, final Ebms3UserMessage aUserMessage, final IPMode aPMode, final Node aPayload, final ICommonsList<WSS4JAttachment> aIncomingAttachments, final IAS4MessageState aState, final ICommonsList<Ebms3Error> aProcessingErrorMessages) {
try {
final byte[] aDecryptedBytes = StreamHelper.getAllBytes(aIncomingAttachments.getFirst().getInputStreamProvider());
aDecryptedConsumer.accept(aDecryptedBytes);
LOGGER.info("Handled decrypted payload with " + aDecryptedBytes.length + " bytes");
return AS4MessageProcessorResult.createSuccess();
} catch (final Exception ex) {
throw new IllegalStateException(ex);
}
}
public AS4SignalMessageProcessorResult processAS4SignalMessage(final IAS4IncomingMessageMetadata aMessageMetadata, final HttpHeaderMap aHttpHeaders, final Ebms3SignalMessage aSignalMessage, final IPMode aPMode, final IAS4MessageState aState, final ICommonsList<Ebms3Error> aProcessingErrorMessages) {
LOGGER.error("Unexpected signal msg");
return AS4SignalMessageProcessorResult.createSuccess();
}
};
rh.setProcessorSupplier(() -> new CommonsArrayList<>(aSPI));
rh.handleRequest(new NonBlockingByteArrayInputStream(aAS4InData, nHttpEnd, aAS4InData.length - nHttpEnd), hm, new IAS4ResponseAbstraction() {
public void setStatus(final int nStatusCode) {
}
public void setMimeType(final IMimeType aMimeType) {
}
public void setContent(final HttpHeaderMap aHeaderMap, final IHasInputStream aHasIS) {
}
public void setContent(final byte[] aResultBytes, final Charset aCharset) {
}
});
} finally {
WebScopeManager.onGlobalEnd();
}
}
use of com.helger.phase4.ebms3header.Ebms3SignalMessage in project phase4 by phax.
the class PeppolCompatibilityValidatorTest method testValidateSignalMessageNoMessageID.
@Test
public void testValidateSignalMessageNoMessageID() {
final Ebms3SignalMessage aSignalMessage = new Ebms3SignalMessage();
aSignalMessage.setMessageInfo(new Ebms3MessageInfo());
VALIDATOR.validateSignalMessage(aSignalMessage, m_aErrorList);
assertTrue(m_aErrorList.containsAny(x -> x.getErrorText(LOCALE).contains("MessageID is missing")));
}
use of com.helger.phase4.ebms3header.Ebms3SignalMessage in project phase4 by phax.
the class CEFCompatibilityValidatorTest method testValidateSignalMessageNoMessageID.
@Test
public void testValidateSignalMessageNoMessageID() {
final Ebms3SignalMessage aSignalMessage = new Ebms3SignalMessage();
aSignalMessage.setMessageInfo(new Ebms3MessageInfo());
VALIDATOR.validateSignalMessage(aSignalMessage, m_aErrorList);
assertTrue(m_aErrorList.containsAny(x -> x.getErrorText(LOCALE).contains("MessageID is missing")));
}
Aggregations