Skip to content

Commit

Permalink
Added a status endpoint to the demo web app; #215
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 26, 2024
1 parent bdd6ca4 commit 7da9b82
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ private static void _initAS4 ()
{
AS4ServerInitializer.initAS4Server ();

final IAS4CryptoFactory aCF = AS4CryptoFactoryProperties.getDefaultInstance ();

// Check if crypto properties are okay
final KeyStore aKS = aCF.getKeyStore ();
if (aKS == null)
throw new InitializationException ("Failed to load configured AS4 Key store - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 key store from the crypto factory");

final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
if (aPKE == null)
throw new InitializationException ("Failed to load configured AS4 private key - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 private key from the crypto factory");
{
final IAS4CryptoFactory aCF = AS4CryptoFactoryProperties.getDefaultInstance ();

final KeyStore aKS = aCF.getKeyStore ();
if (aKS == null)
throw new InitializationException ("Failed to load configured AS4 Key store - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 key store from the crypto factory");

final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
if (aPKE == null)
throw new InitializationException ("Failed to load configured AS4 private key - fix the configuration");
LOGGER.info ("Successfully loaded configured AS4 private key from the crypto factory");
}

// Store the incoming file as is
AS4DumpManager.setIncomingDumper (new AS4IncomingDumperFileBased ( (aMessageMetadata,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.helger.phase4.server.servlet;

import com.helger.commons.http.EHttpMethod;
import com.helger.xservlet.AbstractXServlet;

import jakarta.servlet.annotation.WebServlet;

/**
* The servlet to show the application status.
*
* @author Philip Helger
*/
@WebServlet (name = "peppol-status", urlPatterns = "/peppol-status")
public class Phase4PeppolStatusServlet extends AbstractXServlet
{
public static final String SERVLET_DEFAULT_NAME = "peppol-status";
public static final String SERVLET_DEFAULT_PATH = '/' + SERVLET_DEFAULT_NAME;

public Phase4PeppolStatusServlet ()
{
handlerRegistry ().registerHandler (EHttpMethod.GET, new Phase4PeppolStatusXServletHandler ());
handlerRegistry ().unregisterHandler (EHttpMethod.OPTIONS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.helger.phase4.server.servlet;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.cert.X509Certificate;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.datetime.PDTFactory;
import com.helger.commons.datetime.PDTWebDateHelper;
import com.helger.commons.debug.GlobalDebug;
import com.helger.commons.mime.CMimeType;
import com.helger.commons.mime.MimeType;
import com.helger.commons.system.SystemProperties;
import com.helger.json.IJsonObject;
import com.helger.json.JsonObject;
import com.helger.phase4.CAS4Version;
import com.helger.phase4.crypto.AS4CryptoFactoryProperties;
import com.helger.phase4.crypto.IAS4CryptoFactory;
import com.helger.servlet.response.UnifiedResponse;
import com.helger.web.scope.IRequestWebScopeWithoutResponse;
import com.helger.xservlet.handler.simple.IXServletSimpleHandler;

/**
* Create the demo application status information
*
* @author Philip Helger
*/
public class Phase4PeppolStatusXServletHandler implements IXServletSimpleHandler
{
private static final Logger LOGGER = LoggerFactory.getLogger (Phase4PeppolStatusXServletHandler.class);
private static final Charset CHARSET = StandardCharsets.UTF_8;

@Nonnull
@ReturnsMutableCopy
public static IJsonObject getDefaultStatusData ()
{
final IJsonObject aStatusData = new JsonObject ();
aStatusData.add ("status.datetime", PDTWebDateHelper.getAsStringXSD (PDTFactory.getCurrentOffsetDateTimeUTC ()));
aStatusData.add ("global.debug", GlobalDebug.isDebugMode ());
aStatusData.add ("global.production", GlobalDebug.isProductionMode ());
aStatusData.add ("java.version", SystemProperties.getJavaVersion ());
aStatusData.add ("phase4.version", CAS4Version.BUILD_VERSION);
aStatusData.add ("phase4.build-timestamp", CAS4Version.BUILD_TIMESTAMP);

final IAS4CryptoFactory aCF = AS4CryptoFactoryProperties.getDefaultInstance ();
final KeyStore aKS = aCF.getKeyStore ();
aStatusData.add ("phase4.keystore.loaded", aKS != null);
if (aKS != null)
{
aStatusData.add ("phase4.keystore.key.alias", aCF.getKeyAlias ());
final KeyStore.PrivateKeyEntry aPKE = aCF.getPrivateKeyEntry ();
aStatusData.add ("phase4.keystore.key.loaded", aPKE != null);
if (aPKE != null)
{
final X509Certificate aCert = (X509Certificate) aPKE.getCertificate ();
aStatusData.add ("phase4.keystore.key.issuer", aCert.getIssuerX500Principal ().getName ());
aStatusData.add ("phase4.keystore.key.subject", aCert.getSubjectX500Principal ().getName ());
}
}

return aStatusData;
}

public void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
@Nonnull final UnifiedResponse aUnifiedResponse) throws Exception
{
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Status information requested");

// Build data to provide
final IJsonObject aStatusData = getDefaultStatusData ();

// Put JSON on response
aUnifiedResponse.disableCaching ();
aUnifiedResponse.setMimeType (new MimeType (CMimeType.APPLICATION_JSON).addParameter (CMimeType.PARAMETER_NAME_CHARSET,
CHARSET.name ()));
aUnifiedResponse.setContentAndCharset (aStatusData.getAsJsonString (), CHARSET);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private StorageHelper ()
{}

@Nonnull
private static File _getStorageFile (@Nonnull final OffsetDateTime aLDT, @Nonnull final String sExt)
private static File _getStorageFile (@Nonnull final OffsetDateTime aLDT, @Nonnull final String sFilenameExt)
{
final String sYear = StringHelper.getLeadingZero (aLDT.getYear (), 4);
final String sMonth = StringHelper.getLeadingZero (aLDT.getMonthValue (), 2);
Expand All @@ -57,30 +57,30 @@ private static File _getStorageFile (@Nonnull final OffsetDateTime aLDT, @Nonnul
"-" +
FILE_SEQ_COUNTER.incrementAndGet () +
"-" +
sExt);
sFilenameExt);
return WebFileIO.getDataIO ().getFile ("as4dump/" + sYear + "/" + sMonth + "/" + sDay + "/" + sFilename);
}

@Nonnull
public static File getStorageFile (@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata,
@Nonnull final String sExt)
@Nonnull final String sFilenameExt)
{
ValueEnforcer.notNull (aMessageMetadata, "MessageMetadata");
ValueEnforcer.notEmpty (sExt, "Ext");
ValueEnforcer.isTrue (sExt.contains ("."), "Extension must contain a dot");
ValueEnforcer.notEmpty (sFilenameExt, "Ext");
ValueEnforcer.isTrue (sFilenameExt.contains ("."), "Extension must contain a dot");

return _getStorageFile (aMessageMetadata.getIncomingDT (), aMessageMetadata.getIncomingUniqueID () + sExt);
return _getStorageFile (aMessageMetadata.getIncomingDT (), aMessageMetadata.getIncomingUniqueID () + sFilenameExt);
}

@Nonnull
public static File getStorageFile (@Nonnull @Nonempty final String sMessageID,
@Nonnegative final int nTry,
@Nonnull final String sExt)
@Nonnull final String sFilenameExt)
{
ValueEnforcer.notEmpty (sMessageID, "MessageID");
ValueEnforcer.notEmpty (sExt, "Ext");
ValueEnforcer.isTrue (sExt.contains ("."), "Extension must contain a dot");
ValueEnforcer.notEmpty (sFilenameExt, "Ext");
ValueEnforcer.isTrue (sFilenameExt.contains ("."), "Extension must contain a dot");

return _getStorageFile (MetaAS4Manager.getTimestampMgr ().getCurrentDateTime (), sMessageID + "-" + nTry + sExt);
return _getStorageFile (MetaAS4Manager.getTimestampMgr ().getCurrentDateTime (), sMessageID + "-" + nTry + sFilenameExt);
}
}
4 changes: 3 additions & 1 deletion phase4-server-webapp/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
xmlns="http://java.sun.com/xml/ns/javaee"
version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<!-- com.helger.phase4.server.servlet.AS4WebAppListener is configured via annotations -->
<!-- com.helger.phase4.server.servlet.Phase4PeppolStatusServlet is configured via annotations -->

<servlet>
<servlet-name>AS4Servlet</servlet-name>
Expand Down

0 comments on commit 7da9b82

Please sign in to comment.