Skip to content

Commit

Permalink
Moved checks to CEF and Peppol profile; #33
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Mar 11, 2020
1 parent 760b639 commit 52e9193
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.helger.phase4.profile.cef;

import java.util.List;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
Expand All @@ -27,11 +29,16 @@
import com.helger.commons.error.SingleError;
import com.helger.commons.error.list.ErrorList;
import com.helger.commons.string.StringHelper;
import com.helger.phase4.CAS4;
import com.helger.phase4.attachment.EAS4CompressionMode;
import com.helger.phase4.crypto.ECryptoAlgorithmCrypt;
import com.helger.phase4.crypto.ECryptoAlgorithmSign;
import com.helger.phase4.crypto.ECryptoAlgorithmSignDigest;
import com.helger.phase4.ebms3header.Ebms3From;
import com.helger.phase4.ebms3header.Ebms3MessageProperties;
import com.helger.phase4.ebms3header.Ebms3Property;
import com.helger.phase4.ebms3header.Ebms3SignalMessage;
import com.helger.phase4.ebms3header.Ebms3To;
import com.helger.phase4.ebms3header.Ebms3UserMessage;
import com.helger.phase4.mgr.MetaAS4Manager;
import com.helger.phase4.model.EMEP;
Expand Down Expand Up @@ -67,61 +74,6 @@ private static IError _createError (@Nonnull final String sMsg)
return SingleError.builderError ().setErrorText (sMsg).build ();
}

public void validatePMode (@Nonnull final IPMode aPMode, @Nonnull final ErrorList aErrorList)
{
assert aErrorList.isEmpty () : "Errors in global PMode validation: " + aErrorList.toString ();

try
{
MetaAS4Manager.getPModeMgr ().validatePMode (aPMode);
}
catch (final PModeValidationException ex)
{
aErrorList.add (_createError (ex.getMessage ()));
}

final EMEP eMEP = aPMode.getMEP ();
final EMEPBinding eMEPBinding = aPMode.getMEPBinding ();

if ((eMEP == EMEP.ONE_WAY && eMEPBinding == EMEPBinding.PUSH) ||
(eMEP == EMEP.TWO_WAY && eMEPBinding == EMEPBinding.PUSH_PUSH))
{
// Valid
}
else
{
aErrorList.add (_createError ("An invalid combination of PMode MEP (" +
eMEP +
") and MEP binding (" +
eMEPBinding +
") was specified, valid are only one-way/push and two-way/push-push."));
}

// Leg1 must be present
final PModeLeg aPModeLeg1 = aPMode.getLeg1 ();
if (aPModeLeg1 == null)
{
aErrorList.add (_createError ("PMode is missing Leg 1"));
}
else
{
_checkIfLegIsValid (aPMode, aErrorList, aPModeLeg1);

if (eMEP.isTwoWay ())
{
final PModeLeg aPModeLeg2 = aPMode.getLeg2 ();
if (aPModeLeg2 == null)
{
aErrorList.add (_createError ("PMode is missing Leg 2 and is specified as TWO-WAY"));
}
else
{
_checkIfLegIsValid (aPMode, aErrorList, aPModeLeg2);
}
}
}
}

private void _checkIfLegIsValid (@Nonnull final IPMode aPMode,
@Nonnull final ErrorList aErrorList,
@Nonnull final PModeLeg aPModeLeg)
Expand Down Expand Up @@ -306,53 +258,142 @@ private void _checkIfLegIsValid (@Nonnull final IPMode aPMode,
}
}

public void validateUserMessage (@Nonnull final Ebms3UserMessage aUserMsg, @Nonnull final ErrorList aErrorList)
public void validatePMode (@Nonnull final IPMode aPMode, @Nonnull final ErrorList aErrorList)
{
ValueEnforcer.notNull (aUserMsg, "UserMsg");
assert aErrorList.isEmpty () : "Errors in global PMode validation: " + aErrorList.toString ();

if (aUserMsg.getMessageInfo () != null)
try
{
MetaAS4Manager.getPModeMgr ().validatePMode (aPMode);
}
catch (final PModeValidationException ex)
{
aErrorList.add (_createError (ex.getMessage ()));
}

if (StringHelper.hasNoText (aUserMsg.getMessageInfo ().getMessageId ()))
{
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));
}
final EMEP eMEP = aPMode.getMEP ();
final EMEPBinding eMEPBinding = aPMode.getMEPBinding ();

