Search in sources :

Example 6 with PMode

use of com.helger.phase4.model.pmode.PMode in project phase4 by phax.

the class CEFPMode method createCEFPModeTwoWay.

/**
 * Two-Way Version of the CEF pmode uses two-way push-push
 *
 * @param sInitiatorID
 *        Initiator ID
 * @param sResponderID
 *        Responder ID
 * @param sResponderAddress
 *        Responder URL
 * @param aPModeIDProvider
 *        PMode ID provider
 * @param bPersist
 *        <code>true</code> to persist the PMode <code>false</code> to have it
 *        only in memory.
 * @return New PMode
 */
@Nonnull
public static PMode createCEFPModeTwoWay(@Nonnull @Nonempty final String sInitiatorID, @Nonnull @Nonempty final String sResponderID, @Nullable final String sResponderAddress, @Nonnull final IPModeIDProvider aPModeIDProvider, final boolean bPersist) {
    final PModeParty aInitiator = PModeParty.createSimple(sInitiatorID, CAS4.DEFAULT_INITIATOR_URL);
    final PModeParty aResponder = PModeParty.createSimple(sResponderID, CAS4.DEFAULT_RESPONDER_URL);
    final PMode aPMode = new PMode(aPModeIDProvider.getPModeID(sInitiatorID, sResponderID), aInitiator, aResponder, DEFAULT_AGREEMENT_ID, EMEP.TWO_WAY, EMEPBinding.PUSH_PUSH, generatePModeLeg(sResponderAddress), generatePModeLeg(sResponderAddress), (PModePayloadService) null, PModeReceptionAwareness.createDefault());
    if (bPersist) {
        // Ensure it is stored
        MetaAS4Manager.getPModeMgr().createOrUpdatePMode(aPMode);
    }
    return aPMode;
}
Also used : PModeParty(com.helger.phase4.model.pmode.PModeParty) PMode(com.helger.phase4.model.pmode.PMode) Nonnull(javax.annotation.Nonnull)

Example 7 with PMode

use of com.helger.phase4.model.pmode.PMode in project phase4 by phax.

the class MockClientMessages method createUserMessageNotSigned.

@Nonnull
public static AS4UserMessage createUserMessageNotSigned(@Nonnull final ESoapVersion eSoapVersion, @Nullable final Node aPayload, @Nullable final ICommonsList<WSS4JAttachment> aAttachments) {
    // Add properties
    final ICommonsList<Ebms3Property> aEbms3Properties = new CommonsArrayList<>();
    aEbms3Properties.add(MessageHelperMethods.createEbms3Property("ProcessInst", "PurchaseOrder:123456"));
    aEbms3Properties.add(MessageHelperMethods.createEbms3Property("ContextID", "987654321"));
    aEbms3Properties.add(MessageHelperMethods.createEbms3Property(CAS4.ORIGINAL_SENDER, "C1 OS"));
    aEbms3Properties.add(MessageHelperMethods.createEbms3Property(CAS4.FINAL_RECIPIENT, "C4 FR"));
    final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo();
    final Ebms3PayloadInfo aEbms3PayloadInfo = MessageHelperMethods.createEbms3PayloadInfo(aPayload != null, aAttachments);
    final Ebms3CollaborationInfo aEbms3CollaborationInfo = MessageHelperMethods.createEbms3CollaborationInfo("pmode-twoway", DEFAULT_AGREEMENT, "MyServiceTypes", "QuoteToCollect", "NewPurchaseOrder", "4321");
    final Ebms3PartyInfo aEbms3PartyInfo = MessageHelperMethods.createEbms3PartyInfo(CAS4.DEFAULT_INITIATOR_URL, "1234", CAS4.DEFAULT_RESPONDER_URL, "5678");
    final Ebms3MessageProperties aEbms3MessageProperties = MessageHelperMethods.createEbms3MessageProperties(aEbms3Properties);
    final AS4UserMessage aDoc = AS4UserMessage.create(aEbms3MessageInfo, aEbms3PayloadInfo, aEbms3CollaborationInfo, aEbms3PartyInfo, aEbms3MessageProperties, eSoapVersion).setMustUnderstand(true);
    return aDoc;
}
Also used : Ebms3MessageProperties(com.helger.phase4.ebms3header.Ebms3MessageProperties) Ebms3PayloadInfo(com.helger.phase4.ebms3header.Ebms3PayloadInfo) Ebms3CollaborationInfo(com.helger.phase4.ebms3header.Ebms3CollaborationInfo) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) Nonnull(javax.annotation.Nonnull)

Example 8 with PMode

