Skip to content

Commit

Permalink
Merge branch 'DD4J-229' into 'master'
Browse files Browse the repository at this point in the history
DD4J-229

See merge request !71
  • Loading branch information
jentsoni committed Mar 1, 2018
2 parents 2cf495d + 3bd717a commit 7e801cd
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 147 deletions.
39 changes: 28 additions & 11 deletions src/main/java/org/digidoc4j/Configuration.java
Expand Up @@ -698,6 +698,20 @@ public void setValidationPolicy(String validationPolicy) {
this.setConfigurationParameter(ConfigurationParameter.ValidationPolicy, validationPolicy);
}

/**
* @return whether to print validation report
*/
public boolean getPrintValidationReport() {
return this.getConfigurationParameter(ConfigurationParameter.PrintValidationReport, Boolean.class);
}

/**
* @param printValidationReport whether to print validation report
*/
public void setPrintValidationReport(Boolean printValidationReport) {
this.setConfigurationParameter(ConfigurationParameter.PrintValidationReport, printValidationReport.toString());
}

/**
* Revocation and timestamp delta in minutes.
*
Expand Down Expand Up @@ -1099,6 +1113,7 @@ private void initDefaultValues() {
this.setConfigurationParameter(ConfigurationParameter.ValidationPolicy, Constant.Test.VALIDATION_POLICY);
this.setConfigurationParameter(ConfigurationParameter.OcspSource, Constant.Test.OCSP_SOURCE);
this.setConfigurationParameter(ConfigurationParameter.SignOcspRequests, "false");
this.setConfigurationParameter(ConfigurationParameter.PrintValidationReport, "true");
this.setJDigiDocParameter("SIGN_OCSP_REQUESTS", "false");
} else {
this.setConfigurationParameter(ConfigurationParameter.TspSource, Constant.Production.TSP_SOURCE);
Expand All @@ -1108,6 +1123,7 @@ private void initDefaultValues() {
this.setConfigurationParameter(ConfigurationParameter.ValidationPolicy, Constant.Production.VALIDATION_POLICY);
this.setConfigurationParameter(ConfigurationParameter.OcspSource, Constant.Production.OCSP_SOURCE);
this.setConfigurationParameter(ConfigurationParameter.SignOcspRequests, "false");
this.setConfigurationParameter(ConfigurationParameter.PrintValidationReport, "false");
this.trustedTerritories = Constant.Production.DEFAULT_TRUESTED_TERRITORIES;
this.setJDigiDocParameter("SIGN_OCSP_REQUESTS", "false");
}
Expand Down Expand Up @@ -1153,6 +1169,7 @@ private void loadInitialConfigurationValues() {
this.setConfigurationParameterFromFile("SIGNATURE_PROFILE", ConfigurationParameter.SignatureProfile);
this.setConfigurationParameterFromFile("SIGNATURE_DIGEST_ALGORITHM",
ConfigurationParameter.SignatureDigestAlgorithm);
this.setConfigurationParameterFromFile("PRINT_VALIDATION_REPORT", ConfigurationParameter.PrintValidationReport);
this.setJDigiDocConfigurationValue("SIGN_OCSP_REQUESTS", Boolean.toString(this.hasToBeOCSPRequestSigned()));
this.setJDigiDocConfigurationValue("DIGIDOC_PKCS12_CONTAINER", this.getOCSPAccessCertificateFileName());
this.initOcspAccessCertPasswordForJDigidoc();
Expand Down Expand Up @@ -1230,7 +1247,7 @@ private String defaultIfNull(String configParameter, String defaultValue) {

private boolean valueIsAllowed(String configParameter, String value) {
List<String> mustBeBooleans = Arrays.asList("SIGN_OCSP_REQUESTS", "KEY_USAGE_CHECK", "DATAFILE_HASHCODE_MODE",
"DIGIDOC_USE_LOCAL_TSL", "ALLOW_UNSAFE_INTEGER");
"DIGIDOC_USE_LOCAL_TSL", "ALLOW_UNSAFE_INTEGER", "PRINT_VALIDATION_REPORT");
List<String> mustBeIntegers = Arrays.asList("DIGIDOC_MAX_DATAFILE_CACHED", "HTTP_PROXY_PORT");
boolean errorFound = false;
if (mustBeBooleans.contains(configParameter)) {
Expand All @@ -1246,7 +1263,7 @@ private boolean isValidBooleanParameter(String configParameter, String value) {
if (!("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase()))) {
String errorMessage = "Configuration parameter " + configParameter + " should be set to true or false"
+ " but the actual value is: " + value + ".";
logError(errorMessage);
this.logError(errorMessage);
return false;
}
return true;
Expand All @@ -1259,13 +1276,13 @@ private boolean isValidIntegerParameter(String configParameter, String value) {
} catch (Exception e) {
String errorMessage = "Configuration parameter " + configParameter + " should have an integer value"
+ " but the actual value is: " + value + ".";
logError(errorMessage);
this.logError(errorMessage);
return false;
}
if (configParameter.equals("DIGIDOC_MAX_DATAFILE_CACHED") && parameterValue < -1) {
String errorMessage = "Configuration parameter " + configParameter + " should be greater or equal -1"
+ " but the actual value is: " + value + ".";
logError(errorMessage);
this.logError(errorMessage);
return false;
}
return true;
Expand All @@ -1278,7 +1295,7 @@ private void loadOCSPCertificates(LinkedHashMap digiDocCA, String caPrefix) {
if (ocsps == null) {
errorMessage = "No OCSPS entry found or OCSPS entry is empty. Configuration from: "
+ configurationInputSourceName;
logError(errorMessage);
this.logError(errorMessage);
return;
}
int numberOfOCSPCertificates = ocsps.size();
Expand All @@ -1291,12 +1308,12 @@ private void loadOCSPCertificates(LinkedHashMap digiDocCA, String caPrefix) {
if (!loadOCSPCertificateEntry(entry, ocsp, prefix)) {
errorMessage = "OCSPS list entry " + i + " does not have an entry for " + entry
+ " or the entry is empty\n";
logError(errorMessage);
this.logError(errorMessage);
}
}
if (!getOCSPCertificates(prefix, ocsp)) {
errorMessage = "OCSPS list entry " + i + " does not have an entry for CERTS or the entry is empty\n";
logError(errorMessage);
this.logError(errorMessage);
}
}
}
Expand Down Expand Up @@ -1348,12 +1365,11 @@ private Hashtable<String, String> mapToJDigiDocConfiguration() {
}

private void loadCertificateAuthoritiesAndCertificates() {
this.log.debug("");
@SuppressWarnings("unchecked")
ArrayList<LinkedHashMap> digiDocCAs = (ArrayList<LinkedHashMap>) configurationFromFile.get("DIGIDOC_CAS");
if (digiDocCAs == null) {
String errorMessage = "Empty or no DIGIDOC_CAS entry";
logError(errorMessage);
this.logError(errorMessage);
return;
}

Expand All @@ -1364,7 +1380,7 @@ private void loadCertificateAuthoritiesAndCertificates() {
LinkedHashMap digiDocCA = (LinkedHashMap) digiDocCAs.get(i).get("DIGIDOC_CA");
if (digiDocCA == null) {
String errorMessage = "Empty or no DIGIDOC_CA for entry " + (i + 1);
logError(errorMessage);
this.logError(errorMessage);
} else {
loadCertificateAuthorityCerts(digiDocCA, caPrefix);
loadOCSPCertificates(digiDocCA, caPrefix);
Expand All @@ -1378,7 +1394,6 @@ private void logError(String errorMessage) {
}

private void reportFileParseErrors() {
this.log.debug("");
if (inputSourceParseErrors.size() > 0) {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Configuration from ");
Expand Down Expand Up @@ -1494,6 +1509,8 @@ private <T> T getConfigurationParameter(ConfigurationParameter parameter, Class<
return (T) Integer.valueOf(value);
} else if (clazz.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(value);
} else if (clazz.isAssignableFrom(Boolean.class)) {
return (T) Boolean.valueOf(value);
}
throw new RuntimeException(String.format("Type <%s> not supported", clazz.getSimpleName()));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/digidoc4j/ConfigurationParameter.java
Expand Up @@ -38,6 +38,7 @@ public enum ConfigurationParameter {
TspCountryKeystorePath,
TspCountryKeystoreType,
TspCountryKeystorePassword,
AllowASN1UnsafeInteger
AllowASN1UnsafeInteger,
PrintValidationReport

}
12 changes: 3 additions & 9 deletions src/main/java/org/digidoc4j/ContainerOpener.java
Expand Up @@ -57,11 +57,8 @@ public static Container open(String path, Configuration configuration) throws Di
return new DDocOpener().open(path, configuration);
}
} catch (EOFException eof) {
String msg = "File is not valid.";
logger.error(msg);
throw new DigiDoc4JException(msg);
throw new DigiDoc4JException("File is invalid");
} catch (IOException e) {
logger.error(e.getMessage());
throw new DigiDoc4JException(e);
}
}
Expand Down Expand Up @@ -90,7 +87,6 @@ public static Container open(String path) throws DigiDoc4JException {
public static Container open(InputStream stream, boolean actAsBigFilesSupportEnabled) {
logger.debug("Opening container from stream");
BufferedInputStream bufferedInputStream = new BufferedInputStream(stream);

try {
if (Helper.isZipFile(bufferedInputStream)) {
if (Helper.isAsicSContainer(bufferedInputStream)){
Expand All @@ -103,7 +99,6 @@ public static Container open(InputStream stream, boolean actAsBigFilesSupportEna
return new DDocOpener().open(bufferedInputStream);
}
} catch (IOException e) {
logger.error(e.getMessage());
throw new DigiDoc4JException(e);
} finally {
IOUtils.closeQuietly(bufferedInputStream);
Expand All @@ -121,7 +116,6 @@ public static Container open(InputStream stream, boolean actAsBigFilesSupportEna
public static Container open(InputStream stream, Configuration configuration) {
logger.debug("Opening container from stream");
BufferedInputStream bufferedInputStream = new BufferedInputStream(stream);

try {
if (Helper.isZipFile(bufferedInputStream)) {
if (Helper.isAsicSContainer(bufferedInputStream)){
Expand All @@ -134,7 +128,6 @@ public static Container open(InputStream stream, Configuration configuration) {
return new DDocOpener().open(bufferedInputStream, configuration);
}
} catch (IOException e) {
logger.error(e.getMessage());
throw new DigiDoc4JException(e);
} finally {
IOUtils.closeQuietly(bufferedInputStream);
Expand All @@ -153,6 +146,7 @@ private static Container openBDocContainer(String path, Configuration configurat

private static Container openPadesContainer(String path, Configuration configuration) {
configuration.loadConfiguration("digidoc4j.yaml", false);
return new PadesContainer(path, configuration);
return new PadesContainer(configuration, path);
}

}
Expand Up @@ -62,7 +62,7 @@ public void saveXmlReports(Path directory) {
*/

