Search in sources :

Example 11 with IMimeType

use of com.helger.commons.mime.IMimeType 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)

Aggregations

IMimeType (com.helger.commons.mime.IMimeType)11 Nonnull (javax.annotation.Nonnull)5 MimeTypeParserException (com.helger.commons.mime.MimeTypeParserException)3 Charset (java.nio.charset.Charset)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)2 IHasInputStream (com.helger.commons.io.IHasInputStream)2 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)2 EMandatory (com.helger.commons.state.EMandatory)2 Phase4Exception (com.helger.phase4.util.Phase4Exception)2 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 MessagingException (javax.mail.MessagingException)2 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 HasInputStream (com.helger.commons.io.stream.HasInputStream)1 Wrapper (com.helger.commons.wrapper.Wrapper)1