use of com.helger.phase4.model.pmode.PMode in project phase4 by phax.

the class SOAPHeaderElementProcessorWSS4J method processHeaderElement.

@Nonnull
public ESuccess processHeaderElement(@Nonnull final Document aSOAPDoc, @Nonnull final Element aSecurityNode, @Nonnull final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull final AS4MessageState aState, @Nonnull final ErrorList aErrorList) {
    IPMode aPMode = aState.getPMode();
    if (aPMode == null)
        aPMode = m_aFallbackPMode;
    // Safety Check
    if (aPMode == null)
        throw new IllegalStateException("No PMode contained in AS4 state - seems like Ebms3 Messaging header is missing!");
    // Default is Leg 1, gets overwritten when a reference to a message id
    // exists and then uses leg2
    final Locale aLocale = aState.getLocale();
    PModeLeg aPModeLeg = aPMode.getLeg1();
    final Ebms3UserMessage aUserMessage = aState.getEbmsUserMessage();
    if (aUserMessage != null && StringHelper.hasText(aUserMessage.getMessageInfo().getRefToMessageId()))
        aPModeLeg = aPMode.getLeg2();
    // Does security - leg part checks if not <code>null</code>
    if (aPModeLeg.getSecurity() != null) {
        // Get Signature Algorithm
        Element aSignedNode = XMLHelper.getFirstChildElementOfName(aSecurityNode, CAS4.DS_NS, "Signature");
        if (aSignedNode != null) {
            // Go through the security nodes to find the algorithm attribute
            aSignedNode = XMLHelper.getFirstChildElementOfName(aSignedNode, CAS4.DS_NS, "SignedInfo");
            final Element aSignatureAlgorithm = XMLHelper.getFirstChildElementOfName(aSignedNode, CAS4.DS_NS, "SignatureMethod");
            String sAlgorithm = aSignatureAlgorithm == null ? null : aSignatureAlgorithm.getAttribute("Algorithm");
            final ECryptoAlgorithmSign eSignAlgo = ECryptoAlgorithmSign.getFromURIOrNull(sAlgorithm);
            if (eSignAlgo == null) {
                LOGGER.error("Error processing the Security Header, your signing algorithm '" + sAlgorithm + "' is incorrect. Expected one of the following '" + Arrays.asList(ECryptoAlgorithmSign.values()) + "' algorithms");
                aErrorList.add(EEbmsError.EBMS_FAILED_AUTHENTICATION.getAsError(aLocale));
                return ESuccess.FAILURE;
            }
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Using signature algorithm " + eSignAlgo);
            // Get Signature Digest Algorithm
            aSignedNode = XMLHelper.getFirstChildElementOfName(aSignedNode, CAS4.DS_NS, "Reference");
            aSignedNode = XMLHelper.getFirstChildElementOfName(aSignedNode, CAS4.DS_NS, "DigestMethod");
            sAlgorithm = aSignedNode == null ? null : aSignedNode.getAttribute("Algorithm");
            final ECryptoAlgorithmSignDigest eSignDigestAlgo = ECryptoAlgorithmSignDigest.getFromURIOrNull(sAlgorithm);
            if (eSignDigestAlgo == null) {
                LOGGER.error("Error processing the Security Header, your signing digest algorithm is incorrect. Expected one of the following'" + Arrays.toString(ECryptoAlgorithmSignDigest.values()) + "' algorithms");
                aErrorList.add(EEbmsError.EBMS_FAILED_AUTHENTICATION.getAsError(aLocale));
                return ESuccess.FAILURE;
            }
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Using signature digest algorithm " + eSignDigestAlgo);
        }
        // Check attachment validity only if a PartInfo element is available
        if (aUserMessage != null) {
            final boolean bBodyPayloadPresent = aState.isSoapBodyPayloadPresent();
            // Check if Attachment IDs are the same
            for (int i = 0; i < aAttachments.size(); i++) {
                String sAttachmentID = aAttachments.get(i).getHeaders().get(AttachmentUtils.MIME_HEADER_CONTENT_ID);
                if (StringHelper.hasNoText(sAttachmentID)) {
                    LOGGER.error("The provided attachment ID in the 'Content-ID' header may not be empty.");
                    aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
                    return ESuccess.FAILURE;
                }
                if (!sAttachmentID.startsWith(WSS4JAttachment.CONTENT_ID_PREFIX)) {
                    LOGGER.error("The provided attachment ID '" + sAttachmentID + "' in the 'Content-ID' header does not start with the required prefix '" + WSS4JAttachment.CONTENT_ID_PREFIX + "'");
                    aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
                    return ESuccess.FAILURE;
                }
                if (!sAttachmentID.endsWith(WSS4JAttachment.CONTENT_ID_SUFFIX)) {
                    LOGGER.error("The provided attachment ID '" + sAttachmentID + "' in the 'Content-ID' header does not end with the required suffix '" + WSS4JAttachment.CONTENT_ID_SUFFIX + "'");
                    aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
                    return ESuccess.FAILURE;
                }
                // Strip prefix and suffix
                sAttachmentID = sAttachmentID.substring(WSS4JAttachment.CONTENT_ID_PREFIX.length(), sAttachmentID.length() - WSS4JAttachment.CONTENT_ID_SUFFIX.length());
                // Add +1 because the payload has index 0
                final String sHref = aUserMessage.getPayloadInfo().getPartInfoAtIndex((bBodyPayloadPresent ? 1 : 0) + i).getHref();
                if (!sHref.contains(sAttachmentID)) {
                    LOGGER.error("The usermessage part information '" + sHref + "' does not reference the respective attachment ID '" + sAttachmentID + "'");
                    aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
                    return ESuccess.FAILURE;
                }
            }
        }
        final ESuccess eSuccess;
        if (AS4Configuration.isWSS4JSynchronizedSecurity()) {
            // Use static WSSConfig creation
            eSuccess = WSSSynchronizer.call(() -> _verifyAndDecrypt(aSOAPDoc, aAttachments, aState, aErrorList, WSSConfigManager::createStaticWSSConfig));
        } else {
            // Use instance WSSConfig creation
            eSuccess = _verifyAndDecrypt(aSOAPDoc, aAttachments, aState, aErrorList, WSSConfigManager.getInstance()::createWSSConfig);
        }
        if (eSuccess.isFailure())
            return ESuccess.FAILURE;
    }
    return ESuccess.SUCCESS;
}
Also used : Locale(java.util.Locale) ESuccess(com.helger.commons.state.ESuccess) PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) Element(org.w3c.dom.Element) IPMode(com.helger.phase4.model.pmode.IPMode) ECryptoAlgorithmSignDigest(com.helger.phase4.crypto.ECryptoAlgorithmSignDigest) WSSConfigManager(com.helger.phase4.wss.WSSConfigManager) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) ECryptoAlgorithmSign(com.helger.phase4.crypto.ECryptoAlgorithmSign) Nonnull(javax.annotation.Nonnull)

