Skip to content

Commit

Permalink
Add exception to url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
EkaterinaKomar committed Sep 29, 2023
1 parent 5ef04e2 commit 66fbade
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/main/java/org/verapdf/rest/resources/ValidateResource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
*
*
*/
package org.verapdf.rest.resources;

Expand Down Expand Up @@ -100,9 +100,8 @@ public static InputStream validateXml(@PathParam("profileId") String profileId,
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML })
public static InputStream validateXml(@PathParam("profileId") String profileId,
@FormDataParam("url") String urlLink)
throws VeraPDFException, IOException {
InputStream uploadedInputStream = new URL(urlLink).openStream();
@FormDataParam("url") String urlLink) throws VeraPDFException {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, profileId, FormatOption.XML);
}
Expand Down Expand Up @@ -141,9 +140,8 @@ public static InputStream validateJson(@PathParam("profileId") String profileId,
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
public static InputStream validateJson(@PathParam("profileId") String profileId,
@FormDataParam("url") String urlLink)
throws VeraPDFException, IOException {
InputStream uploadedInputStream = new URL(urlLink).openStream();
@FormDataParam("url") String urlLink) throws VeraPDFException {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, profileId, FormatOption.JSON);
}
Expand Down Expand Up @@ -180,9 +178,8 @@ public static InputStream validateHtml(@PathParam("profileId") String profileId,
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.TEXT_HTML })
public static InputStream validateHtml(@PathParam("profileId") String profileId,
@FormDataParam("url") String urlLink)
throws VeraPDFException, IOException {
InputStream uploadedInputStream = new URL(urlLink).openStream();
@FormDataParam("url") String urlLink) throws VeraPDFException {
InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink);

return validate(uploadedInputStream, profileId, FormatOption.HTML);
}
Expand Down Expand Up @@ -286,6 +283,18 @@ private static MessageDigest getDigest() {
}
}

private static InputStream getInputStreamByUrlLink(String urlLink) throws VeraPDFException {
try {
return new URL(urlLink).openStream();
} catch (IOException e) {
if (urlLink.isEmpty()) {
throw new VeraPDFException("URL is empty");
} else {
throw new VeraPDFException("URL is incorrect: " + urlLink);
}
}
}

private static ProcessorConfig createProcessorConfig(ValidatorConfig validatorConfig) {
VeraAppConfig veraAppConfig = configManager.getApplicationConfig();
return ProcessorFactory.fromValues(validatorConfig, configManager.getFeaturesConfig(),
Expand Down

0 comments on commit 66fbade

Please sign in to comment.