Skip to content

Commit

Permalink
Started making incoming dumper more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Oct 1, 2019
1 parent dbc481e commit 20a3e17
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 70 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ The subproject `as2-demo-webapp` is licensed under the Apache 2 license.

* v4.4.5 - work in progress
* Added custom MIC matching handler to class `AS2ClientSettings`
* Added the possibility to provide a custom callback to receive the certificate used in class `AS2ClientSettings`
* Added the possibility to provide a custom callback to receive the certificate used in class `AS2ClientSettings`
* Moved `IHTTPOutgoingDumperFactory` from package ".http" to ".dump" (incompatible change)
* v4.4.4 - 2019-09-26
* Removed all information about the exception from the MDN (see [#94](https://github.com/phax/as2-lib/pull/94))
* Updated to ph-commons 9.3.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import com.helger.as2lib.disposition.DispositionOptions;
import com.helger.as2lib.processor.resender.IProcessorResenderModule;
import com.helger.as2lib.processor.sender.AbstractHttpSenderModule;
import com.helger.as2lib.util.http.IHTTPOutgoingDumperFactory;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumperFactory;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.functional.IConsumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import javax.activation.DataSource;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;

Expand Down Expand Up @@ -147,9 +148,11 @@ public void handle (@Nonnull final AbstractActiveNetModule aOwner, @Nonnull fina
// Read in the message request, headers, and data
try
{
final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
final DataSource aDataSourceBody = readAndDecodeHttpRequest (new AS2InputStreamProviderSocket (aSocket),
aResponseHandler,
aMsg);
aMsg,
aIncomingDumper);
aData = StreamHelper.getAllBytes (aDataSourceBody.getInputStream ());

// Asynch MDN 2007-03-12
Expand Down Expand Up @@ -383,7 +386,9 @@ public boolean checkAsyncMDN (@Nonnull final AS2Message aMsg) throws OpenAS2Exce
return true;
}

public void reparse (@Nonnull final AS2Message aMsg, @Nonnull final AS2HttpClient aHttpClient) throws OpenAS2Exception
public void reparse (@Nonnull final AS2Message aMsg,
@Nonnull final AS2HttpClient aHttpClient,
@Nullable final IHTTPIncomingDumper aIncomingDumper) throws OpenAS2Exception
{
// Create a MessageMDN and copy HTTP headers
final IMessageMDN aMDN = new AS2MessageMDN (aMsg);
Expand Down Expand Up @@ -413,7 +418,6 @@ public void reparse (@Nonnull final AS2Message aMsg, @Nonnull final AS2HttpClien
StreamHelper.close (aMDNStream);
}

final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
if (aIncomingDumper != null)
aIncomingDumper.dumpIncomingRequest (aMDN.headers ().getAllHeaderLines (true),
aMDNStream != null ? aMDNStream.toByteArray ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.helger.as2lib.util.AS2Helper;
import com.helger.as2lib.util.AS2HttpHelper;
import com.helger.as2lib.util.AS2IOHelper;
import com.helger.as2lib.util.dump.IHTTPIncomingDumper;
import com.helger.as2lib.util.http.AS2HttpResponseHandlerSocket;
import com.helger.as2lib.util.http.AS2InputStreamProviderSocket;
import com.helger.as2lib.util.http.HTTPHelper;
Expand Down Expand Up @@ -697,7 +698,11 @@ public void handle (@Nonnull final AbstractActiveNetModule aOwner, @Nonnull fina
try
{
// Read in the message request, headers, and data
aMsgDataSource = readAndDecodeHttpRequest (new AS2InputStreamProviderSocket (aSocket), aResponseHandler, aMsg);
final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
aMsgDataSource = readAndDecodeHttpRequest (new AS2InputStreamProviderSocket (aSocket),
aResponseHandler,
aMsg,
aIncomingDumper);
}
catch (final Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.slf4j.LoggerFactory;

import com.helger.as2lib.message.IMessage;
import com.helger.as2lib.util.dump.IHTTPIncomingDumper;
import com.helger.as2lib.util.http.HTTPHelper;
import com.helger.as2lib.util.http.IAS2HttpResponseHandler;
import com.helger.as2lib.util.http.IAS2InputStreamProvider;
Expand Down Expand Up @@ -115,10 +116,12 @@ public String getClientInfo (@Nonnull final Socket aSocket)
@Nonnull
protected DataSource readAndDecodeHttpRequest (@Nonnull final IAS2InputStreamProvider aISP,
@Nonnull final IAS2HttpResponseHandler aResponseHandler,
@Nonnull final IMessage aMsg) throws IOException, MessagingException
@Nonnull final IMessage aMsg,
@Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException,
MessagingException
{
// Main read
DataSource aPayload = HTTPHelper.readHttpRequest (aISP, aResponseHandler, aMsg);
DataSource aPayload = HTTPHelper.readHttpRequest (aISP, aResponseHandler, aMsg, aIncomingDumper);

// Check the transfer encoding of the request. If none is provided, check
// the partnership for a default one. If none is in the partnership used the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,14 +642,18 @@ protected void updateHttpHeaders (@Nonnull final AS2HttpHeaderSetter aHeaderSett
* URLConnection
* @param aOriginalMIC
* mic value from original msg
* @param aIncomingDumper
* Incoming dumper. May be <code>null</code>.
* @throws OpenAS2Exception
* in case of an error
* @throws IOException
* in case of an IO error
*/
protected void receiveSyncMDN (@Nonnull final AS2Message aMsg,
@Nonnull final AS2HttpClient aHttpClient,
@Nonnull final MIC aOriginalMIC) throws OpenAS2Exception, IOException
@Nonnull final MIC aOriginalMIC,
@Nullable final IHTTPIncomingDumper aIncomingDumper) throws OpenAS2Exception,
IOException
{
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Receiving synchronous MDN for message" + aMsg.getLoggingText ());
Expand Down Expand Up @@ -679,7 +683,6 @@ protected void receiveSyncMDN (@Nonnull final AS2Message aMsg,
}

// Dump collected message
final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
if (aIncomingDumper != null)
aIncomingDumper.dumpIncomingRequest (aMDN.headers ().getAllHeaderLines (true), aMDNStream.toByteArray (), aMDN);

Expand Down Expand Up @@ -804,7 +807,8 @@ private void _sendViaHTTP (@Nonnull final AS2Message aMsg,
@Nonnull final MimeBodyPart aSecuredMimePart,
@Nullable final MIC aMIC,
@Nonnull final EContentTransferEncoding eCTE,
@Nullable final IHTTPOutgoingDumper aOutgoingDumper) throws OpenAS2Exception,
@Nullable final IHTTPOutgoingDumper aOutgoingDumper,
@Nullable final IHTTPIncomingDumper aIncomingDumper) throws OpenAS2Exception,
IOException,
MessagingException
{
Expand Down Expand Up @@ -873,7 +877,7 @@ private void _sendViaHTTP (@Nonnull final AS2Message aMsg,
// go ahead to receive sync MDN
// Note: If an MDN is requested, a MIC is present
assert aMIC != null;
receiveSyncMDN (aMsg, aConn, aMIC);
receiveSyncMDN (aMsg, aConn, aMIC, aIncomingDumper);

if (LOGGER.isInfoEnabled ())
LOGGER.info ("message sent" + aMsg.getLoggingText ());
Expand Down Expand Up @@ -938,7 +942,8 @@ public void handle (@Nonnull final String sAction,

try (final IHTTPOutgoingDumper aOutgoingDumper = getHttpOutgoingDumper (aMsg))
{
_sendViaHTTP (aMsg, aSecuredData, aMIC, eCTE, aOutgoingDumper);
final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
_sendViaHTTP (aMsg, aSecuredData, aMIC, eCTE, aOutgoingDumper, aIncomingDumper);
}
}
catch (final HttpResponseException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.net.Proxy;
import java.security.GeneralSecurityException;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -47,10 +46,10 @@
import com.helger.as2lib.exception.OpenAS2Exception;
import com.helger.as2lib.message.IBaseMessage;
import com.helger.as2lib.util.AS2IOHelper;
import com.helger.as2lib.util.dump.HTTPOutgoingDumperFileBased;
import com.helger.as2lib.util.dump.DefaultHTTPOutgoingDumperFactory;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumper;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumperFactory;
import com.helger.as2lib.util.http.AS2HttpClient;
import com.helger.as2lib.util.http.IHTTPOutgoingDumperFactory;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.OverrideOnDemand;
Expand Down Expand Up @@ -82,29 +81,6 @@ public abstract class AbstractHttpSenderModule extends AbstractSenderModule
/** Default quote header values: false */
public static final boolean DEFAULT_QUOTE_HEADER_VALUES = false;

private static final class OutgoingDumperFactory implements IHTTPOutgoingDumperFactory
{
// Counter to ensure unique filenames
private final AtomicInteger m_aCounter = new AtomicInteger (0);
private final File m_aDumpDirectory;

public OutgoingDumperFactory (@Nonnull final File aDumpDirectory)
{
m_aDumpDirectory = aDumpDirectory;
}

@Nonnull
public IHTTPOutgoingDumper apply (@Nonnull final IBaseMessage aMsg)
{
return new HTTPOutgoingDumperFileBased (new File (m_aDumpDirectory,
"as2-outgoing-" +
Long.toString (System.currentTimeMillis ()) +
"-" +
Integer.toString (m_aCounter.getAndIncrement ()) +
".http"));
}
}

private static final IHTTPOutgoingDumperFactory DEFAULT_HTTP_OUTGOING_DUMPER_FACTORY;

static
Expand All @@ -116,7 +92,7 @@ public IHTTPOutgoingDumper apply (@Nonnull final IBaseMessage aMsg)
{
final File aDumpDirectory = new File (sHttpDumpOutgoingDirectory);
AS2IOHelper.getFileOperationManager ().createDirIfNotExisting (aDumpDirectory);
DEFAULT_HTTP_OUTGOING_DUMPER_FACTORY = new OutgoingDumperFactory (aDumpDirectory);
DEFAULT_HTTP_OUTGOING_DUMPER_FACTORY = new DefaultHTTPOutgoingDumperFactory (aDumpDirectory);
}
else
DEFAULT_HTTP_OUTGOING_DUMPER_FACTORY = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.helger.as2lib.util.dump;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Nonnull;

import com.helger.as2lib.message.IBaseMessage;

/**
* Default implementation of {@link IHTTPOutgoingDumperFactory}. Extracted as a
* public class in v4.4.5.
*
* @author Philip Helger
*/
public class DefaultHTTPOutgoingDumperFactory implements IHTTPOutgoingDumperFactory
{
// Counter to ensure unique filenames
private final AtomicInteger m_aCounter = new AtomicInteger (0);
private final File m_aDumpDirectory;

public DefaultHTTPOutgoingDumperFactory (@Nonnull final File aDumpDirectory)
{
m_aDumpDirectory = aDumpDirectory;
}

@Nonnull
public IHTTPOutgoingDumper apply (@Nonnull final IBaseMessage aMsg)
{
return new HTTPOutgoingDumperFileBased (new File (m_aDumpDirectory,
"as2-outgoing-" +
Long.toString (System.currentTimeMillis ()) +
"-" +
Integer.toString (m_aCounter.getAndIncrement ()) +
".http"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
* are those of the authors and should not be interpreted as representing
* official policies, either expressed or implied, of the FreeBSD Project.
*/
package com.helger.as2lib.util.http;
package com.helger.as2lib.util.dump;

import com.helger.as2lib.message.IBaseMessage;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumper;
import com.helger.commons.functional.IFunction;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public static ICommonsList <String> getAllHTTPHeaderLines (@Nonnull final Intern
}

/**
* @return the dumper for incoming HTTP requests or <code>null</code> if none
* is present
* @return the global dumper for incoming HTTP requests or <code>null</code>
* if none is present
* @since 3.0.1
*/
@Nullable
Expand All @@ -196,7 +196,7 @@ public static IHTTPIncomingDumper getHTTPIncomingDumper ()
}

/**
* @return the dumper factory for incoming HTTP requests. Never
* @return the global dumper factory for incoming HTTP requests. Never
* <code>null</code>.
* @since 4.4.0
*/
Expand All @@ -207,7 +207,7 @@ public static ISupplier <? extends IHTTPIncomingDumper> getHTTPIncomingDumperFac
}

/**
* Set the factory for creating dumper for incoming HTTP requests.
* Set the global factory for creating dumper for incoming HTTP requests.
*
* @param aHttpDumperFactory
* The dumper factory to be used. May not be <code>null</code>.
Expand All @@ -230,6 +230,8 @@ public static void setHTTPIncomingDumperFactory (@Nonnull final ISupplier <? ext
* The HTTP response handler to be used. May not be <code>null</code>.
* @param aMsg
* The Message to be filled. May not be <code>null</code>.
* @param aIncomingDumper
* Optional incoming HTTP dumper. May be <code>null</code>.
* @return A {@link DataSource} that holds/refers to the body.
* @throws IOException
* In case of error reading from the InputStream
Expand All @@ -239,7 +241,9 @@ public static void setHTTPIncomingDumperFactory (@Nonnull final ISupplier <? ext
@Nonnull
public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP,
@Nonnull final IAS2HttpResponseHandler aResponseHandler,
@Nonnull final IMessage aMsg) throws IOException, MessagingException
@Nonnull final IMessage aMsg,
@Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException,
MessagingException
{
// Get the stream to read from
final InputStream aIS = aISP.getInputStream ();
Expand Down Expand Up @@ -331,7 +335,6 @@ public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider
}

// Dump on demand
final IHTTPIncomingDumper aIncomingDumper = getHTTPIncomingDumper ();
if (aIncomingDumper != null)
aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders),
aBytePayLoad != null ? aBytePayLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import com.helger.as2lib.util.cert.AS2KeyStoreHelper;
import com.helger.as2lib.util.dump.HTTPIncomingDumperStreamBased;
import com.helger.as2lib.util.dump.HTTPOutgoingDumperStreamBased;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumperFactory;
import com.helger.as2lib.util.http.HTTPHelper;
import com.helger.as2lib.util.http.IHTTPOutgoingDumperFactory;
import com.helger.commons.io.stream.NonClosingOutputStream;
import com.helger.commons.mime.CMimeType;
import com.helger.mail.cte.EContentTransferEncoding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
import com.helger.as2lib.util.cert.AS2KeyStoreHelper;
import com.helger.as2lib.util.dump.HTTPIncomingDumperStreamBased;
import com.helger.as2lib.util.dump.HTTPOutgoingDumperStreamBased;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumperFactory;
import com.helger.as2lib.util.http.HTTPHelper;
import com.helger.as2lib.util.http.IHTTPOutgoingDumperFactory;
import com.helger.commons.debug.GlobalDebug;
import com.helger.commons.io.stream.NonClosingOutputStream;
import com.helger.commons.mime.CMimeType;
Expand Down
Loading

0 comments on commit 20a3e17

Please sign in to comment.