Search in sources :

Example 11 with Ebms3SignalMessage

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);
    }
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) IAS4ClientBuildMessageCallback(com.helger.phase4.client.IAS4ClientBuildMessageCallback) Wrapper(com.helger.commons.wrapper.Wrapper) SMPClientReadOnly(com.helger.smpclient.peppol.SMPClientReadOnly) Element(org.w3c.dom.Element) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) IAS4RawResponseConsumer(com.helger.phase4.client.IAS4RawResponseConsumer) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) AbstractAS4Message(com.helger.phase4.messaging.domain.AbstractAS4Message) ESimpleUserMessageSendResult(com.helger.phase4.sender.AbstractAS4UserMessageBuilder.ESimpleUserMessageSendResult) AS4RawResponseConsumerWriteToFile(com.helger.phase4.dump.AS4RawResponseConsumerWriteToFile) AS4RawResponseConsumerWriteToFile(com.helger.phase4.dump.AS4RawResponseConsumerWriteToFile) File(java.io.File) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 12 with Ebms3SignalMessage

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);
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Ebms3Receipt(com.helger.phase4.ebms3header.Ebms3Receipt) NonRepudiationInformation(com.helger.phase4.ebms3header.NonRepudiationInformation) Node(org.w3c.dom.Node) MessagePartNRInformation(com.helger.phase4.ebms3header.MessagePartNRInformation) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) ReferenceType(com.helger.xsds.xmldsig.ReferenceType) Nonnull(javax.annotation.Nonnull)

Example 13 with Ebms3SignalMessage

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();
    }
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Node(org.w3c.dom.Node) IHasInputStream(com.helger.commons.io.IHasInputStream) IAS4ServletMessageProcessorSPI(com.helger.phase4.servlet.spi.IAS4ServletMessageProcessorSPI) IAS4MessageState(com.helger.phase4.servlet.IAS4MessageState) IAS4IncomingMessageMetadata(com.helger.phase4.messaging.IAS4IncomingMessageMetadata) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) IAS4ResponseAbstraction(com.helger.phase4.servlet.IAS4ResponseAbstraction) IMimeType(com.helger.commons.mime.IMimeType) WebScoped(com.helger.web.scope.mgr.WebScoped) IAS4IncomingMessageMetadata(com.helger.phase4.messaging.IAS4IncomingMessageMetadata) AS4IncomingMessageMetadata(com.helger.phase4.servlet.AS4IncomingMessageMetadata) Charset(java.nio.charset.Charset) MessagingException(javax.mail.MessagingException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Phase4Exception(com.helger.phase4.util.Phase4Exception) IOException(java.io.IOException) AS4RequestHandler(com.helger.phase4.servlet.AS4RequestHandler) IPMode(com.helger.phase4.model.pmode.IPMode) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage)

Example 14 with Ebms3SignalMessage

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")));
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) PModeLegSecurity(com.helger.phase4.model.pmode.leg.PModeLegSecurity) MessageHelperMethods(com.helger.phase4.messaging.domain.MessageHelperMethods) EMEP(com.helger.phase4.model.EMEP) ErrorList(com.helger.commons.error.list.ErrorList) ETriState(com.helger.commons.state.ETriState) ESoapVersion(com.helger.phase4.soap.ESoapVersion) Assert.assertNotSame(org.junit.Assert.assertNotSame) EPModeSendReceiptReplyPattern(com.helger.phase4.model.pmode.leg.EPModeSendReceiptReplyPattern) ECryptoAlgorithmSign(com.helger.phase4.crypto.ECryptoAlgorithmSign) Locale(java.util.Locale) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) PMode(com.helger.phase4.model.pmode.PMode) PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) ClassRule(org.junit.ClassRule) PModeLegErrorHandling(com.helger.phase4.model.pmode.leg.PModeLegErrorHandling) PhotonAppWebTestRule(com.helger.photon.app.mock.PhotonAppWebTestRule) Before(org.junit.Before) Ebms3PartyId(com.helger.phase4.ebms3header.Ebms3PartyId) PModeLegProtocol(com.helger.phase4.model.pmode.leg.PModeLegProtocol) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) ECryptoAlgorithmSignDigest(com.helger.phase4.crypto.ECryptoAlgorithmSignDigest) EWSSVersion(com.helger.phase4.wss.EWSSVersion) Assert.assertTrue(org.junit.Assert.assertTrue) EMEPBinding(com.helger.phase4.model.EMEPBinding) Test(org.junit.Test) Ebms3To(com.helger.phase4.ebms3header.Ebms3To) ECryptoAlgorithmCrypt(com.helger.phase4.crypto.ECryptoAlgorithmCrypt) Ebms3From(com.helger.phase4.ebms3header.Ebms3From) Ignore(org.junit.Ignore) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) IPModeIDProvider(com.helger.phase4.model.pmode.IPModeIDProvider) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Test(org.junit.Test)