Example 9 with PMode

use of com.helger.phase4.model.pmode.PMode in project phase4 by phax.

the class DefaultPModeResolver method getPModeOfID.

@Nullable
public IPMode getPModeOfID(@Nullable final String sPModeID, @Nonnull final String sService, @Nonnull final String sAction, @Nonnull @Nonempty final String sInitiatorID, @Nonnull @Nonempty final String sResponderID, @Nullable final String sAgreementRef, @Nullable final String sAddress) {
    final IPModeManager aPModeMgr = MetaAS4Manager.getPModeMgr();
    IPMode ret = null;
    if (StringHelper.hasText(sPModeID)) {
        // An ID is present - try to resolve this ID
        ret = aPModeMgr.getPModeOfID(sPModeID);
        if (ret != null)
            return ret;
    }
    // the PMode id field is empty or null (or invalid)
    // try a combination of service and action
    ret = aPModeMgr.getPModeOfServiceAndAction(sService, sAction);
    if (ret != null)
        return ret;
    // Use default pmode based on profile
    final IAS4Profile aProfile = MetaAS4Manager.getProfileMgr().getDefaultProfileOrNull();
    if (aProfile != null)
        return aProfile.createPModeTemplate(sInitiatorID, sResponderID, sAddress);
    if (!m_bUseDefaultAsFallback) {
        // Not found and no default -> null
        return null;
    }
    // 2. Default default PMode
    return DefaultPMode.getOrCreateDefaultPMode(sInitiatorID, sResponderID, sAddress, true);
}
Also used : IAS4Profile(com.helger.phase4.profile.IAS4Profile) IPModeManager(com.helger.phase4.model.pmode.IPModeManager) IPMode(com.helger.phase4.model.pmode.IPMode) Nullable(javax.annotation.Nullable)

Example 10 with PMode

use of com.helger.phase4.model.pmode.PMode in project phase4 by phax.

the class AbstractAS4UserMessageBuilder method applyToUserMessage.

/**
 * This method applies all builder parameters onto the user message, except
 * the attachments.
 *
 * @param aUserMsg
 *        The user message the parameters should be applied to. May not be
 *        <code>null</code>.
 */