protected String getNotSupportedMessage() {
return String.format("Not supported for <%s>", this.getValidationName());
return String.format("Not supported for <%s>", this.getResultName());
}

/*
Expand Down
52 changes: 29 additions & 23 deletions src/main/java/org/digidoc4j/impl/AbstractValidationResult.java
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.digidoc4j.Configuration;
import org.digidoc4j.ValidationResult;
import org.digidoc4j.exceptions.DigiDoc4JException;
import org.digidoc4j.utils.Helper;
Expand All @@ -29,30 +30,35 @@ public boolean hasWarnings() {
return CollectionUtils.isNotEmpty(this.warnings);
}

public void print() {
boolean hasWarningsOnly = CollectionUtils.isNotEmpty(this.warnings) && this.isValid();
if (hasWarningsOnly || CollectionUtils.isNotEmpty(this.errors)) {
if (hasWarningsOnly) {
Helper.printWarningSection(this.log, String.format("Start of <%s> validation result", this.getValidationName
()));
} else {
Helper.printErrorSection(this.log, String.format("Start of <%s> validation result", this.getValidationName()));
}
if (CollectionUtils.isNotEmpty(this.errors)) {
for (DigiDoc4JException error : this.errors) {
this.log.error(error.toString());
/**
* @param configuration configuration context
*/
public void print(Configuration configuration) {
if (configuration.getPrintValidationReport()) {
boolean hasWarningsOnly = CollectionUtils.isNotEmpty(this.warnings) && this.isValid();
if (hasWarningsOnly || CollectionUtils.isNotEmpty(this.errors)) {
if (hasWarningsOnly) {
Helper.printWarningSection(this.log, String.format("Start of <%s> validation result", this.getResultName
()));
} else {
Helper.printErrorSection(this.log, String.format("Start of <%s> validation result", this.getResultName()));
}
}
if (CollectionUtils.isNotEmpty(this.warnings)) {
for (DigiDoc4JException warning : this.warnings) {
this.log.warn(warning.toString());
if (CollectionUtils.isNotEmpty(this.errors)) {
for (DigiDoc4JException error : this.errors) {
this.log.error(error.toString());
}
}
if (CollectionUtils.isNotEmpty(this.warnings)) {
for (DigiDoc4JException warning : this.warnings) {
this.log.warn(warning.toString());
}
}
if (hasWarningsOnly) {
Helper.printWarningSection(this.log, String.format("End of <%s> validation result", this
.getResultName()));
} else {
Helper.printErrorSection(this.log, String.format("End of <%s> validation result", this.getResultName()));
}
}
if (hasWarningsOnly) {
Helper.printWarningSection(this.log, String.format("End of <%s> validation result", this
.getValidationName()));
} else {
Helper.printErrorSection(this.log, String.format("End of <%s> validation result", this.getValidationName()));
}
}
}
Expand All @@ -61,7 +67,7 @@ public void print() {
* RESTRICTED METHODS
*/

protected abstract String getValidationName();
protected abstract String getResultName();

/*
* ACCESSORS
Expand Down
Expand Up @@ -19,7 +19,7 @@ public SimpleValidationResult(String validationName) {
*/

@Override
protected String getValidationName() {
protected String getResultName() {
return this.validationName;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/digidoc4j/impl/asic/AsicContainer.java
Expand Up @@ -137,7 +137,7 @@ public ContainerValidationResult validate() {
this.validationResult = this.validateContainer();
}
if (this.validationResult instanceof AbstractValidationResult) {
((AbstractValidationResult) this.validationResult).print();
((AbstractValidationResult) this.validationResult).print(this.configuration);
}
return this.validationResult;
}
Expand Down
Expand Up @@ -98,7 +98,7 @@ public void generate(AsicValidationReportBuilder validationReportBuilder) {
*/

@Override
protected String getValidationName() {
protected String getResultName() {
return "ASiC container";
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ public List<DigiDoc4JException> getContainerErrors() {
*/

@Override
protected String getValidationName() {
protected String getResultName() {
return "Timestamp container";
}

Expand Down

0 comments on commit 7e801cd

Please sign in to comment.