Search in sources :

Example 16 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class PModeLegProtocolMicroTypeConverter method convertToNative.

@Nonnull
public PModeLegProtocol convertToNative(@Nonnull final IMicroElement aElement) {
    final String sAddress = aElement.getAttributeValue(ATTR_ADDRESS);
    final String sSoapVersion = aElement.getAttributeValue(ATTR_SOAP_VERSION);
    final ESoapVersion eSoapVersion = ESoapVersion.getFromVersionOrNull(sSoapVersion);
    if (eSoapVersion == null)
        throw new IllegalStateException("Failed to resolve SOAP version '" + sSoapVersion + "'");
    return new PModeLegProtocol(sAddress, eSoapVersion);
}
Also used : ESoapVersion(com.helger.phase4.soap.ESoapVersion) Nonnull(javax.annotation.Nonnull)

Example 17 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class AS4IncomingHandler method parseAS4Message.

public static void parseAS4Message(@Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @Nonnull final IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull @WillClose final InputStream aPayloadIS, @Nonnull final HttpHeaderMap aHttpHeaders, @Nonnull final IAS4ParsedMessageCallback aCallback, @Nullable final IAS4IncomingDumper aIncomingDumper) throws Phase4Exception, IOException, MessagingException, WSSecurityException {
    // Determine content type
    final String sContentType = aHttpHeaders.getFirstHeaderValue(CHttpHeader.CONTENT_TYPE);
    if (StringHelper.hasNoText(sContentType))
        throw new Phase4Exception("Content-Type header is missing");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Received Content-Type string: '" + sContentType + "'");
    final IMimeType aContentType = MimeTypeParser.safeParseMimeType(sContentType);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Received Content-Type object: " + aContentType);
    if (aContentType == null)
        throw new Phase4Exception("Failed to parse Content-Type '" + sContentType + "'");
    final IMimeType aPlainContentType = aContentType.getCopyWithoutParameters();
    // Fallback to global dumper if none is provided
    final IAS4IncomingDumper aRealIncomingDumper = aIncomingDumper != null ? aIncomingDumper : AS4DumpManager.getIncomingDumper();
    Document aSoapDocument = null;
    ESoapVersion eSoapVersion = null;
    final ICommonsList<WSS4JAttachment> aIncomingAttachments = new CommonsArrayList<>();
    final Wrapper<OutputStream> aDumpOSHolder = new Wrapper<>();
    if (aPlainContentType.equals(AS4RequestHandler.MT_MULTIPART_RELATED)) {
        // MIME message
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Received MIME message");
        final String sBoundary = aContentType.getParameterValueWithName("boundary");
        if (StringHelper.hasNoText(sBoundary))
            throw new Phase4Exception("Content-Type '" + sContentType + "' misses 'boundary' parameter");
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("MIME Boundary: '" + sBoundary + "'");
        // Ensure the stream gets closed correctly
        try (final InputStream aRequestIS = AS4DumpManager.getIncomingDumpAwareInputStream(aRealIncomingDumper, aPayloadIS, aMessageMetadata, aHttpHeaders, aDumpOSHolder)) {
            // PARSING MIME Message via MultipartStream
            final MultipartStream aMulti = new MultipartStream(aRequestIS, sBoundary.getBytes(StandardCharsets.ISO_8859_1), (MultipartProgressNotifier) null);
            int nIndex = 0;
            while (true) {
                final boolean bHasNextPart = nIndex == 0 ? aMulti.skipPreamble() : aMulti.readBoundary();
                if (!bHasNextPart)
                    break;
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Found MIME part #" + nIndex);
                try (final MultipartItemInputStream aBodyPartIS = aMulti.createInputStream()) {
                    // Read headers AND content
                    final MimeBodyPart aBodyPart = new MimeBodyPart(aBodyPartIS);
                    if (nIndex == 0) {
                        // First MIME part -> SOAP document
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("Parsing first MIME part as SOAP document");
                        // Read SOAP document
                        aSoapDocument = DOMReader.readXMLDOM(aBodyPart.getInputStream());
                        IMimeType aPlainPartMT = MimeTypeParser.safeParseMimeType(aBodyPart.getContentType());
                        if (aPlainPartMT != null)
                            aPlainPartMT = aPlainPartMT.getCopyWithoutParameters();
                        // Determine SOAP version from MIME part content type
                        eSoapVersion = ESoapVersion.getFromMimeTypeOrNull(aPlainPartMT);
                        if (eSoapVersion != null && LOGGER.isDebugEnabled())
                            LOGGER.debug("Determined SOAP version " + eSoapVersion + " from Content-Type");
                        if (eSoapVersion == null && aSoapDocument != null) {
                            // Determine SOAP version from the read document
                            eSoapVersion = ESoapVersion.getFromNamespaceURIOrNull(XMLHelper.getNamespaceURI(aSoapDocument));
                            if (eSoapVersion != null && LOGGER.isDebugEnabled())
                                LOGGER.debug("Determined SOAP version " + eSoapVersion + " from XML root element namespace URI");
                        }
                    } else {
                        // MIME Attachment (index is gt 0)
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("Parsing MIME part #" + nIndex + " as attachment");
                        final WSS4JAttachment aAttachment = aIAF.createAttachment(aBodyPart, aResHelper);
                        aIncomingAttachments.add(aAttachment);
                    }
                }
                nIndex++;
            }
        }
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Read MIME message with " + aIncomingAttachments.size() + " attachment(s)");
    } else {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Received plain message");
        // Expect plain SOAP - read whole request to DOM
        // Note: this may require a huge amount of memory for large requests
        aSoapDocument = DOMReader.readXMLDOM(AS4DumpManager.getIncomingDumpAwareInputStream(aRealIncomingDumper, aPayloadIS, aMessageMetadata, aHttpHeaders, aDumpOSHolder));
        if (LOGGER.isDebugEnabled())
            if (aSoapDocument != null)
                LOGGER.debug("Successfully parsed payload as XML");
            else
                LOGGER.debug("Failed to parse payload as XML");
        if (aSoapDocument != null) {
            // Determine SOAP version from the read document
            eSoapVersion = ESoapVersion.getFromNamespaceURIOrNull(XMLHelper.getNamespaceURI(aSoapDocument));
            if (eSoapVersion != null && LOGGER.isDebugEnabled())
                LOGGER.debug("Determined SOAP version " + eSoapVersion + " from XML root element namespace URI");
        }
        if (eSoapVersion == null) {
            // Determine SOAP version from content type
            eSoapVersion = ESoapVersion.getFromMimeTypeOrNull(aPlainContentType);
            if (eSoapVersion != null && LOGGER.isDebugEnabled())
                LOGGER.debug("Determined SOAP version " + eSoapVersion + " from Content-Type");
        }
    }
    try {
        if (aSoapDocument == null) {
            // We don't have a SOAP document
            throw new Phase4Exception(eSoapVersion == null ? "Failed to parse incoming message!" : "Failed to parse incoming SOAP " + eSoapVersion.getVersion() + " document!");
        }
        if (eSoapVersion == null) {
            // We're missing a SOAP version
            throw new Phase4Exception("Failed to determine SOAP version of XML document!");
        }
        aCallback.handle(aHttpHeaders, aSoapDocument, eSoapVersion, aIncomingAttachments);
    } finally {
        // Here, the incoming dump is finally ready closed and usable
        if (aRealIncomingDumper != null && aDumpOSHolder.isSet())
            try {
                aRealIncomingDumper.onEndRequest(aMessageMetadata);
            } catch (final Exception ex) {
                LOGGER.error("IncomingDumper.onEndRequest failed. Dumper=" + aRealIncomingDumper + "; MessageMetadata=" + aMessageMetadata, ex);
            }
    }
}
Also used : Wrapper(com.helger.commons.wrapper.Wrapper) IAS4IncomingDumper(com.helger.phase4.dump.IAS4IncomingDumper) MultipartItemInputStream(com.helger.web.multipart.MultipartStream.MultipartItemInputStream) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) HasInputStream(com.helger.commons.io.stream.HasInputStream) IHasInputStream(com.helger.commons.io.IHasInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Document(org.w3c.dom.Document) 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) Phase4Exception(com.helger.phase4.util.Phase4Exception) IMimeType(com.helger.commons.mime.IMimeType) MultipartItemInputStream(com.helger.web.multipart.MultipartStream.MultipartItemInputStream) ESoapVersion(com.helger.phase4.soap.ESoapVersion) MimeBodyPart(javax.mail.internet.MimeBodyPart) MultipartStream(com.helger.web.multipart.MultipartStream) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment)

