Search in sources :

Example 16 with PMode

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

the class PModeCheckTest method testNullPModeID.

@Test
public void testNullPModeID() throws Exception {
    m_aEbms3UserMessage.getCollaborationInfo().getAgreementRef().setPmode(null);
    // Needed to set since also Action and Service combination gets checked if
    // they are already in the pmode pool
    final Ebms3Service aService = new Ebms3Service();
    aService.setValue("urn:www.cenbii.eu:profile:bii04:ver2.0");
    aService.setType("cenbii-procid-ubl");
    m_aEbms3UserMessage.getCollaborationInfo().setService(aService);
    m_aEbms3UserMessage.getCollaborationInfo().setAction("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice##urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0::2.1");
    final Document aDoc = AS4UserMessage.create(SOAP_VERSION, m_aEbms3UserMessage).setMustUnderstand(true).getAsSoapDocument(m_aPayload);
    assertNotNull(aDoc);
    sendPlainMessageAndWait(new HttpXMLEntity(aDoc, SOAP_VERSION.getMimeType()), true, null);
}
Also used : HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Ebms3Service(com.helger.phase4.ebms3header.Ebms3Service) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 17 with PMode

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

the class TwoWayAsyncPullPushTest method before.

@Before
public void before() {
    final PMode aPMode = CEFPMode.createCEFPMode(AS4TestConstants.TEST_INITIATOR, AS4TestConstants.TEST_RESPONDER, MockJettySetup.getServerAddressFromSettings(), (i, r) -> "pmode" + GlobalIDFactory.getNewPersistentLongID(), false);
    // Setting second leg to the same as first
    final PModeLeg aLeg2 = aPMode.getLeg1();
    // ESENS PMode is One Way on default settings need to change to two way
    m_aPMode = new PMode(aPMode.getID(), PModeParty.createSimple(DEFAULT_PARTY_ID + "1", CAS4.DEFAULT_ROLE), PModeParty.createSimple(DEFAULT_PARTY_ID + "1", CAS4.DEFAULT_ROLE), aPMode.getAgreement(), EMEP.TWO_WAY, EMEPBinding.PULL_PUSH, aPMode.getLeg1(), aLeg2, aPMode.getPayloadService(), aPMode.getReceptionAwareness());
    // Delete old PMode since it is getting created in the ESENS createPMode
    MetaAS4Manager.getPModeMgr().deletePMode(aPMode.getID());
    MetaAS4Manager.getPModeMgr().createOrUpdatePMode(m_aPMode);
}
Also used : PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) PMode(com.helger.phase4.model.pmode.PMode) CEFPMode(com.helger.phase4.profile.cef.CEFPMode) Before(org.junit.Before)

Example 18 with PMode

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

the class TwoWayMEPTest method before.

@Before
public void before() {
    final PMode aPMode = CEFPMode.createCEFPMode(AS4TestConstants.TEST_INITIATOR, AS4TestConstants.TEST_RESPONDER, MockJettySetup.getServerAddressFromSettings(), (i, r) -> "pmode" + GlobalIDFactory.getNewPersistentLongID(), false);
    // Setting second leg to the same as first
    final PModeLeg aLeg2 = aPMode.getLeg1();
    // ESENS PMode is One Way on default settings need to change to two way
    m_aPMode = new PMode(aPMode.getID(), PModeParty.createSimple(DEFAULT_PARTY_ID + "1", CAS4.DEFAULT_ROLE), PModeParty.createSimple(DEFAULT_PARTY_ID + "1", CAS4.DEFAULT_ROLE), aPMode.getAgreement(), EMEP.TWO_WAY, EMEPBinding.SYNC, aPMode.getLeg1(), aLeg2, aPMode.getPayloadService(), aPMode.getReceptionAwareness());
    // Delete old PMode since it is getting created in the ESENS createPMode
    MetaAS4Manager.getPModeMgr().deletePMode(aPMode.getID());
    MetaAS4Manager.getPModeMgr().createOrUpdatePMode(m_aPMode);
}
Also used : PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) PMode(com.helger.phase4.model.pmode.PMode) IPMode(com.helger.phase4.model.pmode.IPMode) CEFPMode(com.helger.phase4.profile.cef.CEFPMode) Before(org.junit.Before)

