Search in sources :

Example 1 with MultiOutputStream

use of com.helger.phase4.util.MultiOutputStream in project phase4 by phax.

the class BasicHttpPoster method createDumpingHttpEntity.

@Nonnull
protected static HttpEntity createDumpingHttpEntity(@Nullable final IAS4OutgoingDumper aOutgoingDumper, @Nonnull final HttpEntity aSrcEntity, @Nonnull @Nonempty final String sMessageID, @Nullable final HttpHeaderMap aCustomHttpHeaders, @Nonnegative final int nTry, @Nonnull final Wrapper<OutputStream> aDumpOSHolder) throws IOException {
    if (aOutgoingDumper == null) {
        // No dumper
        return aSrcEntity;
    }
    // We don't have a message processing state
    final OutputStream aDumpOS = aOutgoingDumper.onBeginRequest(EAS4MessageMode.REQUEST, null, null, sMessageID, aCustomHttpHeaders, nTry);
    if (aDumpOS == null) {
        // No dumping needed
        return aSrcEntity;
    }
    // Otherwise multiple calls to writeTo and getContent would crash
    if (!aSrcEntity.isRepeatable())
        throw new IllegalStateException("If dumping of outgoing messages is enabled, a repeatable entity must be provided");
    // Remember the output stream used for dumping (to be able to close it
    // later)
    aDumpOSHolder.set(aDumpOS);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Creating dumping entity for the current request");
    return new HttpEntityWrapper(aSrcEntity) {

        @Override
        public void writeTo(@Nonnull @WillNotClose final OutputStream aHttpOS) throws IOException {
            // Write to multiple output streams
            final MultiOutputStream aMultiOS = new MultiOutputStream(aHttpOS, aDumpOS);
            // write to both streams
            super.writeTo(aMultiOS);
            // Flush both, but do not close both
            aMultiOS.flush();
        }
    };
}
Also used : HttpEntityWrapper(org.apache.http.entity.HttpEntityWrapper) Nonnull(javax.annotation.Nonnull) MultiOutputStream(com.helger.phase4.util.MultiOutputStream) OutputStream(java.io.OutputStream) WillNotClose(javax.annotation.WillNotClose) MultiOutputStream(com.helger.phase4.util.MultiOutputStream) Nonnull(javax.annotation.Nonnull)

Aggregations

MultiOutputStream (com.helger.phase4.util.MultiOutputStream)1 OutputStream (java.io.OutputStream)1 Nonnull (javax.annotation.Nonnull)1 WillNotClose (javax.annotation.WillNotClose)1 HttpEntityWrapper (org.apache.http.entity.HttpEntityWrapper)1