Search in sources :

Example 21 with Ebms3UserMessage

use of com.helger.phase4.ebms3header.Ebms3UserMessage 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 22 with Ebms3UserMessage

use of com.helger.phase4.ebms3header.Ebms3UserMessage in project phase4 by phax.

the class ENTSOGCompatibilityValidatorTest method testValidateUserMessageNoMessageID.

@Test
public void testValidateUserMessageNoMessageID() {
    final Ebms3UserMessage aUserMessage = new Ebms3UserMessage();
    aUserMessage.setMessageInfo(new Ebms3MessageInfo());
    VALIDATOR.validateUserMessage(aUserMessage, m_aErrorList);
    assertTrue(m_aErrorList.containsAny(x -> x.getErrorText(LOCALE).contains("MessageInfo/MessageId is missing")));
}
Also used : 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) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Test(org.junit.Test)

Example 23 with Ebms3UserMessage

use of com.helger.phase4.ebms3header.Ebms3UserMessage in project phase4 by phax.

the class PeppolCompatibilityValidator method validateUserMessage.

@Override
public void validateUserMessage(@Nonnull final Ebms3UserMessage aUserMsg, @Nonnull final ErrorList aErrorList) {
    ValueEnforcer.notNull(aUserMsg, "UserMsg");
    if (aUserMsg.getMessageInfo() == null) {
        aErrorList.add(_createError("MessageInfo is missing"));
    } else {
        if (StringHelper.hasNoText(aUserMsg.getMessageInfo().getMessageId()))
            aErrorList.add(_createError("MessageInfo/MessageId is missing"));
        {
            // Check if originalSender and finalRecipient are present
            // Since these two properties are mandatory
            final Ebms3MessageProperties aMessageProperties = aUserMsg.getMessageProperties();
            if (aMessageProperties == null)
                aErrorList.add(_createError("MessageProperties is missing but 'originalSender' and 'finalRecipient' properties are required"));
            else {
                final List<Ebms3Property> aProps = aMessageProperties.getProperty();
                if (aProps.isEmpty())
                    aErrorList.add(_createError("MessageProperties/Property must not be empty"));
                else {
                    String sOriginalSenderC1 = null;
                    String sFinalRecipientC4 = null;
                    for (final Ebms3Property sProperty : aProps) {
                        if (sProperty.getName().equals(CAS4.ORIGINAL_SENDER))
                            sOriginalSenderC1 = sProperty.getValue();
                        else if (sProperty.getName().equals(CAS4.FINAL_RECIPIENT))
                            sFinalRecipientC4 = sProperty.getValue();
                    }
                    if (StringHelper.hasNoText(sOriginalSenderC1))
                        aErrorList.add(_createError("MessageProperties/Property '" + CAS4.ORIGINAL_SENDER + "' property is empty or not existant but mandatory"));
                    if (StringHelper.hasNoText(sFinalRecipientC4))
                        aErrorList.add(_createError("MessageProperties/Property '" + CAS4.FINAL_RECIPIENT + "' property is empty or not existant but mandatory"));
                }
            }
        }
    }
    if (aUserMsg.getPartyInfo() == null) {
        aErrorList.add(_createError("PartyInfo is missing"));
    } else {
        final Ebms3From aFrom = aUserMsg.getPartyInfo().getFrom();
        if (aFrom != null) {
            if (aFrom.getPartyIdCount() > 1)
                aErrorList.add(_createError("PartyInfo/From must contain no more than one PartyID"));
            else if (aFrom.getPartyIdCount() == 1) {
                if (!PeppolPMode.DEFAULT_PARTY_TYPE_ID.equals(aFrom.getPartyIdAtIndex(0).getType())) {
                    aErrorList.add(_createError("PartyInfo/From[0]/@type must be '" + PeppolPMode.DEFAULT_PARTY_TYPE_ID + "' instead of '" + aFrom.getPartyIdAtIndex(0).getType() + "'"));
                }
            }
        }
        final Ebms3To aTo = aUserMsg.getPartyInfo().getTo();
        if (aTo != null) {
            if (aTo.getPartyIdCount() > 1)
                aErrorList.add(_createError("PartyInfo/To must contain no more than one PartyID"));
            else if (aTo.getPartyIdCount() == 1) {
                if (!PeppolPMode.DEFAULT_PARTY_TYPE_ID.equals(aTo.getPartyIdAtIndex(0).getType())) {
                    aErrorList.add(_createError("PartyInfo/To[0]/@type must be '" + PeppolPMode.DEFAULT_PARTY_TYPE_ID + "' instead of '" + aTo.getPartyIdAtIndex(0).getType() + "'"));
                }
            }
        }
    }
    if (aUserMsg.getCollaborationInfo() == null) {
        aErrorList.add(_createError("CollaborationInfo is missing"));
    } else {
        final Ebms3AgreementRef aAgreementRef = aUserMsg.getCollaborationInfo().getAgreementRef();
        if (aAgreementRef == null) {
            aErrorList.add(_createError("CollaborationInfo/AgreementRef is missing"));
        } else {
            if (!PeppolPMode.DEFAULT_AGREEMENT_ID.equals(aAgreementRef.getValue()))
                aErrorList.add(_createError("CollaborationInfo/AgreementRef must be '" + PeppolPMode.DEFAULT_AGREEMENT_ID + "' instead of '" + aAgreementRef.getValue() + "'"));
            if (StringHelper.hasText(aAgreementRef.getType()))
                aErrorList.add(_createError("CollaborationInfo/AgreementRef/@type must not be set"));
        }
    }
}
Also used : Ebms3MessageProperties(com.helger.phase4.ebms3header.Ebms3MessageProperties) Ebms3From(com.helger.phase4.ebms3header.Ebms3From) Ebms3To(com.helger.phase4.ebms3header.Ebms3To) ErrorList(com.helger.commons.error.list.ErrorList) List(java.util.List) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) Ebms3AgreementRef(com.helger.phase4.ebms3header.Ebms3AgreementRef)

