Example #1
0
  public final void contextInitialized(@Nonnull final ServletContextEvent aSCE) {
    final ServletContext aSC = aSCE.getServletContext();

    if (s_aInited.getAndSet(true))
      throw new IllegalStateException("WebAppListener was already instantiated!");

    final StopWatch aSW = StopWatch.createdStarted();
    m_aInitializationStartDT = PDTFactory.getCurrentLocalDateTime();

    // set global debug/trace mode
    final boolean bDebugMode = StringParser.parseBool(getInitParameterDebug(aSC));
    final boolean bProductionMode = StringParser.parseBool(getInitParameterProduction(aSC));
    GlobalDebug.setDebugModeDirect(bDebugMode);
    GlobalDebug.setProductionModeDirect(bProductionMode);

    final boolean bNoStartupInfo = StringParser.parseBool(getInitParameterNoStartupInfo(aSC));
    if (!bNoStartupInfo) {
      // Requires the global debug things to be present
      logStartupInfo(aSC);
    }

    // StaticServerInfo
    {
      final String sInitParameter = getInitParameterServerURL(aSC, bProductionMode);
      if (StringHelper.hasText(sInitParameter)) {
        final URL aURL = URLHelper.getAsURL(sInitParameter);
        if (aURL != null) {
          StaticServerInfo.init(
              aURL.getProtocol(), aURL.getHost(), aURL.getPort(), aSC.getContextPath());
        } else
          s_aLogger.error(
              "The init-parameter for the server URL"
                  + (bProductionMode ? " (production mode)" : " (non-production mode)")
                  + "contains the non-URL value '"
                  + sInitParameter
                  + "'");
      }
    }

    // Call callback
    beforeContextInitialized(aSC);

    // begin global context
    WebScopeManager.onGlobalBegin(aSC);

    // Init IO
    initPaths(aSC);

    // Set persistent ID provider - must be done after IO is setup
    initGlobalIDFactory();

    // Callback
    afterContextInitialized(aSC);

    // Remember end time
    m_aInitializationEndDT = PDTFactory.getCurrentLocalDateTime();

    // Finally
    if (s_aLogger.isInfoEnabled())
      s_aLogger.info(
          "Servlet context '"
              + aSC.getServletContextName()
              + "' was initialized in "
              + aSW.stopAndGetMillis()
              + " milli seconds");
  }
  /**
   * Verify the content of all contained fields so that all know issues are captured before sending.
   * This method is automatically called before the message is send (see {@link
   * #sendSynchronous()}). All verification warnings and errors are handled via the message handler.
   *
   * @throws AS2ClientBuilderException In case the message handler throws an exception in case of an
   *     error.
   * @see #setMessageHandler(IAS2ClientBuilderMessageHandler)
   */
  public void verifyContent() throws AS2ClientBuilderException {
    if (m_aKeyStoreFile == null) m_aMessageHandler.error("No AS2 key store is defined");
    else {
      if (!m_aKeyStoreFile.exists())
        m_aMessageHandler.error(
            "The provided AS2 key store '"
                + m_aKeyStoreFile.getAbsolutePath()
                + "' does not exist.");
      else if (!m_aKeyStoreFile.isFile())
        m_aMessageHandler.error(
            "The provided AS2 key store '"
                + m_aKeyStoreFile.getAbsolutePath()
                + "' is not a file but potentially a directory.");
      else if (!m_aKeyStoreFile.canWrite())
        m_aMessageHandler.error(
            "The provided AS2 key store '"
                + m_aKeyStoreFile.getAbsolutePath()
                + "' is not writable. As it is dynamically modified, it must be writable.");
    }
    if (m_sKeyStorePassword == null)
      m_aMessageHandler.error(
          "No key store password provided. If you need an empty password, please provide an empty String!");

    if (StringHelper.hasNoText(m_sAS2Subject))
      m_aMessageHandler.error("The AS2 message subject is missing");

    if (StringHelper.hasNoText(m_sSenderAS2ID))
      m_aMessageHandler.error("The AS2 sender ID is missing");
    else if (!m_sSenderAS2ID.startsWith(APP_PREFIX))
      m_aMessageHandler.warn(
          "The AS2 sender ID '"
              + m_sSenderAS2ID
              + "' should start with '"
              + APP_PREFIX
              + "' as required by the PEPPOL specification");

    if (StringHelper.hasNoText(m_sSenderAS2Email))
      m_aMessageHandler.error("The AS2 sender email address is missing");
    else if (!EmailAddressHelper.isValid(m_sSenderAS2Email))
      m_aMessageHandler.warn(
          "The AS2 sender email address '"
              + m_sSenderAS2Email
              + "' seems to be an invalid email address.");

    if (StringHelper.hasNoText(m_sSenderAS2KeyAlias))
      m_aMessageHandler.error("The AS2 sender key alias is missing");
    else if (!m_sSenderAS2KeyAlias.startsWith(APP_PREFIX))
      m_aMessageHandler.warn(
          "The AS2 sender key alias '"
              + m_sSenderAS2KeyAlias
              + "' should start with '"
              + APP_PREFIX
              + "' for the use with the dynamic AS2 partnerships");
    else if (m_sSenderAS2ID != null && !m_sSenderAS2ID.equals(m_sSenderAS2KeyAlias))
      m_aMessageHandler.warn(
          "The AS2 sender key alias ('"
              + m_sSenderAS2KeyAlias
              + "') should match the AS2 sender ID ('"
              + m_sSenderAS2ID
              + "')");

    if (StringHelper.hasNoText(m_sReceiverAS2ID))
      m_aMessageHandler.error("The AS2 receiver ID is missing");
    else if (!m_sReceiverAS2ID.startsWith(APP_PREFIX))
      m_aMessageHandler.warn(
          "The AS2 receiver ID '"
              + m_sReceiverAS2ID
              + "' should start with '"
              + APP_PREFIX
              + "' as required by the PEPPOL specification");

    if (StringHelper.hasNoText(m_sReceiverAS2KeyAlias))
      m_aMessageHandler.error("The AS2 receiver key alias is missing");
    else if (!m_sReceiverAS2KeyAlias.startsWith(APP_PREFIX))
      m_aMessageHandler.warn(
          "The AS2 receiver key alias '"
              + m_sReceiverAS2KeyAlias
              + "' should start with '"
              + APP_PREFIX
              + "' for the use with the dynamic AS2 partnerships");
    else if (m_sReceiverAS2ID != null && !m_sReceiverAS2ID.equals(m_sReceiverAS2KeyAlias))
      m_aMessageHandler.warn(
          "The AS2 receiver key alias ('"
              + m_sReceiverAS2KeyAlias
              + "') should match the AS2 receiver ID ('"
              + m_sReceiverAS2ID
              + "')");

    if (StringHelper.hasNoText(m_sReceiverAS2Url))
      m_aMessageHandler.error("The AS2 receiver URL (AS2 endpoint URL) is missing");
    else if (URLHelper.getAsURL(m_sReceiverAS2Url) == null)
      m_aMessageHandler.warn(
          "The provided AS2 receiver URL '" + m_sReceiverAS2Url + "' seems to be an invalid URL");

    if (m_aReceiverCert == null)
      m_aMessageHandler.error(
          "The receiver X.509 certificate is missing. Usually this is extracted from the SMP response");

    if (m_eSigningAlgo == null)
      m_aMessageHandler.error("The signing algorithm for the AS2 message is missing");

    if (StringHelper.hasNoText(m_sMessageIDFormat))
      m_aMessageHandler.error("The AS2 message ID format is missing.");

    if (m_aBusinessDocumentRes == null && m_aBusinessDocumentElement == null)
      m_aMessageHandler.error("The XML business document to be send is missing.");
    else if (m_aBusinessDocumentRes != null && !m_aBusinessDocumentRes.exists())
      m_aMessageHandler.error(
          "The XML business document to be send '"
              + m_aBusinessDocumentRes.getPath()
              + "' does not exist.");

    if (m_aPeppolSenderID == null)
      m_aMessageHandler.error("The PEPPOL sender participant ID is missing");
    else if (!IdentifierHelper.hasDefaultParticipantIdentifierScheme(m_aPeppolSenderID))
      m_aMessageHandler.warn(
          "The PEPPOL sender participant ID '"
              + IdentifierHelper.getIdentifierURIEncoded(m_aPeppolSenderID)
              + "' is using a non-standard scheme!");

    if (m_aPeppolReceiverID == null)
      m_aMessageHandler.error("The PEPPOL receiver participant ID is missing");
    else if (!IdentifierHelper.hasDefaultParticipantIdentifierScheme(m_aPeppolReceiverID))
      m_aMessageHandler.warn(
          "The PEPPOL receiver participant ID '"
              + IdentifierHelper.getIdentifierURIEncoded(m_aPeppolReceiverID)
              + "' is using a non-standard scheme!");

    if (m_aPeppolDocumentTypeID == null)
      m_aMessageHandler.error("The PEPPOL document type ID is missing");
    else if (!IdentifierHelper.hasDefaultDocumentTypeIdentifierScheme(m_aPeppolDocumentTypeID))
      m_aMessageHandler.warn(
          "The PEPPOL document type ID '"
              + IdentifierHelper.getIdentifierURIEncoded(m_aPeppolDocumentTypeID)
              + "' is using a non-standard scheme!");

    if (m_aPeppolProcessID == null) m_aMessageHandler.error("The PEPPOL process ID is missing");
    else if (!IdentifierHelper.hasDefaultProcessIdentifierScheme(m_aPeppolProcessID))
      m_aMessageHandler.warn(
          "The PEPPOL process ID '"
              + IdentifierHelper.getIdentifierURIEncoded(m_aPeppolProcessID)
              + "' is using a non-standard scheme!");

    if (m_aValidationKey == null)
      m_aMessageHandler.warn(
          "The validation key determining the business document validation is missing. Therefore the outgoing business document is NOT validated!");

    // Ensure that if a non-throwing message handler is installed, that the
    // sending is not performed!
    if (m_aMessageHandler.getErrorCount() > 0)
      throw new AS2ClientBuilderException(
          "Not all required fields are present so the PEPPOL AS2 client call can NOT be performed. See the message handler for details!");
  }