Skip to content

Commit

Permalink
Extracted dump filename providers into a public class
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Oct 22, 2024
1 parent 0707a67 commit dfb6078
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.time.OffsetDateTime;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -30,12 +29,8 @@
import com.helger.commons.ValueEnforcer;
import com.helger.commons.http.HttpHeaderMap;
import com.helger.commons.io.file.FileHelper;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.commons.string.StringHelper;
import com.helger.datetime.util.PDTIOHelper;
import com.helger.phase4.config.AS4Configuration;
import com.helger.phase4.incoming.IAS4IncomingMessageMetadata;
import com.helger.phase4.v3.ChangePhase4V3;

/**
* Simple file based version of {@link IAS4IncomingDumper}.
Expand All @@ -45,58 +40,13 @@
*/
public class AS4IncomingDumperFileBased extends AbstractAS4IncomingDumperWithHeaders <AS4IncomingDumperFileBased>
{
/**
* Callback interface to create a file based on the provided metadata.
*
* @author Philip Helger
* @since 0.9.8
*/
@FunctionalInterface
@ChangePhase4V3 ("Rename and extract")
public interface IFileProvider
{
/** The default file extension to be used */
String DEFAULT_FILE_EXTENSION = ".as4in";

/**
* Get the {@link File} to write the dump to. The filename must be globally
* unique. The resulting file should be an absolute path.
*
* @param aMessageMetadata
* The message metadata of the incoming message. Never
* <code>null</code>.
* @param aHttpHeaderMap
* The HTTP headers of the incoming message. Never <code>null</code>.
* @return A non-<code>null</code> {@link File}.
* @see AS4Configuration#getDumpBasePath()
*/
@Nonnull
File createFile (@Nonnull IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull HttpHeaderMap aHttpHeaderMap);

@Nonnull
static String getFilename (@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata)
{
final OffsetDateTime aLDT = aMessageMetadata.getIncomingDT ();
return aLDT.getYear () +
"/" +
StringHelper.getLeadingZero (aLDT.getMonthValue (), 2) +
"/" +
StringHelper.getLeadingZero (aLDT.getDayOfMonth (), 2) +
"/" +
PDTIOHelper.getTimeForFilename (aLDT.toLocalTime ()) +
"-" +
FilenameHelper.getAsSecureValidASCIIFilename (aMessageMetadata.getIncomingUniqueID ()) +
DEFAULT_FILE_EXTENSION;
}
}

/**
* The default relative path for incoming messages.
*/
public static final String DEFAULT_BASE_PATH = "incoming/";
private static final Logger LOGGER = LoggerFactory.getLogger (AS4IncomingDumperFileBased.class);

private final IFileProvider m_aFileProvider;
private final IAS4IncomingDumperFileProvider m_aFileProvider;

/**
* Default constructor. Writes the files to the AS4 configured data path +
Expand All @@ -108,7 +58,7 @@ public AS4IncomingDumperFileBased ()
{
this ( (aMessageMetadata,
aHttpHeaderMap) -> new File (AS4Configuration.getDumpBasePathFile (),
DEFAULT_BASE_PATH + IFileProvider.getFilename (aMessageMetadata)));
DEFAULT_BASE_PATH + IAS4IncomingDumperFileProvider.getDefaultDirectoryAndFilename (aMessageMetadata)));
}

/**
Expand All @@ -118,7 +68,7 @@ public AS4IncomingDumperFileBased ()
* The file provider that defines where to store the files. May not be
* <code>null</code>.
*/
public AS4IncomingDumperFileBased (@Nonnull final IFileProvider aFileProvider)
public AS4IncomingDumperFileBased (@Nonnull final IAS4IncomingDumperFileProvider aFileProvider)
{
ValueEnforcer.notNull (aFileProvider, "FileProvider");
m_aFileProvider = aFileProvider;
Expand Down Expand Up @@ -151,6 +101,6 @@ public static AS4IncomingDumperFileBased createForDirectory (@Nonnull final File
ValueEnforcer.notNull (aBaseDirectory, "BaseDirectory");
return new AS4IncomingDumperFileBased ( (aMessageMetadata,
aHttpHeaderMap) -> new File (aBaseDirectory,
IFileProvider.getFilename (aMessageMetadata)));
IAS4IncomingDumperFileProvider.getDefaultDirectoryAndFilename (aMessageMetadata)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.time.OffsetDateTime;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
Expand All @@ -32,15 +31,10 @@
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.http.HttpHeaderMap;
import com.helger.commons.io.file.FileHelper;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.commons.string.StringHelper;
import com.helger.datetime.util.PDTIOHelper;
import com.helger.phase4.config.AS4Configuration;
import com.helger.phase4.incoming.IAS4IncomingMessageMetadata;
import com.helger.phase4.incoming.IAS4IncomingMessageState;
import com.helger.phase4.messaging.EAS4MessageMode;
import com.helger.phase4.mgr.MetaAS4Manager;
import com.helger.phase4.v3.ChangePhase4V3;

/**
* File based implementation of {@link IAS4OutgoingDumper}.
Expand All @@ -50,58 +44,13 @@
*/
public class AS4OutgoingDumperFileBased extends AbstractAS4OutgoingDumperWithHeaders <AS4OutgoingDumperFileBased>
{
@FunctionalInterface
@ChangePhase4V3 ("Rename and extract")
public interface IFileProvider
{
/** The default file extension to be used */
String DEFAULT_FILE_EXTENSION = ".as4out";

/**
* Get the {@link File} to write the dump to. The filename must be globally
* unique. The resulting file should be an absolute path.
*
* @param eMsgMode
* Are we dumping a request or a response? Never <code>null</code>.
* Added in v1.2.0.
* @param sAS4MessageID
* The AS4 message ID that was send out. Neither <code>null</code>
* nor empty.
* @param nTry
* The number of the try to send the message. The initial try has
* value 0, the first retry has value 1 etc.
* @return A non-<code>null</code> {@link File}.
* @see AS4Configuration#getDumpBasePath()
*/
@Nonnull
File getFile (@Nonnull EAS4MessageMode eMsgMode, @Nonnull @Nonempty String sAS4MessageID, @Nonnegative int nTry);

@Nonnull
static String getFilename (@Nonnull @Nonempty final String sAS4MessageID, @Nonnegative final int nTry)
{
final OffsetDateTime aNow = MetaAS4Manager.getTimestampMgr ().getCurrentDateTime ();
return aNow.getYear () +
"/" +
StringHelper.getLeadingZero (aNow.getMonthValue (), 2) +
"/" +
StringHelper.getLeadingZero (aNow.getDayOfMonth (), 2) +
"/" +
PDTIOHelper.getTimeForFilename (aNow.toLocalTime ()) +
"-" +
FilenameHelper.getAsSecureValidASCIIFilename (sAS4MessageID) +
"-" +
nTry +
DEFAULT_FILE_EXTENSION;
}
}

/**
* The default relative path for outgoing messages.
*/
public static final String DEFAULT_BASE_PATH = "outgoing/";
private static final Logger LOGGER = LoggerFactory.getLogger (AS4OutgoingDumperFileBased.class);

private final IFileProvider m_aFileProvider;
private final IAS4OutgoingDumperFileProvider m_aFileProvider;

/**
* Default constructor. Writes the files to the AS4 configured data path +
Expand All @@ -112,7 +61,7 @@ static String getFilename (@Nonnull @Nonempty final String sAS4MessageID, @Nonne
public AS4OutgoingDumperFileBased ()
{
this ( (eMsgMode, sMessageID, nTry) -> new File (AS4Configuration.getDumpBasePathFile (),
DEFAULT_BASE_PATH + IFileProvider.getFilename (sMessageID, nTry)));
DEFAULT_BASE_PATH + IAS4OutgoingDumperFileProvider.getDefaultDirectoryAndFilename (sMessageID, nTry)));
}

/**
Expand All @@ -122,14 +71,14 @@ public AS4OutgoingDumperFileBased ()
* The file provider that defines where to store the files. May not be
* <code>null</code>.
*/
public AS4OutgoingDumperFileBased (@Nonnull final IFileProvider aFileProvider)
public AS4OutgoingDumperFileBased (@Nonnull final IAS4OutgoingDumperFileProvider aFileProvider)
{
ValueEnforcer.notNull (aFileProvider, "FileProvider");
m_aFileProvider = aFileProvider;
}

@Nonnull
protected final IFileProvider getFileProvider ()
protected final IAS4OutgoingDumperFileProvider getFileProvider ()
{
return m_aFileProvider;
}
Expand Down Expand Up @@ -165,6 +114,6 @@ public static AS4OutgoingDumperFileBased createForDirectory (@Nonnull final File
return new AS4OutgoingDumperFileBased ( (eMsgMode,
sMessageID,
nTry) -> new File (aBaseDirectory,
IFileProvider.getFilename (sMessageID, nTry)));
IAS4OutgoingDumperFileProvider.getDefaultDirectoryAndFilename (sMessageID, nTry)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2015-2024 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.phase4.dump;

import java.io.File;
import java.time.OffsetDateTime;

import javax.annotation.Nonnull;

import com.helger.commons.http.HttpHeaderMap;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.commons.string.StringHelper;
import com.helger.datetime.util.PDTIOHelper;
import com.helger.phase4.config.AS4Configuration;
import com.helger.phase4.incoming.IAS4IncomingMessageMetadata;

/**
* Callback interface to create a file based on the provided metadata.
*
* @author Philip Helger
* @since 0.9.8
*/
@FunctionalInterface
public interface IAS4IncomingDumperFileProvider
{
/** The default file extension to be used */
String DEFAULT_FILE_EXTENSION = ".as4in";

/**
* Get the {@link File} to write the dump to. The filename must be globally
* unique. The resulting file should be an absolute path.
*
* @param aMessageMetadata
* The message metadata of the incoming message. Never
* <code>null</code>.
* @param aHttpHeaderMap
* The HTTP headers of the incoming message. Never <code>null</code>.
* @return A non-<code>null</code> {@link File}.
* @see AS4Configuration#getDumpBasePath()
*/
@Nonnull
File createFile (@Nonnull IAS4IncomingMessageMetadata aMessageMetadata, @Nonnull HttpHeaderMap aHttpHeaderMap);

@Nonnull
static String getDefaultDirectoryName (@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata)
{
final OffsetDateTime aLDT = aMessageMetadata.getIncomingDT ();
return aLDT.getYear () +
"/" +
StringHelper.getLeadingZero (aLDT.getMonthValue (), 2) +
"/" +
StringHelper.getLeadingZero (aLDT.getDayOfMonth (), 2);
}

@Nonnull
static String getDefaultFilename (@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata)
{
final OffsetDateTime aLDT = aMessageMetadata.getIncomingDT ();
return PDTIOHelper.getTimeForFilename (aLDT.toLocalTime ()) +
'-' +
FilenameHelper.getAsSecureValidASCIIFilename (aMessageMetadata.getIncomingUniqueID ()) +
DEFAULT_FILE_EXTENSION;
}

@Nonnull
static String getDefaultDirectoryAndFilename (@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata)
{
return getDefaultDirectoryName (aMessageMetadata) + "/" + getDefaultFilename (aMessageMetadata);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2015-2024 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.phase4.dump;

import java.io.File;
import java.time.OffsetDateTime;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.commons.string.StringHelper;
import com.helger.datetime.util.PDTIOHelper;
import com.helger.phase4.config.AS4Configuration;
import com.helger.phase4.messaging.EAS4MessageMode;
import com.helger.phase4.mgr.MetaAS4Manager;

@FunctionalInterface
public interface IAS4OutgoingDumperFileProvider
{
/** The default file extension to be used */
String DEFAULT_FILE_EXTENSION = ".as4out";

/**
* Get the {@link File} to write the dump to. The filename must be globally
* unique. The resulting file should be an absolute path.
*
* @param eMsgMode
* Are we dumping a request or a response? Never <code>null</code>.
* Added in v1.2.0.
* @param sAS4MessageID
* The AS4 message ID that was send out. Neither <code>null</code> nor
* empty.
* @param nTry
* The number of the try to send the message. The initial try has value
* 0, the first retry has value 1 etc.
* @return A non-<code>null</code> {@link File}.
* @see AS4Configuration#getDumpBasePath()
*/
@Nonnull
File getFile (@Nonnull EAS4MessageMode eMsgMode, @Nonnull @Nonempty String sAS4MessageID, @Nonnegative int nTry);

@Nonnull
static String getDefaultDirectoryName (@Nonnull final OffsetDateTime aNow)
{
return aNow.getYear () +
"/" +
StringHelper.getLeadingZero (aNow.getMonthValue (), 2) +
"/" +
StringHelper.getLeadingZero (aNow.getDayOfMonth (), 2);
}

@Nonnull
static String getDefaultFilename (@Nonnull final OffsetDateTime aNow,
@Nonnull @Nonempty final String sAS4MessageID,
@Nonnegative final int nTry)
{
return PDTIOHelper.getTimeForFilename (aNow.toLocalTime ()) +
"-" +
FilenameHelper.getAsSecureValidASCIIFilename (sAS4MessageID) +
"-" +
nTry +
DEFAULT_FILE_EXTENSION;
}

@Nonnull
static String getDefaultDirectoryAndFilename (@Nonnull @Nonempty final String sAS4MessageID,
@Nonnegative final int nTry)
{
final OffsetDateTime aNow = MetaAS4Manager.getTimestampMgr ().getCurrentDateTime ();
return getDefaultDirectoryName (aNow) + '/' + getDefaultFilename (aNow, sAS4MessageID, nTry);
}
}
Loading

0 comments on commit dfb6078

Please sign in to comment.