Skip to content

Commit

Permalink
Merge pull request #138 from veraPDF/fix/exceptions
Browse files Browse the repository at this point in the history
Add exceptions
  • Loading branch information
EkaterinaKomar authored Nov 10, 2023
2 parents fd9cfac + ad5df6f commit aaa5100
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/main/java/org/verapdf/rest/resources/ValidateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static InputStream validateXml(@Parameter(description = "the String id of
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.XML);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, null, FormatOption.XML);
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public static InputStream validateXml(@Parameter(description = "the String id of
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, sha1Hex, FormatOption.XML);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, sha1Hex, FormatOption.XML);
}

@POST
Expand All @@ -175,9 +175,7 @@ public static InputStream validateXml(@Parameter(description = "the String id of
@PathParam("profileId") String profileId,
@Parameter(description = "a URL of PDF to be validated")
@FormDataParam("url") String urlLink) {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, urlLink, profileId, null, FormatOption.XML);
return validateUrl(urlLink, profileId, FormatOption.XML);
}

/**
Expand All @@ -203,7 +201,7 @@ public static InputStream validateJson(@Parameter(description = "the String id o
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.JSON);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, null, FormatOption.JSON);
}

/**
Expand Down Expand Up @@ -234,7 +232,7 @@ public static InputStream validateJson(@Parameter(description = "the String id o
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, sha1Hex, FormatOption.JSON);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, sha1Hex, FormatOption.JSON);
}

@POST
Expand All @@ -246,9 +244,7 @@ public static InputStream validateJson(@Parameter(description = "the String id o
@PathParam("profileId") String profileId,
@Parameter(description = "a URL of PDF to be validated")
@FormDataParam("url") String urlLink) {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, urlLink, profileId, null, FormatOption.JSON);
return validateUrl(urlLink, profileId, FormatOption.JSON);
}

/**
Expand All @@ -271,7 +267,7 @@ public static InputStream validateHtml(@PathParam("profileId") String profileId,
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.HTML);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, null, FormatOption.HTML);
}

/**
Expand Down Expand Up @@ -299,7 +295,7 @@ public static InputStream validateHtml(@PathParam("profileId") String profileId,
style = ParameterStyle.FORM, description = "an InputStream of the PDF to be validated")
@FormDataParam("file") InputStream uploadedInputStream,
@Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, sha1Hex, FormatOption.HTML);
return validateFile(uploadedInputStream, contentDispositionHeader, profileId, sha1Hex, FormatOption.HTML);
}

@POST
Expand All @@ -311,17 +307,35 @@ public static InputStream validateHtml(@Parameter(description = "the String id o
@PathParam("profileId") String profileId,
@Parameter(description = "a URL of PDF to be validated")
@FormDataParam("url") String urlLink) {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, urlLink, profileId, null, FormatOption.HTML);
return validateUrl(urlLink, profileId, FormatOption.HTML);
}

public static void setMaxFileSize(Integer maxFileSize) {
ValidateResource.maxFileSize = maxFileSize;
}

private static InputStream validateFile(InputStream uploadedInputStream,
FormDataContentDisposition contentDispositionHeader, String profileId,
String sha1Hex, FormatOption formatOption) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, sha1Hex, formatOption);
}

private static InputStream validateUrl(String urlLink, String profileId, FormatOption formatOption) {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, urlLink, profileId, null, formatOption);
}

private static InputStream validate(InputStream uploadedInputStream, String fileName, String profileId,
String sha1Hex, FormatOption formatOption) {
if (fileName == null) {
throw new BadRequestException("File name is empty");
}

SeekableInputStream seekableInputStream = createInputStream(uploadedInputStream, sha1Hex);
PDFAFlavour flavour = PDFAFlavour.byFlavourId(profileId);
ValidatorConfig validatorConfig = configManager.getValidatorConfig();
Expand Down

0 comments on commit aaa5100

Please sign in to comment.