Skip to content

Commit

Permalink
Merge pull request #232 from axonivy-market/standardLogging
Browse files Browse the repository at this point in the history
Standard logging
  • Loading branch information
ivy-rew authored Oct 31, 2023
2 parents 50a9709 + dea68cc commit 9fff446
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
uses: './.github/workflows/ci.yml'
secrets: inherit
with:
mvnArgs: '"-Divy.engine.download.url=https://dev.axonivy.com/permalink/dev/axonivy-engine.zip" "-Divy.engine.version=11.2.0" "-Dproject-build-plugin-version=11.2.0-SNAPSHOT" -Dmaven.deploy.skip=true'
mvnArgs: '"-Divy.engine.download.url=https://dev.axonivy.com/permalink/dev/axonivy-engine.zip" "-Divy.engine.version=11.2.0" "-Dproject-build-plugin-version=11.2.0-SNAPSHOT" "-Dweb-tester.version=11.2.0-SNAPSHOT" -Dmaven.deploy.skip=true'

39 changes: 26 additions & 13 deletions doc-factory-demos-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<properties>
<project-build-plugin-version>10.0.14</project-build-plugin-version>
<web-tester.version>10.0.14</web-tester.version>
</properties>

<dependencies>
Expand All @@ -18,10 +19,10 @@
<type>iar</type>
</dependency>
<dependency>
<groupId>com.axonivy.ivy.webtest</groupId>
<artifactId>web-tester</artifactId>
<version>10.0.0</version>
<scope>test</scope>
<groupId>com.axonivy.ivy.webtest</groupId>
<artifactId>web-tester</artifactId>
<version>${web-tester.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
Expand Down Expand Up @@ -62,14 +63,26 @@

<pluginRepositories>
<pluginRepository>
<id>central.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
<id>central.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</pluginRepositories>
<repositories>
<repository>
<id>central.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.axonivy.ivy.webtest.IvyWebTest;
import com.axonivy.ivy.webtest.engine.EngineUrl;
import com.axonivy.ivy.webtest.engine.WebAppFixture;
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.FileDownloadMode;
import com.codeborne.selenide.Selenide;
Expand All @@ -41,8 +42,8 @@ void docWithCompositeObjPdf() throws Exception {
@Test
void docWithCompositeObjPdfA() throws Exception {
assertDownload("start8.ivp", "DocWithCompositeObjectA.pdf");
}
}

@Test
void docWithNestedTablesPDF() throws Exception {
assertDownload("start3.ivp", "DocWithFullNestedTables.pdf");
Expand All @@ -67,18 +68,17 @@ void zipMultipleDocuments() throws FileNotFoundException {
}

@Test
void ivyDocApi() {
void ivyDocApi(WebAppFixture fixture) {
if (!EngineUrl.isDesigner()) {
open(EngineUrl.create().path("login").toUrl());
$(By.id("loginForm:userName")).shouldBe(visible).sendKeys("test");
$(By.id("loginForm:password")).shouldBe(visible).sendKeys("test");
$(By.id("loginForm:login")).click();
$(By.id("sessionUserName")).shouldHave(exactText("test"));
fixture.login("test", "test");
}
fixture.config("StandardProcess.DefaultPages", "ch.ivyteam.workflow:dev-workflow-ui");

open(EngineUrl.createProcessUrl("/DocFactoryDemos/16DFD8AB2E4BFFF9/start2.ivp"));
$(By.id("form:name")).shouldBe(visible).sendKeys("Batman");
$("button").click();
$(withText("Task End")).shouldBe(visible);

open(EngineUrl.create().path("tasks").toUrl());
$(By.linkText("Task: View attached document")).shouldBe(visible).click();
Selenide.switchTo().frame("iFrame");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"type" : "Script",
"name" : "Create document",
"config" : {
"security" : "system",
"output" : {
"code" : "in.service.createHelloWord();"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.jupiter.api.Test;

import ch.ivyteam.ivy.addons.docfactory.exception.DocFactoryException;
import ch.ivyteam.ivy.addons.docfactory.log.DocFactoryLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.pdf.PdfAType;
import ch.ivyteam.ivy.addons.docfactory.pdfbox.PdfAValidator;

Expand Down Expand Up @@ -48,7 +47,6 @@ public static void setupClass() throws URISyntaxException {
@BeforeEach
public void setup() throws Exception {
asposePdfFactory = new AsposePdfFactory(false);
asposePdfFactory.setLogDirectoryRetriever(new LogDirectoryRetriever());
}

@Test
Expand Down Expand Up @@ -209,16 +207,4 @@ private java.io.File makePDFToConvert(String pdfToConvertPath) throws IOExceptio
return pdfToConvert;
}

private class LogDirectoryRetriever implements DocFactoryLogDirectoryRetriever {

@Override
public java.io.File getLogDirectory() {
java.io.File logDir = new java.io.File("test/logs");
if (!logDir.isDirectory()) {
logDir.mkdir();
}
return new java.io.File("test/logs");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ch.ivyteam.ivy.addons.docfactory.aspose;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.aspose.pdf.ConvertErrorAction;
Expand All @@ -15,20 +15,15 @@
import ch.ivyteam.api.API;
import ch.ivyteam.ivy.addons.docfactory.PdfFactory;
import ch.ivyteam.ivy.addons.docfactory.exception.DocFactoryException;
import ch.ivyteam.ivy.addons.docfactory.log.DocFactoryLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.log.IvyLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.pdf.PdfAType;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.File;
import ch.ivyteam.log.Logger;

public class AsposePdfFactory extends PdfFactory {

private static final long PDF_FACTORY_LOG_MAX_BYTE_SIZE = 5 * 1024;
private static final Logger LOGGER = Logger.getLogger(AsposePdfFactory.class);
private static final String PDF_EXTENSION = ".pdf";
private static final String PDF_FACTORY_LOG_FILENAME = "pdfFactory.log";
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";

private DocFactoryLogDirectoryRetriever logDirectoryRetriever;

public AsposePdfFactory() {
init();
Expand All @@ -53,8 +48,7 @@ private void init() {
}

@Override
public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppend)
throws DocFactoryException {
public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppend) throws DocFactoryException {
API.checkNotEmpty(resultFilePath, "resultFileName");
API.checkNotNull(pdfsToAppend, "pdfsToAppend");
API.checkNotEmpty(pdfsToAppend, "pdfsToAppend");
Expand All @@ -63,8 +57,7 @@ public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppen
Document pdfDocument = new Document(new FileInputStream(pdfsToAppend.get(0)));
return appendFilesToDocument(pdfDocument, pdfsToAppend.subList(1, pdfsToAppend.size()), resultFilePath);
} catch (Exception ex) {
throw new DocFactoryException("An error occurred while generating the pdf file. " + ex.getMessage(),
ex);
throw new DocFactoryException("An error occurred while generating the pdf file. " + ex.getMessage(), ex);
}
}

Expand All @@ -74,37 +67,24 @@ public void convertToPdfA(String filePath, PdfAType pdfAType)
if (StringUtils.isBlank(filePath) || !filePath.toLowerCase().endsWith(PDF_EXTENSION)) {
throw new IllegalArgumentException("The filePath parameter must point to a PDF file.");
}
com.aspose.pdf.Document pdfDocument = null;
try (InputStream in = new FileInputStream(filePath)) {
pdfDocument = new com.aspose.pdf.Document(in);
pdfDocument.convert(getAsposePdfFactoryLogFile().getAbsolutePath(), getAsposePDFAType(pdfAType),
ConvertErrorAction.Delete);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (InputStream in = new FileInputStream(filePath);
var pdfDocument = new com.aspose.pdf.Document(in)) {
pdfDocument.convert(bos, getAsposePDFAType(pdfAType), ConvertErrorAction.Delete);
pdfDocument.setLinearized(true);
pdfDocument.save(filePath);
} catch (Exception ex) {
throw new DocFactoryException(ex);
} finally {
if (pdfDocument != null) {
pdfDocument.close();
var logs = new String(bos.toByteArray());
if (!logs.isBlank()) {
logs.lines().forEachOrdered(logLine -> {
LOGGER.error(logLine);
});
}
}
}

public DocFactoryLogDirectoryRetriever getLogDirectoryRetriever() {
if (this.logDirectoryRetriever == null) {
this.setLogDirectoryRetriever(new IvyLogDirectoryRetriever());
}
return logDirectoryRetriever;
}

/**
* visible for unit tests
* @param logDirectoryRetriever
*/
protected void setLogDirectoryRetriever(DocFactoryLogDirectoryRetriever logDirectoryRetriever) {
this.logDirectoryRetriever = logDirectoryRetriever;
}

private PdfFormat getAsposePDFAType(PdfAType pdfAType) {
switch (pdfAType) {
case PDF_A_1A:
Expand All @@ -128,23 +108,6 @@ private PdfFormat getAsposePDFAType(PdfAType pdfAType) {
}
}

private java.io.File getAsposePdfFactoryLogFile() throws IOException {
java.io.File logDir = this.getLogDirectoryRetriever().getLogDirectory();
if (logDir == null) {
logDir = new java.io.File(System.getProperty(JAVA_IO_TMPDIR));
}
java.io.File logFile = new java.io.File(logDir, PDF_FACTORY_LOG_FILENAME);
if (!logFile.exists()) {
logFile.createNewFile();
} else if (FileUtils.sizeOf(logFile) > PDF_FACTORY_LOG_MAX_BYTE_SIZE) {
FileUtils.moveFile(logFile,
new java.io.File(logFile.getParentFile(), logFile.getName() + System.nanoTime()));
logFile.createNewFile();
}

return logFile;
}

/**
* Only visible for testing
*/
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 9fff446

Please sign in to comment.