Example 19 with PMode

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

the class SignatureTest method testX509SignatureIS.

/**
 * The test uses the Issuer Serial key identifier type.
 * <p/>
 *
 * @throws java.lang.Exception
 *         Thrown when there is any problem in signing or verification
 */
@Test
public void testX509SignatureIS() throws Exception {
    final IAS4CryptoFactory aCryptoFactory = AS4CryptoFactoryProperties.getDefaultInstance();
    final Document aDoc = _getSoapEnvelope11();
    final WSSecHeader aSecHeader = new WSSecHeader(aDoc);
    aSecHeader.insertSecurityHeader();
    final WSSecSignature aBuilder = new WSSecSignature(aSecHeader);
    aBuilder.setUserInfo(aCryptoFactory.getKeyAlias(), aCryptoFactory.getKeyPassword());
    aBuilder.setKeyIdentifierType(ECryptoKeyIdentifierType.BST_DIRECT_REFERENCE.getTypeID());
    aBuilder.setSignatureAlgorithm(ECryptoAlgorithmSign.RSA_SHA_256.getAlgorithmURI());
    // PMode indicates the DigestAlgorithmen as Hash Function
    aBuilder.setDigestAlgo(ECryptoAlgorithmSignDigest.DIGEST_SHA_256.getAlgorithmURI());
    final Document signedDoc = aBuilder.build(aCryptoFactory.getCrypto());
    // final String outputString = XMLUtils.prettyDocumentToString (signedDoc);
    final WSSecurityEngine aSecEngine = new WSSecurityEngine();
    aSecEngine.setWssConfig(WSSConfigManager.getInstance().createWSSConfig());
    final WSHandlerResult aResults = aSecEngine.processSecurityHeader(signedDoc, null, null, aCryptoFactory.getCrypto());
    final WSSecurityEngineResult actionResult = aResults.getActionResults().get(Integer.valueOf(WSConstants.SIGN)).get(0);
    assertNotNull(actionResult.get(WSSecurityEngineResult.TAG_X509_CERTIFICATE));
    assertNotNull(actionResult.get(WSSecurityEngineResult.TAG_X509_REFERENCE_TYPE));
    final STRParser.REFERENCE_TYPE referenceType = (STRParser.REFERENCE_TYPE) actionResult.get(WSSecurityEngineResult.TAG_X509_REFERENCE_TYPE);
    assertSame(STRParser.REFERENCE_TYPE.DIRECT_REF, referenceType);
}
Also used : STRParser(org.apache.wss4j.dom.str.STRParser) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) WSSecHeader(org.apache.wss4j.dom.message.WSSecHeader) WSSecSignature(org.apache.wss4j.dom.message.WSSecSignature) WSSecurityEngine(org.apache.wss4j.dom.engine.WSSecurityEngine) Document(org.w3c.dom.Document) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) Test(org.junit.Test)

Example 20 with PMode

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

the class AS4RequestHandler method _invokeSPIsForIncoming.

/**
 * Invoke custom SPI message processors
 *
 * @param aHttpHeaders
 *        The received HTTP headers. Never <code>null</code>.
 * @param aEbmsUserMessage
 *        Current user message. Either this OR signal message must be
 *        non-<code>null</code>.
 * @param aEbmsSignalMessage
 *        The signal message to use. Either this OR user message must be
 *        non-<code>null</code>.
 * @param aPayloadNode
 *        Optional SOAP body payload (only if direct SOAP msg, not for MIME).
 *        May be <code>null</code>.
 * @param aDecryptedAttachments
 *        Original attachments from source message. May be <code>null</code>.
 * @param aPMode
 *        PMode to be used - may be <code>null</code> for Receipt messages.
 * @param aState
 *        The current state. Never <code>null</<code></code>.
 * @param aErrorMessagesTarget
 *        The list of error messages to be filled if something goes wrong.
 *        Never <code>null</code>.
 * @param aResponseAttachmentsTarget
 *        The list of attachments to be added to the response. Never
 *        <code>null</code>.
 * @param aSPIResult
 *        The result object to be filled. May not be <code>null</code>.
 */