Example 15 with Ebms3SignalMessage

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")));
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) PModeLegSecurity(com.helger.phase4.model.pmode.leg.PModeLegSecurity) MessageHelperMethods(com.helger.phase4.messaging.domain.MessageHelperMethods) EMEP(com.helger.phase4.model.EMEP) ErrorList(com.helger.commons.error.list.ErrorList) ETriState(com.helger.commons.state.ETriState) ESoapVersion(com.helger.phase4.soap.ESoapVersion) Assert.assertNotSame(org.junit.Assert.assertNotSame) EPModeSendReceiptReplyPattern(com.helger.phase4.model.pmode.leg.EPModeSendReceiptReplyPattern) ECryptoAlgorithmSign(com.helger.phase4.crypto.ECryptoAlgorithmSign) Locale(java.util.Locale) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) PMode(com.helger.phase4.model.pmode.PMode) PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) ClassRule(org.junit.ClassRule) PModeLegErrorHandling(com.helger.phase4.model.pmode.leg.PModeLegErrorHandling) PhotonAppWebTestRule(com.helger.photon.app.mock.PhotonAppWebTestRule) Before(org.junit.Before) Ebms3PartyId(com.helger.phase4.ebms3header.Ebms3PartyId) PModeLegProtocol(com.helger.phase4.model.pmode.leg.PModeLegProtocol) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) ECryptoAlgorithmSignDigest(com.helger.phase4.crypto.ECryptoAlgorithmSignDigest) EWSSVersion(com.helger.phase4.wss.EWSSVersion) Assert.assertTrue(org.junit.Assert.assertTrue) EMEPBinding(com.helger.phase4.model.EMEPBinding) Test(org.junit.Test) Ebms3To(com.helger.phase4.ebms3header.Ebms3To) ECryptoAlgorithmCrypt(com.helger.phase4.crypto.ECryptoAlgorithmCrypt) Ebms3From(com.helger.phase4.ebms3header.Ebms3From) Ignore(org.junit.Ignore) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) IPModeIDProvider(com.helger.phase4.model.pmode.IPModeIDProvider) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Test(org.junit.Test)

Aggregations

Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)16 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)11 Nonnull (javax.annotation.Nonnull)9 Ebms3MessageInfo (com.helger.phase4.ebms3header.Ebms3MessageInfo)8 Ebms3PartyInfo (com.helger.phase4.ebms3header.Ebms3PartyInfo)6 PModeLeg (com.helger.phase4.model.pmode.leg.PModeLeg)6 Phase4Exception (com.helger.phase4.util.Phase4Exception)6 Locale (java.util.Locale)6 Ebms3PartyId (com.helger.phase4.ebms3header.Ebms3PartyId)5 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)5 Ebms3PullRequest (com.helger.phase4.ebms3header.Ebms3PullRequest)5 MessageHelperMethods (com.helger.phase4.messaging.domain.MessageHelperMethods)5 Node (org.w3c.dom.Node)5 ErrorList (com.helger.commons.error.list.ErrorList)4 ETriState (com.helger.commons.state.ETriState)4 Wrapper (com.helger.commons.wrapper.Wrapper)4 ECryptoAlgorithmCrypt (com.helger.phase4.crypto.ECryptoAlgorithmCrypt)4 ECryptoAlgorithmSign (com.helger.phase4.crypto.ECryptoAlgorithmSign)4 ECryptoAlgorithmSignDigest (com.helger.phase4.crypto.ECryptoAlgorithmSignDigest)4 Ebms3From (com.helger.phase4.ebms3header.Ebms3From)4