Skip to content

Commit

Permalink
Add exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
EkaterinaKomar committed Nov 3, 2023
1 parent 5e00fcc commit bb4c790
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/verapdf/rest/resources/ValidateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.XML);
}

Expand Down Expand Up @@ -153,6 +157,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

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

Expand Down Expand Up @@ -203,6 +211,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.JSON);
}

Expand Down Expand Up @@ -234,6 +246,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

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

Expand Down Expand Up @@ -271,6 +287,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, null, FormatOption.HTML);
}

Expand Down Expand Up @@ -299,6 +319,10 @@ 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) {
if (contentDispositionHeader == null) {
throw new BadRequestException("File is empty");
}

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

Expand All @@ -322,6 +346,10 @@ public static void setMaxFileSize(Integer maxFileSize) {

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 bb4c790

Please sign in to comment.