private void _invokeSPIsForIncoming(@Nonnull final HttpHeaderMap aHttpHeaders, @Nullable final Ebms3UserMessage aEbmsUserMessage, @Nullable final Ebms3SignalMessage aEbmsSignalMessage, @Nullable final Node aPayloadNode, @Nullable final ICommonsList<WSS4JAttachment> aDecryptedAttachments, @Nullable final IPMode aPMode, @Nonnull final IAS4MessageState aState, @Nonnull final ICommonsList<Ebms3Error> aErrorMessagesTarget, @Nonnull final ICommonsList<WSS4JAttachment> aResponseAttachmentsTarget, @Nonnull final SPIInvocationResult aSPIResult) {
    ValueEnforcer.isTrue(aEbmsUserMessage != null || aEbmsSignalMessage != null, "User OR Signal Message must be present");
    ValueEnforcer.isFalse(aEbmsUserMessage != null && aEbmsSignalMessage != null, "Only one of User OR Signal Message may be present");
    final boolean bIsUserMessage = aEbmsUserMessage != null;
    final String sMessageID = bIsUserMessage ? aEbmsUserMessage.getMessageInfo().getMessageId() : aEbmsSignalMessage.getMessageInfo().getMessageId();
    // Get all processors
    final ICommonsList<IAS4ServletMessageProcessorSPI> aAllProcessors = m_aProcessorSupplier.get();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Trying to invoke the following " + aAllProcessors.size() + " SPIs on message ID '" + sMessageID + "': " + aAllProcessors);
    if (aAllProcessors.isEmpty())
        LOGGER.error("No IAS4ServletMessageProcessorSPI is available to process an incoming message");
    // Invoke ALL non-null SPIs
    for (final IAS4ServletMessageProcessorSPI aProcessor : aAllProcessors) if (aProcessor != null)
        try {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Invoking AS4 message processor " + aProcessor + " for incoming message");
            // Main processing
            final AS4MessageProcessorResult aResult;
            final ICommonsList<Ebms3Error> aProcessingErrorMessages = new CommonsArrayList<>();
            if (bIsUserMessage) {
                aResult = aProcessor.processAS4UserMessage(m_aMessageMetadata, aHttpHeaders, aEbmsUserMessage, aPMode, aPayloadNode, aDecryptedAttachments, aState, aProcessingErrorMessages);
            } else {
                aResult = aProcessor.processAS4SignalMessage(m_aMessageMetadata, aHttpHeaders, aEbmsSignalMessage, aPMode, aState, aProcessingErrorMessages);
            }
            // Result returned?
            if (aResult == null)
                throw new IllegalStateException("No result object present from AS4 message processor " + aProcessor + " - this is a programming error");
            if (aProcessingErrorMessages.isNotEmpty() || aResult.isFailure()) {
                if (aProcessingErrorMessages.isNotEmpty()) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("AS4 message processor " + aProcessor + " had processing errors - breaking. Details: " + aProcessingErrorMessages);
                    if (aResult.isSuccess())
                        LOGGER.warn("Processing errors are present but success was returned by a previous AS4 message processor " + aProcessor + " - considering the whole processing to be failed instead");
                    aErrorMessagesTarget.addAll(aProcessingErrorMessages);
                }
                if (aResult.isFailure() && aResult.hasErrorMessage()) {
                    aErrorMessagesTarget.add(EEbmsError.EBMS_OTHER.getAsEbms3Error(m_aLocale, sMessageID, "Invoked AS4 message processor SPI " + aProcessor + " on '" + sMessageID + "' returned a failure: " + aResult.getErrorMessage()));
                }
                // Stop processing
                return;
            }
            // SPI invocation returned success and no errors
            {
                final String sAsyncResultURL = aResult.getAsyncResponseURL();
                if (StringHelper.hasText(sAsyncResultURL)) {
                    // URL present
                    if (aSPIResult.hasAsyncResponseURL()) {
                        // A second processor returned a response URL - not allowed
                        final String sErrorMsg = "Invoked AS4 message processor SPI " + aProcessor + " on '" + sMessageID + "' failed: the previous processor already returned an async response URL; it is not possible to handle two URLs. Please check your SPI implementations.";
                        LOGGER.error(sErrorMsg);
                        aErrorMessagesTarget.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsEbms3Error(m_aLocale, sMessageID, sErrorMsg));
                        // Stop processing
                        return;
                    }
                    aSPIResult.setAsyncResponseURL(sAsyncResultURL);
                    LOGGER.info("Using asynchronous response URL '" + sAsyncResultURL + "' for message ID '" + sMessageID + "'");
                }
            }
            if (bIsUserMessage) {
            // User message specific processing result handling
            // empty
            } else {
                // Signal message specific processing result handling
                assert aResult instanceof AS4SignalMessageProcessorResult;
                if (aEbmsSignalMessage.getReceipt() == null) {
                    final Ebms3UserMessage aPullReturnUserMsg = ((AS4SignalMessageProcessorResult) aResult).getPullReturnUserMessage();
                    if (aSPIResult.hasPullReturnUserMsg()) {
                        // to the pullrequest initiator
                        if (aPullReturnUserMsg != null) {
                            final String sErrorMsg = "Invoked AS4 message processor SPI " + aProcessor + " on '" + sMessageID + "' failed: the previous processor already returned a usermessage; it is not possible to return two usermessage. Please check your SPI implementations.";
                            LOGGER.warn(sErrorMsg);
                            aErrorMessagesTarget.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsEbms3Error(m_aLocale, sMessageID, sErrorMsg));
                            // Stop processing
                            return;
                        }
                    } else {
                        // Initial return user msg
                        if (aPullReturnUserMsg == null) {
                            // No message contained in the MPC
                            final String sErrorMsg = "Invoked AS4 message processor SPI " + aProcessor + " on '" + sMessageID + "' returned a failure: no UserMessage contained in the MPC";
                            LOGGER.warn(sErrorMsg);
                            aErrorMessagesTarget.add(EEbmsError.EBMS_EMPTY_MESSAGE_PARTITION_CHANNEL.getAsEbms3Error(m_aLocale, sMessageID, sErrorMsg));
                            // Stop processing
                            return;
                        }
                        // We have something :)
                        aSPIResult.setPullReturnUserMsg(aPullReturnUserMsg);
                    }
                } else {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("The AS4 EbmsSignalMessage already has a Receipt");
                }
            }
            // Add response attachments, payloads
            aResult.addAllAttachmentsTo(aResponseAttachmentsTarget);
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Successfully invoked AS4 message processor " + aProcessor);
        } catch (final AS4DecompressException ex) {
            LOGGER.error("Failed to decompress AS4 payload", ex);
            // Hack for invalid GZip content from WSS4JAttachment.getSourceStream
            aErrorMessagesTarget.add(EEbmsError.EBMS_DECOMPRESSION_FAILURE.getAsEbms3Error(m_aLocale, sMessageID));
            return;
        } catch (final RuntimeException ex) {
            // Re-throw
            throw ex;
        } catch (final Exception ex) {
            throw new IllegalStateException("Error processing incoming AS4 message with processor " + aProcessor, ex);
        }
    // Remember success
    aSPIResult.setSuccess(true);
}
Also used : AS4DecompressException(com.helger.phase4.attachment.AS4DecompressException) IAS4ServletMessageProcessorSPI(com.helger.phase4.servlet.spi.IAS4ServletMessageProcessorSPI) Ebms3Error(com.helger.phase4.ebms3header.Ebms3Error) AS4DecompressException(com.helger.phase4.attachment.AS4DecompressException) MessagingException(javax.mail.MessagingException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Phase4Exception(com.helger.phase4.util.Phase4Exception) IOException(java.io.IOException) AS4MessageProcessorResult(com.helger.phase4.servlet.spi.AS4MessageProcessorResult) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) AS4SignalMessageProcessorResult(com.helger.phase4.servlet.spi.AS4SignalMessageProcessorResult) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

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