Example 24 with Ebms3UserMessage

use of com.helger.phase4.ebms3header.Ebms3UserMessage in project phase4 by phax.

the class PeppolCompatibilityValidatorTest method testValidateUserMessageNoMessageID.

@Test
public void testValidateUserMessageNoMessageID() {
    final Ebms3UserMessage aUserMessage = new Ebms3UserMessage();
    aUserMessage.setMessageInfo(new Ebms3MessageInfo());
    VALIDATOR.validateUserMessage(aUserMessage, m_aErrorList);
    assertTrue(m_aErrorList.containsAny(x -> x.getErrorText(LOCALE).contains("MessageInfo/MessageId is missing")));
}
Also used : 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) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Test(org.junit.Test)

Example 25 with Ebms3UserMessage

use of com.helger.phase4.ebms3header.Ebms3UserMessage in project phase4 by phax.

the class PeppolCompatibilityValidatorTest method testValidateUserMessageMoreThanOnePartyID.

@Test
public void testValidateUserMessageMoreThanOnePartyID() {
    final Ebms3PartyId aFirstId = MessageHelperMethods.createEbms3PartyId("type", "value");
    final Ebms3PartyId aSecondId = MessageHelperMethods.createEbms3PartyId("type2", "value2");
    final Ebms3From aFromPart = new Ebms3From();
    aFromPart.addPartyId(aFirstId);
    aFromPart.addPartyId(aSecondId);
    final Ebms3To aToPart = new Ebms3To();
    aToPart.addPartyId(aFirstId);
    aToPart.addPartyId(aSecondId);
    final Ebms3PartyInfo aPartyInfo = new Ebms3PartyInfo();
    aPartyInfo.setFrom(aFromPart);
    aPartyInfo.setTo(aToPart);
    final Ebms3UserMessage aUserMessage = new Ebms3UserMessage();
    aUserMessage.setPartyInfo(aPartyInfo);
    VALIDATOR.validateUserMessage(aUserMessage, m_aErrorList);
    assertTrue(m_aErrorList.containsAny(x -> x.getErrorText(LOCALE).contains("must contain no more than one PartyID")));
}
Also used : 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) Ebms3PartyId(com.helger.phase4.ebms3header.Ebms3PartyId) Ebms3From(com.helger.phase4.ebms3header.Ebms3From) Ebms3To(com.helger.phase4.ebms3header.Ebms3To) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) Test(org.junit.Test)

Aggregations

Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)26 Ebms3PartyInfo (com.helger.phase4.ebms3header.Ebms3PartyInfo)15 Ebms3MessageInfo (com.helger.phase4.ebms3header.Ebms3MessageInfo)14 Ebms3From (com.helger.phase4.ebms3header.Ebms3From)13 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)13 Ebms3To (com.helger.phase4.ebms3header.Ebms3To)13 Nonnull (javax.annotation.Nonnull)13 Test (org.junit.Test)13 ErrorList (com.helger.commons.error.list.ErrorList)12 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)12 PModeLeg (com.helger.phase4.model.pmode.leg.PModeLeg)12 Locale (java.util.Locale)12 Before (org.junit.Before)10 ECryptoAlgorithmSign (com.helger.phase4.crypto.ECryptoAlgorithmSign)9 ECryptoAlgorithmSignDigest (com.helger.phase4.crypto.ECryptoAlgorithmSignDigest)9 Ebms3PartyId (com.helger.phase4.ebms3header.Ebms3PartyId)9 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)8 ETriState (com.helger.commons.state.ETriState)8 ECryptoAlgorithmCrypt (com.helger.phase4.crypto.ECryptoAlgorithmCrypt)8 Ebms3CollaborationInfo (com.helger.phase4.ebms3header.Ebms3CollaborationInfo)8