if ((eMEP == EMEP.ONE_WAY && eMEPBinding == EMEPBinding.PUSH) ||
(eMEP == EMEP.TWO_WAY && eMEPBinding == EMEPBinding.PUSH_PUSH))
{
// Valid
}
else
{
aErrorList.add (_createError ("MessageInfo is missing but is mandatory!"));
aErrorList.add (_createError ("An invalid combination of PMode MEP (" +
eMEP +
") and MEP binding (" +
eMEPBinding +
") was specified, valid are only one-way/push and two-way/push-push."));
}

if (aUserMsg.getPartyInfo () != null)
// Leg1 must be present
final PModeLeg aPModeLeg1 = aPMode.getLeg1 ();
if (aPModeLeg1 == null)
{
aErrorList.add (_createError ("PMode is missing Leg 1"));
}
else
{
if (aUserMsg.getPartyInfo ().getTo () != null)
_checkIfLegIsValid (aPMode, aErrorList, aPModeLeg1);

if (eMEP.isTwoWay ())
{
if (aUserMsg.getPartyInfo ().getTo ().getPartyIdCount () > 1)
final PModeLeg aPModeLeg2 = aPMode.getLeg2 ();
if (aPModeLeg2 == null)
{
aErrorList.add (_createError ("PMode is missing Leg 2 and is specified as TWO-WAY"));
}
else
{
aErrorList.add (_createError ("Only 1 PartyID is allowed in PartyTo - part"));
_checkIfLegIsValid (aPMode, aErrorList, aPModeLeg2);
}
}
if (aUserMsg.getPartyInfo ().getFrom () != null)
}
}

public void validateUserMessage (@Nonnull final Ebms3UserMessage aUserMsg, @Nonnull final ErrorList aErrorList)
{
ValueEnforcer.notNull (aUserMsg, "UserMsg");

if (aUserMsg.getMessageInfo () != null)
{
if (StringHelper.hasNoText (aUserMsg.getMessageInfo ().getMessageId ()))
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));

{
if (aUserMsg.getPartyInfo ().getFrom ().getPartyIdCount () > 1)
// Check if originalSender and finalRecipient are present
// Since these two properties are mandatory
final Ebms3MessageProperties aMessageProperties = aUserMsg.getMessageProperties ();
if (aMessageProperties == null)
aErrorList.add (_createError ("No Message Properties present but originalSender and finalRecipient have to be present"));
else
{
aErrorList.add (_createError ("Only 1 PartyID is allowed in PartyFrom - part"));
final List <Ebms3Property> aProps = aMessageProperties.getProperty ();
if (aProps.isEmpty ())
aErrorList.add (_createError ("Message Property element present but no properties"));
else
{
String sOriginalSenderC1 = null;
String sFinalRecipientC4 = null;

for (final Ebms3Property sProperty : aProps)
{
if (sProperty.getName ().equals (CAS4.ORIGINAL_SENDER))
sOriginalSenderC1 = sProperty.getValue ();
else
if (sProperty.getName ().equals (CAS4.FINAL_RECIPIENT))
sFinalRecipientC4 = sProperty.getValue ();
}

if (StringHelper.hasNoText (sOriginalSenderC1))
aErrorList.add (_createError ("'" +
CAS4.ORIGINAL_SENDER +
"' property is empty or not existant but mandatory"));
if (StringHelper.hasNoText (sFinalRecipientC4))
aErrorList.add (_createError ("'" +
CAS4.FINAL_RECIPIENT +
"' property is empty or not existant but mandatory"));
}
}
}
}
else
{
aErrorList.add (_createError ("MessageInfo is missing but is mandatory!"));
}

if (aUserMsg.getPartyInfo () == null)
{
aErrorList.add (_createError ("At least one PartyInfo element has to be present"));
}
else
{
final Ebms3To aTo = aUserMsg.getPartyInfo ().getTo ();
if (aTo != null)
{
if (aTo.getPartyIdCount () > 1)
aErrorList.add (_createError ("Only 1 PartyID is allowed in PartyInfo/To - part"));
}

final Ebms3From aFrom = aUserMsg.getPartyInfo ().getFrom ();
if (aFrom != null)
{
if (aFrom.getPartyIdCount () > 1)
aErrorList.add (_createError ("Only 1 PartyID is allowed in PartyInfo/From - part"));
}
}
}

