Search in sources :

Example 1 with IAS4MessageState

use of com.helger.phase4.servlet.IAS4MessageState in project phase4 by phax.

the class StoringServletMessageProcessorSPI method processAS4UserMessage.

@Nonnull
public AS4MessageProcessorResult processAS4UserMessage(@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull final HttpHeaderMap aHttpHeaders, @Nonnull final Ebms3UserMessage aUserMessage, @Nonnull final IPMode aPMode, @Nullable final Node aPayload, @Nullable final ICommonsList<WSS4JAttachment> aIncomingAttachments, @Nonnull final IAS4MessageState aState, @Nonnull final ICommonsList<Ebms3Error> aProcessingErrorMessages) {
    LOGGER.info("Received AS4 user message");
    _dumpSoap(aMessageMetadata, aState);
    // Dump all incoming attachments (but only if they are repeatable)
    if (aIncomingAttachments != null) {
        int nAttachmentIndex = 0;
        for (final WSS4JAttachment aIncomingAttachment : aIncomingAttachments) {
            if (aIncomingAttachment.isRepeatable())
                _dumpIncomingAttachment(aMessageMetadata, aIncomingAttachment, nAttachmentIndex);
            nAttachmentIndex++;
        }
    }
    return AS4MessageProcessorResult.createSuccess();
}
Also used : WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Nonnull(javax.annotation.Nonnull)

Example 2 with IAS4MessageState

use of com.helger.phase4.servlet.IAS4MessageState in project phase4 by phax.

the class ExampleReceiveMessageProcessorSPI method processAS4UserMessage.

@Nonnull
public AS4MessageProcessorResult processAS4UserMessage(@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull final HttpHeaderMap aHttpHeaders, @Nonnull final Ebms3UserMessage aUserMessage, @Nonnull final IPMode aPMode, @Nullable final Node aPayload, @Nullable final ICommonsList<WSS4JAttachment> aIncomingAttachments, @Nonnull final IAS4MessageState aState, @Nonnull final ICommonsList<Ebms3Error> aProcessingErrorMessages) {
    LOGGER.info("Received AS4 user message");
    _dumpSoap(aMessageMetadata, aState);
    if (aIncomingAttachments != null) {
        int nIndex = 1;
        for (final WSS4JAttachment aIncomingAttachment : aIncomingAttachments) {
            final File aFile = StorageHelper.getStorageFile(aMessageMetadata, "-" + nIndex + ".payload");
            if (StreamHelper.copyInputStreamToOutputStream(aIncomingAttachment.getSourceStream(), FileHelper.getOutputStream(aFile)).isFailure())
                LOGGER.error("Failed to write incoming attachment [" + nIndex + "] to '" + aFile.getAbsolutePath() + "'");
            else
                LOGGER.info("Wrote incoming attachment [" + nIndex + "] to '" + aFile.getAbsolutePath() + "'");
            ++nIndex;
        }
    }
    return AS4MessageProcessorResult.createSuccess();
}
Also used : File(java.io.File) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Nonnull(javax.annotation.Nonnull)

Example 3 with IAS4MessageState

use of com.helger.phase4.servlet.IAS4MessageState in project phase4 by phax.

the class AS4RequestHandler method _createResponseUserMessage.

/**
 * With this method it is possible to send a usermessage back, the method will
 * check if signing is needed and if the message needs to be a mime message.
 *
 * @param aState
 *        The state of the incoming message. Never <code>null</code>.
 * @param eSoapVersion
 *        the SOAP version to use. May not be <code>null</code>
 * @param aResponseUserMsg
 *        the response user message that should be sent
 * @param sMessagingID
 *        ID of the "Messaging" element
 * @param aResponseAttachments
 *        attachments if any that should be added
 * @param aSigningParams
 *        Signing parameters
 * @param aCryptParams
 *        Encryption parameters
 * @throws WSSecurityException
 *         on error
 * @throws MessagingException
 *         on error
 */
@Nonnull
private IAS4ResponseFactory _createResponseUserMessage(@Nonnull final IAS4MessageState aState, @Nonnull final ESoapVersion eSoapVersion, @Nonnull final AS4UserMessage aResponseUserMsg, @Nonnull final ICommonsList<WSS4JAttachment> aResponseAttachments, @Nonnull final AS4SigningParams aSigningParams, @Nonnull final AS4CryptParams aCryptParams) throws WSSecurityException, MessagingException {
    final String sResponseMessageID = aResponseUserMsg.getEbms3UserMessage().getMessageInfo().getMessageId();
    final Document aSignedDoc = _signResponseIfNeeded(aResponseAttachments, aSigningParams, aResponseUserMsg.getAsSoapDocument(), eSoapVersion, aResponseUserMsg.getMessagingID());
    final IAS4ResponseFactory ret;
    if (aResponseAttachments.isEmpty()) {
        // FIXME encryption of SOAP body is missing here
        ret = new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aSignedDoc, eSoapVersion.getMimeType());
    } else {
        // Create (maybe encrypted) MIME message
        final AS4MimeMessage aMimeMsg = _createMimeMessageForResponse(aSignedDoc, aResponseAttachments, eSoapVersion, aCryptParams);
        ret = new AS4ResponseFactoryMIME(m_aMessageMetadata, aState, sResponseMessageID, aMimeMsg);
    }
    return ret;
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Document(org.w3c.dom.Document) Nonnull(javax.annotation.Nonnull)

Example 4 with IAS4MessageState

use of com.helger.phase4.servlet.IAS4MessageState in project phase4 by phax.

