Skip to content

Commit

Permalink
Added some API extensions and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 23, 2024
1 parent cf26971 commit eac478b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ public boolean hasAsyncResponseURL ()
private Supplier <? extends ICommonsList <IAS4ServletMessageProcessorSPI>> m_aProcessorSupplier = AS4ServletMessageProcessorManager::getAllProcessors;
private IAS4RequestHandlerErrorConsumer m_aErrorConsumer;

@ChangePhase4V3 ("See https://github.com/phax/phase4/discussions/265")
public AS4RequestHandler (@Nonnull final IAS4CryptoFactory aCryptoFactorySign,
@Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt,
@Nonnull final IPModeResolver aPModeResolver,
Expand Down Expand Up @@ -684,9 +685,8 @@ private void _invokeSPIsForIncoming (@Nonnull final HttpHeaderMap aHttpHeaders,
"Only one of User OR Signal Message may be present");

final boolean bIsUserMessage = aEbmsUserMessage != null;
final String sMessageID = bIsUserMessage ? aEbmsUserMessage.getMessageInfo ().getMessageId () : aEbmsSignalMessage
.getMessageInfo ()
.getMessageId ();
final String sMessageID = bIsUserMessage ? aEbmsUserMessage.getMessageInfo ().getMessageId ()
: aEbmsSignalMessage.getMessageInfo ().getMessageId ();

// Get all processors
final ICommonsList <IAS4ServletMessageProcessorSPI> aAllProcessors = m_aProcessorSupplier.get ();
Expand Down Expand Up @@ -909,8 +909,8 @@ private void _invokeSPIsForResponse (@Nonnull final IAS4MessageState aState,
byte [] aResponsePayload = null;
if (aResponseFactory != null)
{
final HttpEntity aRealHttpEntity = aHttpEntity != null ? aHttpEntity : aResponseFactory.getHttpEntityForSending (
aMimeType);
final HttpEntity aRealHttpEntity = aHttpEntity != null ? aHttpEntity
: aResponseFactory.getHttpEntityForSending (aMimeType);
if (aRealHttpEntity.isRepeatable ())
{
int nContentLength = (int) aRealHttpEntity.getContentLength ();
Expand Down Expand Up @@ -1658,8 +1658,9 @@ private IAS4ResponseFactory _handleSoapMessage (@Nonnull final HttpHeaderMap aHt
new ResponseHandlerXml ());
}
AS4HttpDebug.debug ( () -> "SEND-RESPONSE [async sent] received: " +
(aAsyncResponse == null ? "null" : XMLWriter.getNodeAsString (aAsyncResponse,
AS4HttpDebug.getDebugXMLWriterSettings ())));
(aAsyncResponse == null ? "null"
: XMLWriter.getNodeAsString (aAsyncResponse,
AS4HttpDebug.getDebugXMLWriterSettings ())));
};

final CompletableFuture <Void> aFuture = PhotonWorkerPool.getInstance ()
Expand Down Expand Up @@ -1877,8 +1878,8 @@ public void handleRequest (@Nonnull @WillClose final InputStream aRequestInputSt
if (aResponder != null)
{
// Response present -> send back
final IAS4OutgoingDumper aRealOutgoingDumper = m_aOutgoingDumper != null ? m_aOutgoingDumper : AS4DumpManager
.getOutgoingDumper ();
final IAS4OutgoingDumper aRealOutgoingDumper = m_aOutgoingDumper != null ? m_aOutgoingDumper
: AS4DumpManager.getOutgoingDumper ();
aResponder.applyToResponse (aHttpResponse, aRealOutgoingDumper);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void customizeBeforeHandling (@Nonnull IRequestWebScopeWithoutResponse aRequestS
* The main handler doing the hard work. Never <code>null</code>.
* @since 0.9.5
*/
@ChangePhase4V3 ("Removed default")
@ChangePhase4V3 ("Remove default")
default void customizeAfterHandling (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
@Nonnull final AS4UnifiedResponse aUnifiedResponse,
@Nonnull final AS4RequestHandler aHandler)
Expand All @@ -97,7 +97,7 @@ default void customizeAfterHandling (@Nonnull final IRequestWebScopeWithoutRespo
private IPModeResolver m_aPModeResolver;
private IAS4IncomingAttachmentFactory m_aIAF;
private IAS4IncomingSecurityConfiguration m_aISC = AS4IncomingSecurityConfiguration.createDefaultInstance ();
private IHandlerCustomizer m_aHandlerCustomizer;
private IHandlerCustomizer m_aRequestHandlerCustomizer;

/**
* Default constructor.
Expand Down Expand Up @@ -181,8 +181,11 @@ public final Supplier <? extends IAS4CryptoFactory> getCryptoFactoryCryptSupplie
* @param aCryptoFactorySupplier
* Crypto factory supplier. May not be <code>null</code>.
* @return this for chaining
* @see #setCryptoFactory(IAS4CryptoFactory)
* @see #setCryptoFactorySignSupplier(Supplier)
* @see #setCryptoFactorySign(IAS4CryptoFactory)
* @see #setCryptoFactoryCryptSupplier(Supplier)
* @see #setCryptoFactoryCrypt(IAS4CryptoFactory)
* @since 0.9.15
*/
@Nonnull
Expand All @@ -192,6 +195,25 @@ public final AS4XServletHandler setCryptoFactorySupplier (@Nonnull final Supplie
return setCryptoFactorySignSupplier (aCryptoFactorySupplier).setCryptoFactoryCryptSupplier (aCryptoFactorySupplier);
}

/**
* Set the same crypto factory for signing and crypting. This is a sanity
* wrapper around {@link #setCryptoFactorySupplier(Supplier)}.
*
* @param aCryptoFactory
* Crypto factory to use. May not be <code>null</code>.
* @return this for chaining
* @see #setCryptoFactorySupplier(Supplier)
* @see #setCryptoFactoryCrypt(IAS4CryptoFactory)
* @see #setCryptoFactorySign(IAS4CryptoFactory)
* @since 2.8.2
*/
@Nonnull
public final AS4XServletHandler setCryptoFactory (@Nonnull final IAS4CryptoFactory aCryptoFactory)
{
ValueEnforcer.notNull (aCryptoFactory, "CryptoFactory");
return setCryptoFactorySupplier ( () -> aCryptoFactory);
}

/**
* @param aCryptoFactorySignSupplier
* Crypto factory supplier for signing. May not be <code>null</code>.
Expand All @@ -207,6 +229,24 @@ public final AS4XServletHandler setCryptoFactorySignSupplier (@Nonnull final Sup
return this;
}

/**
* Set the crypto factory for signing. This is a sanity wrapper around
* {@link #setCryptoFactorySignSupplier(Supplier)}.
*
* @param aCryptoFactorySign
* Crypto factory for signing to use. May not be <code>null</code>.
* @return this for chaining
* @see #setCryptoFactory(IAS4CryptoFactory)
* @see #setCryptoFactoryCrypt(IAS4CryptoFactory)
* @since 2.8.2
*/
@Nonnull
public final AS4XServletHandler setCryptoFactorySign (@Nonnull final IAS4CryptoFactory aCryptoFactorySign)
{
ValueEnforcer.notNull (aCryptoFactorySign, "CryptoFactorySign");
return setCryptoFactorySignSupplier ( () -> aCryptoFactorySign);
}

/**
* @param aCryptoFactoryCryptSupplier
* Crypto factory supplier for signing. May not be <code>null</code>.
Expand All @@ -222,6 +262,24 @@ public final AS4XServletHandler setCryptoFactoryCryptSupplier (@Nonnull final Su
return this;
}

/**
* Set the crypto factory crypting. This is a sanity wrapper around
* {@link #setCryptoFactoryCryptSupplier(Supplier)}.
*
* @param aCryptoFactoryCrypt
* Crypto factory for crypting to use. May not be <code>null</code>.
* @return this for chaining
* @see #setCryptoFactory(IAS4CryptoFactory)
* @see #setCryptoFactorySign(IAS4CryptoFactory)
* @since 2.8.2
*/
@Nonnull
public final AS4XServletHandler setCryptoFactoryCrypt (@Nonnull final IAS4CryptoFactory aCryptoFactoryCrypt)
{
ValueEnforcer.notNull (aCryptoFactoryCrypt, "CryptoFactoryCrypt");
return setCryptoFactoryCryptSupplier ( () -> aCryptoFactoryCrypt);
}

/**
* @return The {@link IPModeResolver} to be used. Never <code>null</code>.
* @since 0.9.15
Expand Down Expand Up @@ -301,9 +359,10 @@ public final AS4XServletHandler setIncomingSecurityConfiguration (@Nonnull final
* @return The additional customizer. May be <code>null</code>.
*/
@Nullable
@ChangePhase4V3 ("Rename to getRequestHandlerCustomizer")
public final IHandlerCustomizer getHandlerCustomizer ()
{
return m_aHandlerCustomizer;
return m_aRequestHandlerCustomizer;
}

/**
Expand All @@ -314,9 +373,10 @@ public final IHandlerCustomizer getHandlerCustomizer ()
* @return this for chaining
*/
@Nonnull
@ChangePhase4V3 ("Rename to setRequestHandlerCustomizer")
public final AS4XServletHandler setHandlerCustomizer (@Nullable final IHandlerCustomizer aHandlerCustomizer)
{
m_aHandlerCustomizer = aHandlerCustomizer;
m_aRequestHandlerCustomizer = aHandlerCustomizer;
return this;
}

Expand Down Expand Up @@ -459,6 +519,7 @@ protected void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRe
}

// Don't make this final, so that subclasses can call the other handleRequest
@ChangePhase4V3 ("Make final and require usage of 'IHandlerCustomizer' instead")
public void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
@Nonnull final UnifiedResponse aUnifiedResponse) throws Exception
{
Expand All @@ -479,6 +540,6 @@ public void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aReque
m_aPModeResolver,
m_aIAF,
m_aISC,
m_aHandlerCustomizer);
m_aRequestHandlerCustomizer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Philip Helger
*/
@Retention (RetentionPolicy.SOURCE)
@Target ({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
@Target ({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
public @interface ChangePhase4V3
{
String value() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@
import com.helger.phase4.profile.peppol.PeppolCRLDownloader;
import com.helger.phase4.profile.peppol.Phase4PeppolHttpClientSettings;
import com.helger.phase4.servlet.AS4IncomingProfileSelectorFromGlobal;
import com.helger.phase4.servlet.AS4RequestHandler;
import com.helger.phase4.servlet.AS4ServerInitializer;
import com.helger.phase4.servlet.AS4UnifiedResponse;
import com.helger.phase4.servlet.AS4XServletHandler;
import com.helger.phase4.servlet.AS4XServletHandler.IHandlerCustomizer;
import com.helger.phase4.servlet.mgr.AS4ProfileSelector;
import com.helger.photon.io.WebFileIO;
import com.helger.security.certificate.CertificateHelper;
import com.helger.servlet.ServletHelper;
import com.helger.smpclient.peppol.SMPClientReadOnly;
import com.helger.web.scope.IRequestWebScopeWithoutResponse;
import com.helger.web.scope.mgr.WebScopeManager;
import com.helger.xservlet.AbstractXServlet;
import com.helger.xservlet.requesttrack.RequestTrackerSettings;
Expand Down Expand Up @@ -94,17 +97,43 @@ public MyAS4Servlet ()
// This method refers to the outer static method
hdl.setCryptoFactorySupplier (ServletConfig::getCryptoFactoryToUse);

// Example code to disable PMode validation
if (false)
hdl.setHandlerCustomizer ( (aRequestScope, aUnifiedResponse, aHandler) -> aHandler.setIncomingProfileSelector (
new AS4IncomingProfileSelectorFromGlobal ()
{
public boolean validateAgainstProfile ()
{
// override;
return false;
}
}));
{
// Example code to show all possibilities of the handler customizer
hdl.setHandlerCustomizer (new IHandlerCustomizer ()
{
public void customizeBeforeHandling (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
@Nonnull final AS4UnifiedResponse aUnifiedResponse,
@Nonnull final AS4RequestHandler aHandler)
{
// Set a different crypto factory based on the request
hdl.setCryptoFactory (null);
// TODO
}

public void customizeAfterHandling (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
@Nonnull final AS4UnifiedResponse aUnifiedResponse,
@Nonnull final AS4RequestHandler aHandler)
{
// empty
}
});
}

if (false)
{
// Example code to disable PMode validation
hdl.setHandlerCustomizer ( (aRequestScope,
aUnifiedResponse,
aHandler) -> aHandler.setIncomingProfileSelector (new AS4IncomingProfileSelectorFromGlobal ()
{
public boolean validateAgainstProfile ()
{
// override;
return false;
}
}));
}

// Example for changing the receiver data based on the source URL
if (false)
Expand Down Expand Up @@ -187,8 +216,8 @@ private static void _initGlobalSettings (@Nonnull final ServletContext aSC)
HttpDebugger.setEnabled (false);

// Sanity check
if (CommandMap.getDefaultCommandMap ().createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) ==
null)
if (CommandMap.getDefaultCommandMap ()
.createDataContentHandler (CMimeType.MULTIPART_RELATED.getAsString ()) == null)
throw new IllegalStateException ("No DataContentHandler for MIME Type '" +
CMimeType.MULTIPART_RELATED.getAsString () +
"' is available. There seems to be a problem with the dependencies/packaging");
Expand Down

0 comments on commit eac478b

Please sign in to comment.