public void validateSignalMessage (@Nonnull final Ebms3SignalMessage aSignalMsg, @Nonnull final ErrorList aErrorList)
{
ValueEnforcer.notNull (aSignalMsg, "SignalMsg");

if (StringHelper.hasNoText (aSignalMsg.getMessageInfo ().getMessageId ()))
if (aSignalMsg.getMessageInfo () != null)
{
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));
if (StringHelper.hasNoText (aSignalMsg.getMessageInfo ().getMessageId ()))
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.helger.phase4.profile.peppol;

import java.util.List;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
Expand All @@ -27,11 +29,14 @@
import com.helger.commons.error.SingleError;
import com.helger.commons.error.list.ErrorList;
import com.helger.commons.string.StringHelper;
import com.helger.phase4.CAS4;
import com.helger.phase4.attachment.EAS4CompressionMode;
import com.helger.phase4.crypto.ECryptoAlgorithmCrypt;
import com.helger.phase4.crypto.ECryptoAlgorithmSign;
import com.helger.phase4.crypto.ECryptoAlgorithmSignDigest;
import com.helger.phase4.ebms3header.Ebms3From;
import com.helger.phase4.ebms3header.Ebms3MessageProperties;
import com.helger.phase4.ebms3header.Ebms3Property;
import com.helger.phase4.ebms3header.Ebms3SignalMessage;
import com.helger.phase4.ebms3header.Ebms3To;
import com.helger.phase4.ebms3header.Ebms3UserMessage;
Expand Down Expand Up @@ -292,8 +297,7 @@ public void validatePMode (@Nonnull final IPMode aPMode, @Nonnull final ErrorLis
{
_checkIfLegIsValid (aPMode, aErrorList, aPModeLeg1);

final PModeLeg aPModeLeg2 = aPMode.getLeg2 ();
if (aPModeLeg2 != null)
if (aPMode.getLeg2 () != null)
{
aErrorList.add (_createError ("PMode should never have Leg 2"));
}
Expand All @@ -308,6 +312,43 @@ public void validateUserMessage (@Nonnull final Ebms3UserMessage aUserMsg, @Nonn
{
if (StringHelper.hasNoText (aUserMsg.getMessageInfo ().getMessageId ()))
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));

{
// Check if originalSender and finalRecipient are present
// Since these two properties are mandatory
final Ebms3MessageProperties aMessageProperties = aUserMsg.getMessageProperties ();
if (aMessageProperties == null)
aErrorList.add (_createError ("No Message Properties present but originalSender and finalRecipient have to be present"));
else
{
final List <Ebms3Property> aProps = aMessageProperties.getProperty ();
if (aProps.isEmpty ())
aErrorList.add (_createError ("Message Property element present but no properties"));
else
{
String sOriginalSenderC1 = null;
String sFinalRecipientC4 = null;

for (final Ebms3Property sProperty : aProps)
{
if (sProperty.getName ().equals (CAS4.ORIGINAL_SENDER))
sOriginalSenderC1 = sProperty.getValue ();
else
if (sProperty.getName ().equals (CAS4.FINAL_RECIPIENT))
sFinalRecipientC4 = sProperty.getValue ();
}

if (StringHelper.hasNoText (sOriginalSenderC1))
aErrorList.add (_createError ("'" +
CAS4.ORIGINAL_SENDER +
"' property is empty or not existant but mandatory"));
if (StringHelper.hasNoText (sFinalRecipientC4))
aErrorList.add (_createError ("'" +
CAS4.FINAL_RECIPIENT +
"' property is empty or not existant but mandatory"));
}
}
}
}
else
{
Expand Down Expand Up @@ -360,9 +401,10 @@ public void validateSignalMessage (@Nonnull final Ebms3SignalMessage aSignalMsg,
{
ValueEnforcer.notNull (aSignalMsg, "SignalMsg");

if (StringHelper.hasNoText (aSignalMsg.getMessageInfo ().getMessageId ()))
if (aSignalMsg.getMessageInfo () != null)
{
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));
if (StringHelper.hasNoText (aSignalMsg.getMessageInfo ().getMessageId ()))
aErrorList.add (_createError ("MessageID is missing but is mandatory!"));
}
}
}
Loading

0 comments on commit 52e9193

Please sign in to comment.