Skip to content

Commit

Permalink
MIR-1198 Support validation of PDF/A documents
Browse files Browse the repository at this point in the history
removed unnecessary diffs
  • Loading branch information
Antoniafriedrich committed Oct 10, 2023
1 parent 49a2d14 commit 2ec3cfe
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 1,904 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.mycore.mir.orcid;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -55,10 +56,10 @@ public class MIROrcidServlet extends MCRServlet {
private static final String SEARCH_ORCID_XPATH = "/search:search/search:result/common:orcid-identifier/common:path";

private static List<MIROrcidPersonEntry> getPersonList(String query)
throws MalformedURLException, UnsupportedEncodingException {
String encodedQuery = URLEncoder.encode(query, "UTF-8");
throws MalformedURLException, URISyntaxException {
String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8);

URL url = new URL(ORCID_REQUEST_BASE + "search/?q=" + encodedQuery + "&rows=" + ORCID_FETCH_SIZE * 2);
URL url = new URI(ORCID_REQUEST_BASE + "search/?q=" + encodedQuery + "&rows=" + ORCID_FETCH_SIZE * 2).toURL();

try {
Document document = new SAXBuilder().build(url);
Expand Down Expand Up @@ -88,7 +89,7 @@ private static List<String> getOrcidsFromDocument(Document document) {

private static MIROrcidPersonEntry getEntry(String orcid) {
try {
URL url = new URL(ORCID_REQUEST_BASE + orcid + "/personal-details");
URL url = new URI(ORCID_REQUEST_BASE + orcid + "/personal-details").toURL();
Document document = new SAXBuilder().build(url);
Element element = document.getRootElement();

Expand All @@ -105,7 +106,7 @@ private static MIROrcidPersonEntry getEntry(String orcid) {
}

return new MIROrcidPersonEntry(givenName.getTextTrim(), familyName.getTextTrim(), ORCID_URL + orcid);
} catch (JDOMException | IOException e) {
} catch (JDOMException | IOException | URISyntaxException e) {
throw new MCRException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

Expand Down Expand Up @@ -64,17 +66,15 @@ public Response retrieve(@PathParam("type") String type, @QueryParam("filter") S
urlStr+="&filter="+filter;
}
try {
URL url = new URL(urlStr);
try(InputStream is = url.openStream()){
try(ByteArrayOutputStream bos = new ByteArrayOutputStream()){
is.transferTo(bos);
return Response.ok(bos.toByteArray()).build();
}
URL url = new URI(urlStr).toURL();
try (InputStream is = url.openStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
is.transferTo(bos);
return Response.ok(bos.toByteArray()).build();
} catch (IOException e) {
LOGGER.error("Error while performing request!", e);
return Response.serverError().build();
}
} catch (MalformedURLException e) {
} catch (MalformedURLException | URISyntaxException e) {
LOGGER.error("Error while building URL " + urlStr, e);
return Response.serverError().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -30,16 +32,16 @@ public static String validateSDNB(String sdnb) {
public static boolean validatePPN(String ppn) {
String database = MCRConfiguration2.getString("MIR.PPN.DatabaseList").orElse("gvk");
try {
URL url = new URL("http://uri.gbv.de/document/" + database + ":ppn:" + ppn);
URL url = new URI("http://uri.gbv.de/document/" + database + ":ppn:" + ppn).toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int resCode = connection.getResponseCode();
if (resCode == 200 || resCode == 302) {
return true;
}
} catch (IOException e) {
LogManager.getLogger().error("IOException while validating PPN.", e);
} catch (IOException | URISyntaxException e) {
LogManager.getLogger().error("Exception while validating PPN.", e);
}
return false;
}
Expand Down
Loading

0 comments on commit 2ec3cfe

Please sign in to comment.