Skip to content

Commit

Permalink
Improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 10, 2021
1 parent 2db7b80 commit 5d02d22
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import com.helger.as2lib.partner.Partnership;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.collection.attr.StringMap;
import com.helger.commons.http.HttpHeaderMap;
Expand Down Expand Up @@ -109,6 +110,14 @@ public final Partnership partnership ()
return m_aPartnership;
}

@Nonnull
@Nonempty
public String getLoggingText ()
{
final String sMsgID = getMessageID ();
return " [" + (sMsgID == null ? "no message ID set" : sMsgID) + "]";
}

@Override
public String toString ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,6 @@ public final void setTempSharedFileInputStream (@Nullable final TempSharedFileIn
m_aTempSharedFileInputStream = aTempSharedFileInputStream;
}

@Nonnull
@Nonempty
public String getLoggingText ()
{
final String sMsgID = getMessageID ();
return " [" + (sMsgID == null ? "no message ID set" : sMsgID) + "]";
}

@Nonnull
@Nonempty
public String getAsString ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ default void updateMessageID ()

void setPartnership (@Nonnull Partnership aPartnership);

@Nonnull
@Nonempty
String getLoggingText ();

@Nonnull
@Nonempty
String getAsString ();
Expand Down
5 changes: 0 additions & 5 deletions as2-lib/src/main/java/com/helger/as2lib/message/IMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import javax.mail.internet.MimeBodyPart;

import com.helger.as2lib.util.http.TempSharedFileInputStream;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.http.CHttpHeader;

/**
Expand Down Expand Up @@ -112,10 +111,6 @@ default void setSubject (@Nullable final String sSubject)

void setMDN (@Nullable IMessageMDN aMDN);

@Nonnull
@Nonempty
String getLoggingText ();

String getProtocol ();

boolean isRequestingMDN ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public static MimeBodyPart compressMimeBodyPart (@Nonnull final MimeBodyPart aDa
private static void _logMimeBodyPart (@Nonnull final MimeBodyPart aMimePart, @Nonnull final String sContext) throws IOException,
MessagingException
{
// Should always false in production
// Should always be false in production
if (false)
{
if (LOGGER.isInfoEnabled ())
Expand Down Expand Up @@ -422,7 +422,8 @@ public static MimeBodyPart secureMimeBodyPart (@Nonnull final MimeBodyPart aSrcP
final boolean bIncludeCertificateInSignedContent,
final boolean bUseRFC3851MICAlg,
@Nullable final ECryptoAlgorithmCrypt eCryptAlgorithm,
@Nullable final X509Certificate aReceiverCert) throws Exception
@Nullable final X509Certificate aReceiverCert,
@Nonnull final String sLoggingText) throws Exception
{
ValueEnforcer.notNull (aSrcPart, "SrcPart");
ValueEnforcer.notNull (eCTE, "ContentTransferEncoding");
Expand Down Expand Up @@ -456,6 +457,9 @@ public static MimeBodyPart secureMimeBodyPart (@Nonnull final MimeBodyPart aSrcP
// This is usually "IAS2Message.setData (aDataBP)"
// The MIC is always about the content that is signed
aCompressBeforeSignCallback.accept (aDataBP);

if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully compressed outgoing AS2 message" + sLoggingText);
}

if (eSignAlgorithm != null)
Expand All @@ -472,6 +476,9 @@ public static MimeBodyPart secureMimeBodyPart (@Nonnull final MimeBodyPart aSrcP
bUseRFC3851MICAlg,
eCTE);
_logMimeBodyPart (aDataBP, "signed");

if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully signed outgoing AS2 message" + sLoggingText);
}

if (eCompressionType != null && !bCompressBeforeSign)
Expand All @@ -481,6 +488,9 @@ public static MimeBodyPart secureMimeBodyPart (@Nonnull final MimeBodyPart aSrcP
LOGGER.debug ("Compressing outbound message after signing...");
aDataBP = compressMimeBodyPart (aDataBP, eCompressionType, eCTE);
_logMimeBodyPart (aDataBP, "compressAfterSign");

if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully compressed outgoing AS2 message" + sLoggingText);
}

if (eCryptAlgorithm != null)
Expand All @@ -489,6 +499,9 @@ public static MimeBodyPart secureMimeBodyPart (@Nonnull final MimeBodyPart aSrcP
LOGGER.debug ("Encrypting outbound message...");
aDataBP = AS2Helper.getCryptoHelper ().encrypt (aDataBP, aReceiverCert, eCryptAlgorithm, eCTE);
_logMimeBodyPart (aDataBP, "encrypted");

if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully encrypted outgoing AS2 message" + sLoggingText);
}

return aDataBP;
Expand Down Expand Up @@ -596,7 +609,8 @@ protected MimeBodyPart secure (@Nonnull final IMessage aMsg, @Nonnull final ECon
bIncludeCertificateInSignedContent,
bUseRFC3851MICAlg,
eCryptAlgorithm,
aReceiverCert);
aReceiverCert,
aMsg.getLoggingText ());
}

/**
Expand Down
6 changes: 5 additions & 1 deletion as2-lib/src/main/java/com/helger/as2lib/util/AS2Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public static void createMDNData (@Nonnull final IAS2Session aSession,
bUseOldRFC3851MicAlgs,
EContentTransferEncoding.BASE64);
aMdn.setData (aSignedReport);

if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully signed outgoing MDN message" + aMdn.getLoggingText ());
}
catch (final AS2CertificateNotFoundException | AS2KeyNotFoundException ex)
{
Expand Down Expand Up @@ -414,7 +417,8 @@ public static void parseMDN (@Nonnull final IMessage aMsg,
aMsg.attrs ()
.putIn (AS2Message.ATTRIBUTE_RECEIVED_SIGNATURE_CERTIFICATE, CertificateHelper.getPEMEncodedCertificate (aCertHolder.get ()));

LOGGER.info ("Successfully verified signature of MDN of message" + sLoggingText);
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Successfully verified signature of MDN of message" + sLoggingText);
}

final MimeMultipart aReportParts = new MimeMultipart (aMainPart.getDataHandler ().getDataSource ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public static void main (final String [] args) throws Exception
aSettings.setPartnershipName (aSettings.getSenderAS2ID () + "_" + aSettings.getReceiverAS2ID ());
// When a signed message is used, the algorithm for MIC and message must be
// identical
final ECryptoAlgorithmSign eSignAlgo = ECryptoAlgorithmSign.DIGEST_SHA_512;
final ECryptoAlgorithmSign eSignAlgo = ECryptoAlgorithmSign.DIGEST_SHA_256;
// CBC works, GCM is not supported
final ECryptoAlgorithmCrypt eCryptAlgo = ECryptoAlgorithmCrypt.CRYPT_AES128_CBC;
final ECryptoAlgorithmCrypt eCryptAlgo = ECryptoAlgorithmCrypt.CRYPT_AES128_GCM;
final ECompressionType eCompress = ECompressionType.ZLIB;
final boolean bCompressBeforeSigning = AS2ClientSettings.DEFAULT_COMPRESS_BEFORE_SIGNING;

Expand Down

0 comments on commit 5d02d22

Please sign in to comment.