Example 18 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion 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 19 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class AS4RequestHandler method _createMimeMessageForResponse.

/**
 * Returns the MimeMessage with encrypted attachment or without depending on
 * what is configured in the PMode within Leg2.
 *
 * @param aResponseDoc
 *        the document that contains the user message
 * @param aResponseAttachments
 *        The Attachments that should be encrypted
 * @param aLeg
 *        Leg to get necessary information, EncryptionAlgorithm, SOAPVersion
 * @param sEncryptToAlias
 *        The alias into the keystore that should be used for encryption
 * @return a MimeMessage to be sent
 * @throws MessagingException
 * @throws WSSecurityException
 */
@Nonnull
private AS4MimeMessage _createMimeMessageForResponse(@Nonnull final Document aResponseDoc, @Nonnull final ICommonsList<WSS4JAttachment> aResponseAttachments, @Nonnull final ESoapVersion eSoapVersion, @Nonnull final AS4CryptParams aCryptParms) throws WSSecurityException, MessagingException {
    final AS4MimeMessage aMimeMsg;
    if (aCryptParms.isCryptEnabled(LOGGER::warn)) {
        final boolean bMustUnderstand = true;
        aMimeMsg = AS4Encryptor.encryptMimeMessage(eSoapVersion, aResponseDoc, aResponseAttachments, m_aCryptoFactory, bMustUnderstand, m_aResHelper, aCryptParms);
    } else {
        aMimeMsg = MimeMessageCreator.generateMimeMessage(eSoapVersion, aResponseDoc, aResponseAttachments);
    }
    if (aMimeMsg == null)
        throw new IllegalStateException("Failed to create MimeMessage!");
    return aMimeMsg;
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Nonnull(javax.annotation.Nonnull)

Example 20 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class MockMessages 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 = AS4TestConstants.getEBMSProperties();
    final String sPModeID;
    final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo();
    final Ebms3PayloadInfo aEbms3PayloadInfo = MessageHelperMethods.createEbms3PayloadInfo(aPayload != null, aAttachments);
    final Ebms3CollaborationInfo aEbms3CollaborationInfo;
    final Ebms3PartyInfo aEbms3PartyInfo;
    if (eSOAPVersion.equals(ESoapVersion.SOAP_11)) {
        sPModeID = SOAP_11_PARTY_ID + "-" + SOAP_11_PARTY_ID;
        aEbms3CollaborationInfo = MessageHelperMethods.createEbms3CollaborationInfo(sPModeID, DEFAULT_AGREEMENT, AS4TestConstants.TEST_SERVICE_TYPE, MockPModeGenerator.SOAP11_SERVICE, AS4TestConstants.TEST_ACTION, AS4TestConstants.TEST_CONVERSATION_ID);
        aEbms3PartyInfo = MessageHelperMethods.createEbms3PartyInfo(CAS4.DEFAULT_INITIATOR_URL, SOAP_11_PARTY_ID, CAS4.DEFAULT_RESPONDER_URL, SOAP_11_PARTY_ID);
    } else {
        sPModeID = SOAP_12_PARTY_ID + "-" + SOAP_12_PARTY_ID;
        aEbms3CollaborationInfo = MessageHelperMethods.createEbms3CollaborationInfo(sPModeID, DEFAULT_AGREEMENT, AS4TestConstants.TEST_SERVICE_TYPE, AS4TestConstants.TEST_SERVICE, AS4TestConstants.TEST_ACTION, AS4TestConstants.TEST_CONVERSATION_ID);
        aEbms3PartyInfo = MessageHelperMethods.createEbms3PartyInfo(CAS4.DEFAULT_INITIATOR_URL, SOAP_12_PARTY_ID, CAS4.DEFAULT_RESPONDER_URL, SOAP_12_PARTY_ID);
    }
    final Ebms3MessageProperties aEbms3MessageProperties = MessageHelperMethods.createEbms3MessageProperties(aEbms3Properties);
    return AS4UserMessage.create(aEbms3MessageInfo, aEbms3PayloadInfo, aEbms3CollaborationInfo, aEbms3PartyInfo, aEbms3MessageProperties, eSOAPVersion).setMustUnderstand(true);
}
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) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)29 Document (org.w3c.dom.Document)13 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)12 ESoapVersion (com.helger.phase4.soap.ESoapVersion)12 Ebms3MessageInfo (com.helger.phase4.ebms3header.Ebms3MessageInfo)9 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)9 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)9 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)8 Ebms3CollaborationInfo (com.helger.phase4.ebms3header.Ebms3CollaborationInfo)8 Ebms3MessageProperties (com.helger.phase4.ebms3header.Ebms3MessageProperties)8 Ebms3PartyInfo (com.helger.phase4.ebms3header.Ebms3PartyInfo)8 Ebms3PayloadInfo (com.helger.phase4.ebms3header.Ebms3PayloadInfo)8 Node (org.w3c.dom.Node)8 Ebms3Error (com.helger.phase4.ebms3header.Ebms3Error)6 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)6 IOException (java.io.IOException)6 MessagingException (javax.mail.MessagingException)6 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)6 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)5 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)5