Skip to content

Commit

Permalink
MIR-1198 Support validation of PDF/A documents
Browse files Browse the repository at this point in the history
fixed changes from last commit
  • Loading branch information
Antoniafriedrich committed Oct 11, 2023
1 parent 2ec3cfe commit 2da050b
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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 @@ -56,10 +55,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, URISyntaxException {
String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8);
throws MalformedURLException, UnsupportedEncodingException {
String encodedQuery = URLEncoder.encode(query, "UTF-8");

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

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

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

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

return new MIROrcidPersonEntry(givenName.getTextTrim(), familyName.getTextTrim(), ORCID_URL + orcid);
} catch (JDOMException | IOException | URISyntaxException e) {
} catch (JDOMException | IOException e) {
throw new MCRException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
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 @@ -66,15 +64,17 @@ public Response retrieve(@PathParam("type") String type, @QueryParam("filter") S
urlStr+="&filter="+filter;
}
try {
URL url = new URI(urlStr).toURL();
try (InputStream is = url.openStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
is.transferTo(bos);
return Response.ok(bos.toByteArray()).build();
URL url = new URL(urlStr);
try(InputStream is = url.openStream()){
try(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 | URISyntaxException e) {
} catch (MalformedURLException 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,8 +2,6 @@

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 @@ -32,16 +30,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 URI("http://uri.gbv.de/document/" + database + ":ppn:" + ppn).toURL();
URL url = new URL("http://uri.gbv.de/document/" + database + ":ppn:" + ppn);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int resCode = connection.getResponseCode();
if (resCode == 200 || resCode == 302) {
return true;
}
} catch (IOException | URISyntaxException e) {
LogManager.getLogger().error("Exception while validating PPN.", e);
} catch (IOException e) {
LogManager.getLogger().error("IOException while validating PPN.", e);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ selfRegistration.error.mailExists = Die E-Mail-Adresse wird bereits verwendet.
pdf.errorbox.warning.message=Achtung! Bei einigen Ihrer PDF/A Dokumente sind Fehler aufgetreten. Bitte stellen Sie sicher, dass ihre PDF/A Dokumente mit aktuellen und geeigneten Programmen erstellt wurden und lesen Sie die Fehlerdokumentation. Anbei finden Sie eine Liste der aufgetretenen Fehler!
pdf.errorbox.warning.heading=Warnung!
pdf.errorbox.success.heading=Erfolg!
pdf.errorbox.success.message=Ihre PDF Dokumente wurden überprüft und es wurden keine Fehler gefunden.
pdf.errorbox.success.message=Ihre PDF Dokumente wurden \u00FCberpr\u00FCft und es wurden keine Fehler gefunden.
pdf.errorbox.button.info=Mehr Informationen
pdf.errorbox.button.download=Herunterladen
pdf.errorbox.conformity.level=Konformitätstufe
pdf.errorbox.conformity.level=Konformit\u00E4tstufe
pdf.errorbox.unknown.error=Keine weiteren Informationen!
182 changes: 182 additions & 0 deletions mir-module/src/main/resources/xsl/metadata/mir-pdf-errorbox.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i18n="xalan://org.mycore.services.i18n.MCRTranslation"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mcrxsl="xalan://org.mycore.common.xml.MCRXMLFunctions"
xmlns:xalan="http://xml.apache.org/xalan"
version="1.0" exclude-result-prefixes="i18n mcrxsl">

<xsl:variable name="foo" select="structure/derobjects/derobject/xlink:href/collection/dir/file"/>
<xsl:param name="WebApplicationBaseURL"/>
<xsl:param name="ServletsBaseURL" />
<xsl:param name="HttpSession"/>
<xsl:template match="mycoreobject" mode="displayPdfError">


<xsl:variable name="errorMessages">
<xsl:apply-templates
select="structure/derobjects/derobject"/>
</xsl:variable>




<xsl:choose>
<xsl:when test="contains($errorMessages,'Clause')">
<div class="container pdf-validation mb-3 px-0" id="accordion">
<div class="card-header bg-danger text-white">
<div class="list-group list-group-root well p-3">
<p class="h5">
<xsl:value-of select="i18n:translate('pdf.errorbox.warning.heading')"/>
</p>
<p>
<xsl:value-of select="i18n:translate('pdf.errorbox.warning.message')"/>
</p>
</div>
</div>
<div class="card-body border-left border-right border-bottom">
<xsl:copy-of select="$errorMessages"/>
</div>
</div>
</xsl:when>
<xsl:when test="string-length(normalize-space($errorMessages)) > 0 and not(contains($errorMessages,'Clause'))">
<div class="card-header bg-success text-white mb-3">
<div class="list-group list-group-root well p-3">
<p class="h5">
<xsl:value-of select="i18n:translate('pdf.errorbox.success.heading')"/>
</p>
<p>
<xsl:value-of select="i18n:translate('pdf.errorbox.success.message')"/>
</p>
</div>
</div>
</xsl:when>
</xsl:choose>
</xsl:template>


<xsl:template match="structure/derobjects/derobject">
<xsl:variable name="derivateID" select="@xlink:href"/>
<xsl:variable name="result" select="document(concat('pdfAValidator:', $derivateID))"/>
<xsl:if test="not(normalize-space($result))">
<xsl:apply-templates select="$result/derivate/file"/>
</xsl:if>
<xsl:variable name="derivbase" select="concat($ServletsBaseURL,'MCRFileNodeServlet/',$result/derivate/@id,'/')" />
<xsl:variable name="derivdir" select="concat($derivbase,$HttpSession)" />
</xsl:template>


<xsl:template match="file">
<xsl:variable name="derivate" select="../@id"/>


<xsl:variable name="name">
<xsl:call-template name="getFilename">
<xsl:with-param name="filePath" select="@name"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="badgecolor">
<xsl:choose>
<xsl:when test="failed">danger</xsl:when>
<xsl:otherwise>success</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="uniqueFileId" select="generate-id(.)"/>
<div id="{$derivate}{$uniqueFileId}" class="font-weight-bold d-flex list-group list-group-root">
<a onclick="$('#{$derivate}{$uniqueFileId}cbButton').toggleClass('fa-chevron-right fa-chevron-down');"
data-toggle="collapse" href="#collapse{$derivate}{$uniqueFileId}"
class="text-left d-flex flex-md-row flex-grow-1 list-group-item align-items-center">
<i id="{$derivate}{$uniqueFileId}cbButton" class="fa fa-chevron-right ml-auto mr-1"/>
<span class="flex-grow-1 font-weight-bold text-break">
<xsl:value-of select="$name"/>
</span>
<span class="badge badge-{$badgecolor} badge-pill align-self-center">
<xsl:value-of select="count(failed)"/>
</span>
</a>
</div>
<ul class="list-group collapse" id="collapse{$derivate}{$uniqueFileId}">
<li class="list-group-item d-flex flex-column flex-xl-row flex-grow-2 text-break">
<p class="flex-grow-1 col-lg-8 align-items-center">
<span class="text-muted pdf-term">
<xsl:value-of select="concat(i18n:translate('pdf.errorbox.conformity.level'),': ')"/>
</span>
<span class="pdf-value">
<xsl:value-of select="concat('PDF/A-',@flavour)"/>
</span>
</p>
<xsl:variable name="downloadLink">
<xsl:value-of select="concat($ServletsBaseURL,'MCRFileNodeServlet/',$derivate,'/',mcrxsl:encodeURIPath(@name))"/>
</xsl:variable>


<a class="btn btn-primary col" href="{$downloadLink}" target="_blank">
<xsl:value-of select="concat(i18n:translate('pdf.errorbox.button.download'),' ')"/>
<i class="fas fa-download"/>
</a>
</li>
<xsl:apply-templates select="failed"/>
</ul>
</xsl:template>


<xsl:template match="failed">
<li class="list-group-item d-flex flex-column flex-xl-row flex-grow-1 text-break">
<p class="flex-grow col-lg-8 col-md-9 align-self-center">
<span class="text-muted pdf-term">
Specification:
</span>
<span class="pdf-value">
<xsl:value-of select="concat(' ', @specification, ' ')"/>
</span>
<span class="text-muted pdf-term">
Clause:
</span>
<span class="pdf-value">
<xsl:value-of select="concat(' ', @clause, ' ')"/>
</span>
<span class="text-muted pdf-term">
Test:
</span>
<span class="pdf-value">
<xsl:value-of select="concat(' ', @testNumber, ' ')"/>
</span>
</p>
<xsl:choose>
<xsl:when test="not(@link)">
<a class="btn btn-info col" role="button" href="{@Link}" target="_blank">
<xsl:value-of select="concat(i18n:translate('pdf.errorbox.button.info'),' ')"/>
<i class="fas fa-external-link-alt"/>
</a>
</xsl:when>
<xsl:otherwise>
<div class="text-center alert alert-danger col" role="alert">
<xsl:value-of select="i18n:translate('pdf.errorbox.unknown.error')"/>
</div>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>


<xsl:template name="getFilename">
<xsl:param name="filePath" />
<xsl:variable name="rest-of" select="substring-after($filePath, '/')" />
<xsl:choose>
<xsl:when test="contains($rest-of, '/')">
<xsl:call-template name="getFilename">
<xsl:with-param name="filePath" select="$rest-of" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="normalize-space($rest-of)">
<xsl:value-of select="$rest-of" />
</xsl:if>
<xsl:if test="not(normalize-space($rest-of))">
<xsl:value-of select="$filePath" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package org.mycore.mir.wizard.command;

import java.io.File;
import java.net.URI;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void doExecute() {
Element library = getInputXML().getChild("database").getChild("library");

if (library != null && library.getChildren().size() > 0) {
String libDir = MCRConfigurationDir.getConfigurationDirectory().getAbsolutePath() + File.separator
String libDir = MCRConfigurationDir.getConfigurationDirectory().getAbsolutePath() + File.separator
+ "lib";

boolean success = true;
Expand All @@ -62,13 +62,12 @@ public void doExecute() {
String fname = FilenameUtils.getName(url);
File file = new File(libDir + File.separator + fname);
try {

FileUtils.copyURLToFile(new URI(url).toURL(), file);
FileUtils.copyURLToFile(new URL(url), file);
MCRConfigurationDirSetup.loadExternalLibs();

success = true;
} catch (Exception ex) {
LOGGER.error("Exception while downloading or loading database library: "
LOGGER.error("Exception while downloading or loading database library: "
+ file.getAbsolutePath(), ex);
success = false;
}
Expand Down

0 comments on commit 2da050b

Please sign in to comment.