@OverridingMethodsMustInvokeSuper
protected void applyToUserMessage(@Nonnull final AS4ClientUserMessage aUserMsg) {
    if (m_aHttpRetrySettings != null)
        aUserMsg.httpRetrySettings().assignFrom(m_aHttpRetrySettings);
    aUserMsg.getHttpPoster().setHttpClientFactory(m_aHttpClientFactory);
    // Otherwise Oxalis dies
    aUserMsg.getHttpPoster().setQuoteHttpHeaders(false);
    aUserMsg.setSoapVersion(m_eSoapVersion);
    aUserMsg.setSendingDateTimeOrNow(m_aSendingDateTime);
    // Set the keystore/truststore parameters
    aUserMsg.setAS4CryptoFactory(m_aCryptoFactory);
    aUserMsg.setPMode(m_aPMode, true);
    // Set after PMode
    if (m_aReceiverCertificate != null)
        aUserMsg.cryptParams().setCertificate(m_aReceiverCertificate);
    aUserMsg.setAgreementRefValue(m_sAgreementRef);
    if (StringHelper.hasText(m_sPModeID))
        aUserMsg.setPModeID(m_sPModeID);
    else
        aUserMsg.setPModeIDFactory(x -> null);
    aUserMsg.setServiceType(m_sServiceType);
    aUserMsg.setServiceValue(m_sService);
    aUserMsg.setAction(m_sAction);
    if (StringHelper.hasText(m_sMessageID))
        aUserMsg.setMessageID(m_sMessageID);
    if (StringHelper.hasText(m_sRefToMessageID))
        aUserMsg.setRefToMessageID(m_sRefToMessageID);
    // Empty conversation ID is okay
    aUserMsg.setConversationID(m_sConversationID != null ? m_sConversationID : MessageHelperMethods.createRandomConversationID());
    aUserMsg.setFromPartyIDType(m_sFromPartyIDType);
    aUserMsg.setFromPartyID(m_sFromPartyID);
    aUserMsg.setFromRole(m_sFromRole);
    aUserMsg.setToPartyIDType(m_sToPartyIDType);
    aUserMsg.setToPartyID(m_sToPartyID);
    aUserMsg.setToRole(m_sToRole);
    for (final MessageProperty aItem : m_aMessageProperties) aUserMsg.ebms3Properties().add(aItem.getAsEbms3Property());
}
Also used : X509Certificate(java.security.cert.X509Certificate) Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) MessageHelperMethods(com.helger.phase4.messaging.domain.MessageHelperMethods) IAS4SignalMessageConsumer(com.helger.phase4.client.IAS4SignalMessageConsumer) LoggerFactory(org.slf4j.LoggerFactory) Nonempty(com.helger.commons.annotation.Nonempty) IHasID(com.helger.commons.id.IHasID) EnumHelper(com.helger.commons.lang.EnumHelper) Nonnull(javax.annotation.Nonnull) Phase4Exception(com.helger.phase4.util.Phase4Exception) Nullable(javax.annotation.Nullable) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IPMode(com.helger.phase4.model.pmode.IPMode) StringHelper(com.helger.commons.string.StringHelper) ISuccessIndicator(com.helger.commons.state.ISuccessIndicator) Consumer(java.util.function.Consumer) MessageProperty(com.helger.phase4.model.MessageProperty) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Wrapper(com.helger.commons.wrapper.Wrapper) Phase4OutgoingAttachment(com.helger.phase4.attachment.Phase4OutgoingAttachment) AS4ClientUserMessage(com.helger.phase4.client.AS4ClientUserMessage) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) MessageProperty(com.helger.phase4.model.MessageProperty) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper)

Aggregations

Nonnull (javax.annotation.Nonnull)17 PModeLeg (com.helger.phase4.model.pmode.leg.PModeLeg)15 PMode (com.helger.phase4.model.pmode.PMode)13 Document (org.w3c.dom.Document)9 IPMode (com.helger.phase4.model.pmode.IPMode)8 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)6 Test (org.junit.Test)6 EAS4CompressionMode (com.helger.phase4.attachment.EAS4CompressionMode)5 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)5 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)5 EMEP (com.helger.phase4.model.EMEP)5 EMEPBinding (com.helger.phase4.model.EMEPBinding)5 PModeParty (com.helger.phase4.model.pmode.PModeParty)5 Phase4Exception (com.helger.phase4.util.Phase4Exception)5 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)4 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)4 Ebms3Error (com.helger.phase4.ebms3header.Ebms3Error)4 HttpXMLEntity (com.helger.phase4.http.HttpXMLEntity)4 IOException (java.io.IOException)4 Nullable (javax.annotation.Nullable)4