Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exceptions #138

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading