Repository: corona-warn-app/cwa-server
File: services/distribution/src/main/java/app/coronawarn/server/services/distribution/assembly/component/DigitalGreenCertificateStructureProvider.java (Java)

Code before:
    getRulesArchive(RuleType.Invalidation, INVALIDATION_RULES).ifPresent(dgcDirectory::addWritable);

    return dgcDirectory;
  }

  /**
   * Create onboarded countries Archive. If any exception is thrown during fetching data and packaging process,
   * an empty Archive will be published in order to not override any previous archive on CDN with broken data.
   * @return - Onboarded countries archive.
   */
  private Optional<Writable<WritableOnDisk>> getOnboardedCountriesArchive() {
    ArchiveOnDisk onboardedCountries = new ArchiveOnDisk(ONBOARDED_COUNTRIES);
    try {
      onboardedCountries
          .addWritable(new FileOnDisk("export.bin", dgcToCborMapping.constructCborCountries()));
      logger.info("Onboarded countries archive has been added to the DGC distribution folder");

      return Optional.of(new DistributionArchiveSigningDecorator(onboardedCountries, cryptoProvider,
          distributionServiceConfig));
    } catch (DigitalCovidCertificateException e) {
      logger.error("Onboarded countries archive was not overwritten because of:", e);
    } catch (FetchBusinessRulesException e) {
      logger.error("Onboarded countries archive was not overwritten because countries could not been fetched:", e);
    }

    return Optional.empty();
  }

  /**
   * Create business rules Archive. If any exception is thrown during fetching data and packaging process,
   * an empty Archive will be published in order to not override any previous archive on CDN with broken data.
   * Provided rules are filtered by rule type parameter which could be 'Acceptance' or 'Invalidation'.
   * @param ruleType - rule type to receive rules for
   * @param archiveName - archive name for packaging rules
   * @return - business rules archive
   */
  private Optional<Writable<WritableOnDisk>> getRulesArchive(RuleType ruleType, String archiveName) {
    ArchiveOnDisk rulesArchive = new ArchiveOnDisk(archiveName);

    try {
      rulesArchive
          .addWritable(new FileOnDisk("export.bin", dgcToCborMapping.constructCborRules(ruleType)));
      logger.info(archiveName + " archive has been added to the DGC distribution folder");
      return Optional.of(new DistributionArchiveSigningDecorator(rulesArchive, cryptoProvider,
          distributionServiceConfig));
    } catch (DigitalCovidCertificateException e) {
      logger.error(archiveName + " archive was not overwritten because of:", e);
    } catch (FetchBusinessRulesException e) {
      logger.error(archiveName + " archive was not overwritten because business rules could not been fetched:", e);
    }


Review comment (bug):
I see that `getOnboardedCountriesArchive().ifPresent(dgcDirectory::addWritable);` looks nice, but is there actually a benefit of wrapping this `Optional.of(new DistributionArchiveSigningDecorator(onboardedCountries, cryptoProvider,` in an Optional? The point of failure is `dgcToCborMapping.constructCborCountries()`, so I dont see the benefit here (codingwise) and I would maybe use a Consumer....Maybe we can discuss this :) 

Code after:
        logger.error("Digital green certificate valuesets were not written because of: ", e);
      }
    }

    getOnboardedCountriesArchive().ifPresent(dgcDirectory::addWritable);
    getRulesArchive(RuleType.Acceptance, ACCEPTANCE_RULES).ifPresent(dgcDirectory::addWritable);
    getRulesArchive(RuleType.Invalidation, INVALIDATION_RULES).ifPresent(dgcDirectory::addWritable);
    getDscsArchive().ifPresent(dgcDirectory::addWritable);

    return dgcDirectory;
  }

  /**
   * Create onboarded countries Archive. If any exception is thrown during fetching data and packaging process, an empty
   * Archive will be published in order to not override any previous archive on CDN with broken data.
   *
   * @return - Onboarded countries archive.
   */
  private Optional<Writable<WritableOnDisk>> getOnboardedCountriesArchive() {
    ArchiveOnDisk onboardedCountries = new ArchiveOnDisk(ONBOARDED_COUNTRIES);
    try {
      onboardedCountries
          .addWritable(new FileOnDisk("export.bin", dgcToCborMapping.constructCborCountries()));
      logger.info("Onboarded countries archive has been added to the DGC distribution folder");

      return Optional.of(new DistributionArchiveSigningDecorator(onboardedCountries, cryptoProvider,
          distributionServiceConfig));
    } catch (DigitalCovidCertificateException e) {
      logger.error("Onboarded countries archive was not overwritten because of:", e);
    } catch (FetchBusinessRulesException e) {
      logger.error("Onboarded countries archive was not overwritten because countries could not been fetched:", e);
    }

    return Optional.empty();
  }

  /**
   * Create DSCs archive.
   *
   * @return - DSCs archive
   */
  private Optional<Writable<WritableOnDisk>> getDscsArchive() {
    ArchiveOnDisk dscsArchive = new ArchiveOnDisk(DSCS);
    try {
      dscsArchive
          .addWritable(new FileOnDisk("export.bin",
              digitalSigningCertificatesToProtobufMapping.constructProtobufMapping().toByteArray()));
      logger.info("Onboarded countries archive has been added to the DGC distribution folder");
    } catch (UnableToLoadFileException | FetchDscTrustListException e) {
      logger.error("Onboarded countries archive was not overwritten because of:", e);
      return Optional.empty();