diff --git a/Services/WebServices/RPC/lib/README.md b/Services/WebServices/RPC/lib/README.md index afc29b797e4f..1476e1444dab 100644 --- a/Services/WebServices/RPC/lib/README.md +++ b/Services/WebServices/RPC/lib/README.md @@ -34,6 +34,9 @@ On Debian-based systems try: ````shell > apt-get install php-curl php-xmlrpc openjdk-17-jdk-headless ```` +If you plan to use the RPC server for the generation of PDF documents, +please install `openjdk-17-jre` or `openjdk-17-jdk`. + Dependencies and the build process is managed via maven ```shell > apt-get install maven @@ -45,7 +48,7 @@ Dependencies and the build process is managed via maven ## Build the Java RPC Server ```shell -> cd Services/WebServervices/RPC/lib +> cd Services/WebServices/RPC/lib > mvn clean install ``` To build/compile the jar file for older LTS release than v17, start the maven build process with the following parameters: @@ -84,7 +87,7 @@ IliasIniPath = /var/www/html/ilias/ilias.ini.php ``` - IpAddress: normally localhost is sufficient -- Port: any free non pivileged port +- Port: any free non privileged port - IndexPath: any directory with read/write access for the webserver user - LogFile: Directory must exist. Read/write access for the webserver is required - LogLevel: one of INFO, DEBUG, WARN, ERROR, FATAL diff --git a/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/FO2PDF.java b/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/FO2PDF.java index 59354c6ed94f..0f10d8de4cc6 100644 --- a/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/FO2PDF.java +++ b/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/FO2PDF.java @@ -26,17 +26,21 @@ import org.apache.fop.apps.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.xml.sax.SAXException; import javax.xml.transform.*; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.List; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import org.xml.sax.SAXException; public class FO2PDF { @@ -51,12 +55,45 @@ public class FO2PDF { * Singleton constructor */ public FO2PDF() { + final String pathToConfigFile = "/de/ilias/config/fopConfig.xml"; + try { - fopFactory = FopFactory.newInstance(getClass().getResource("/de/ilias/config/fopConfig.xml").toURI()); - } catch (URISyntaxException | NullPointerException ex) { - logger.error("Cannot load fop configuration:" + ex); - } + logger.info("Trying to read fopConfig from: {}", pathToConfigFile); + + logger.info("Extract resource as temporary file .."); + + File jarFile = new File(FO2PDF.class + .getProtectionDomain() + .getCodeSource() + .getLocation() + .toURI()); + File jarDir = jarFile.getParentFile(); + + if (jarDir == null || !jarDir.isDirectory()) { + throw new IllegalStateException("Cannot determine the directory of the running JAR file."); + } + logger.info("Determined JAR directory path: {}", jarDir.toPath()); + + File tempConfigFile = new File(jarDir, "fopConfig.xml"); + + if (!tempConfigFile.exists()) { + try (InputStream inputStream = FO2PDF.class.getResourceAsStream(pathToConfigFile)) { + if (inputStream == null) { + throw new IllegalStateException("Resource stream is null"); + } + + Files.copy(inputStream, tempConfigFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + } + } + + Path configFilePath = tempConfigFile.toPath(); + + logger.info("fopConfig Path: {}", configFilePath.toUri()); + fopFactory = FopFactory.newInstance(new File(configFilePath.toUri())); + } catch (SAXException | IOException | URISyntaxException | NullPointerException ex) { + logger.error("Cannot load fop configuration: {}", pathToConfigFile, ex); + } } /** @@ -104,20 +141,22 @@ public void transform() List pageSequences = foResults.getPageSequences(); for (Object pageSequence : pageSequences) { PageSequenceResults pageSequenceResults = (PageSequenceResults) pageSequence; - logger.debug("PageSequence " - + (String.valueOf(pageSequenceResults.getID()).length() > 0 - ? pageSequenceResults.getID() : "") - + " generated " + pageSequenceResults.getPageCount() + " pages."); + logger.debug( + "PageSequence {} generated {} pages.", + !String.valueOf(pageSequenceResults.getID()).isEmpty() + ? pageSequenceResults.getID() + : "", pageSequenceResults.getPageCount() + ); } - logger.info("Generated " + foResults.getPageCount() + " pages in total."); + logger.info("Generated {} pages in total.", foResults.getPageCount()); this.setPdf(out.toByteArray()); } catch (TransformerConfigurationException e) { - logger.warn("Configuration exception: " + e); + logger.warn("Configuration exception: {}", String.valueOf(e)); throw new TransformationException(e); } catch (TransformerException e) { - logger.warn("Transformer exception: " + e); + logger.warn("Transformer exception: {}", String.valueOf(e)); throw new TransformationException(e); } catch (FOPException e) { throw new TransformationException(e); diff --git a/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/RPCTransformationHandler.java b/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/RPCTransformationHandler.java index 8ffc7ec1d300..d2866e02f3af 100644 --- a/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/RPCTransformationHandler.java +++ b/Services/WebServices/RPC/lib/src/main/java/de/ilias/services/transformation/RPCTransformationHandler.java @@ -41,23 +41,24 @@ public boolean ping() { return true; } - public byte[] ilFO2PDF(String foString) { - - FO2PDF fo = null; - - try { - - fo = new FO2PDF(); - fo.clearCache(); - fo.setFoString(foString); - fo.transform(); - - return fo.getPdf(); - } - catch (TransformationException e) { - + public byte[] ilFO2PDF(String foString) { + + FO2PDF fo = null; + + try { + + fo = new FO2PDF(); + fo.clearCache(); + fo.setFoString(foString); + logger.info("FO String:" + foString); + + fo.transform(); + + return fo.getPdf(); + } catch (TransformationException e) { logger.warn("Transformation failed:" + e); } - return null; - } + + return null; + } } diff --git a/Services/WebServices/RPC/lib/src/main/resources/de/ilias/config/fopConfig.xml b/Services/WebServices/RPC/lib/src/main/resources/de/ilias/config/fopConfig.xml index c3afc773a4af..c906845f231a 100644 --- a/Services/WebServices/RPC/lib/src/main/resources/de/ilias/config/fopConfig.xml +++ b/Services/WebServices/RPC/lib/src/main/resources/de/ilias/config/fopConfig.xml @@ -2,6 +2,7 @@ true false + .