the class AS4RequestHandler method _invokeSPIsForResponse.

private void _invokeSPIsForResponse(@Nonnull final IAS4MessageState aState, @Nullable final IAS4ResponseFactory aResponseFactory, @Nullable final HttpEntity aHttpEntity, @Nonnull final IMimeType aMimeType, @Nonnull @Nonempty final String sResponseMessageID) {
    // Get response payload as byte array for multiple processing by the SPIs
    final boolean bResponsePayloadIsAvailable = aResponseFactory != null;
    byte[] aResponsePayload = null;
    if (aResponseFactory != null) {
        final HttpEntity aRealHttpEntity = aHttpEntity != null ? aHttpEntity : aResponseFactory.getHttpEntityForSending(aMimeType);
        if (aRealHttpEntity.isRepeatable()) {
            int nContentLength = (int) aRealHttpEntity.getContentLength();
            if (nContentLength < 0)
                nContentLength = 16 * CGlobal.BYTES_PER_KILOBYTE;
            try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(nContentLength)) {
                aRealHttpEntity.writeTo(aBAOS);
                aResponsePayload = aBAOS.getBufferOrCopy();
            } catch (final IOException ex) {
                LOGGER.error("Error dumping response entity", ex);
            }
        } else
            LOGGER.warn("Response entity is not repeatable and therefore not read for SPIs");
    } else
        LOGGER.info("No response factory present");
    // 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 '" + aState.getMessageID() + "' and response message ID '" + sResponseMessageID + ": " + aAllProcessors);
    for (final IAS4ServletMessageProcessorSPI aProcessor : aAllProcessors) if (aProcessor != null)
        try {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Invoking AS4 message processor " + aProcessor + " for response");
            aProcessor.processAS4ResponseMessage(m_aMessageMetadata, aState, sResponseMessageID, aResponsePayload, bResponsePayloadIsAvailable);
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Finished invoking AS4 message processor " + aProcessor + " for response");
        } catch (final RuntimeException ex) {
            // Re-throw
            throw ex;
        } catch (final Exception ex) {
            throw new IllegalStateException("Error invoking AS4 message processor " + aProcessor + " for response", ex);
        }
}
Also used : HttpEntity(org.apache.http.HttpEntity) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IOException(java.io.IOException) IAS4ServletMessageProcessorSPI(com.helger.phase4.servlet.spi.IAS4ServletMessageProcessorSPI) 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)

Example 5 with IAS4MessageState

use of com.helger.phase4.servlet.IAS4MessageState in project phase4 by phax.

the class AS4RequestHandler method _createResponseReceiptMessage.

/**
 * @param aState
 *        The processing state of the incoming message. Never
 *        <code>null</code>.
 * @param aSoapDocument
 *        document which should be used as source for the receipt to convert
 *        it to non-repudiation information. Can be <code>null</code>.
 * @param eSoapVersion
 *        SOAPVersion which should be used
 * @param aEffectiveLeg
 *        the leg that is used to determined, how the receipt should be build
 * @param aUserMessage
 *        used if no non-repudiation information is needed, prints the
 *        usermessage in receipt. Can be <code>null</code>.
 * @param aResponseAttachments
 *        that should be sent back if needed. Can be <code>null</code>.
 * @throws WSSecurityException
 */
@Nonnull
private IAS4ResponseFactory _createResponseReceiptMessage(@Nonnull final IAS4MessageState aState, @Nullable final Document aSoapDocument, @Nonnull final ESoapVersion eSoapVersion, @Nonnull @Nonempty final String sResponseMessageID, @Nonnull final PModeLeg aEffectiveLeg, @Nullable final Ebms3UserMessage aUserMessage, @Nullable final ICommonsList<WSS4JAttachment> aResponseAttachments) throws WSSecurityException {
    final AS4ReceiptMessage aReceiptMessage = AS4ReceiptMessage.create(eSoapVersion, sResponseMessageID, aUserMessage, aSoapDocument, _isSendNonRepudiationInformation(aEffectiveLeg)).setMustUnderstand(true);
    // We've got our response
    final Document aResponseDoc = aReceiptMessage.getAsSoapDocument();
    final AS4SigningParams aSigningParams = new AS4SigningParams().setFromPMode(aEffectiveLeg.getSecurity());
    final Document aSignedDoc = _signResponseIfNeeded(aResponseAttachments, aSigningParams, aResponseDoc, aEffectiveLeg.getProtocol().getSoapVersion(), aReceiptMessage.getMessagingID());
    return new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aSignedDoc, eSoapVersion.getMimeType());
}
Also used : AS4SigningParams(com.helger.phase4.crypto.AS4SigningParams) AS4ReceiptMessage(com.helger.phase4.messaging.domain.AS4ReceiptMessage) Document(org.w3c.dom.Document) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)9 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)8 Phase4Exception (com.helger.phase4.util.Phase4Exception)7 IOException (java.io.IOException)7 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)6 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)6 Node (org.w3c.dom.Node)6 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)5 MessagingException (javax.mail.MessagingException)5 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)5 Document (org.w3c.dom.Document)5 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 Ebms3PullRequest (com.helger.phase4.ebms3header.Ebms3PullRequest)4 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)4 IPMode (com.helger.phase4.model.pmode.IPMode)4 Nullable (javax.annotation.Nullable)4 ICommonsList (com.helger.commons.collection.impl.ICommonsList)3 ErrorList (com.helger.commons.error.list.ErrorList)3 HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)3 IHasInputStream (com.helger.commons.io.IHasInputStream)3