Skip to content

Commit

Permalink
Small code improvements, and some logging added to better know when s…
Browse files Browse the repository at this point in the history
…uccessful rendering was completed (including Pdf Bytes Size).
  • Loading branch information
cajuncoding committed Aug 11, 2022
1 parent aea44e3 commit f8d07f8
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ApacheFopRenderResult {
private final ApacheFopEventListener eventListener;

public ApacheFopRenderResult(byte[] pdfBytes, ApacheFopEventListener eventListener) {
this.pdfBytes = pdfBytes;
this.pdfBytes = pdfBytes != null ? pdfBytes : new byte[0];
this.eventListener = eventListener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.apache.fop.apps.*;
import org.apache.fop.configuration.ConfigurationException;
import org.apache.fop.configuration.DefaultConfigurationBuilder;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
Expand Down Expand Up @@ -94,8 +92,6 @@ public ApacheFopRenderResult renderPdfResult(String xslFOSource, boolean gzipEna
}
}



protected synchronized void initApacheFopFactorySafely() {
if(staticFopFactory == null) {
var baseUri = new File(".").toURI();
Expand All @@ -104,7 +100,7 @@ protected synchronized void initApacheFopFactorySafely() {

try {
String configXmlText = ResourceUtils.loadResourceAsString(configFilePath);
if (!StringUtils.isBlank(configXmlText)) {
if (StringUtils.isNotBlank(configXmlText)) {

//When Debugging log the full Configuration file...
if(this.apacheFopConfig.isDebuggingEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected void readRequestQueryParamsConfig(Map<String, String> queryParams) {

//Determine if Event Log Dump mode is enabled (vs PDF Binary return).
this.eventLogDumpModeEnabled = BooleanUtils.toBoolean(
queryParams.getOrDefault(ApacheFopServerlessQueryParams.EventLogDump, null)
queryParams.getOrDefault(ApacheFopServerlessQueryParams.EventLogDump, null)
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public static String getCurrentW3cDateTime() {
return currentW3cDateTime;
}

public static boolean isNullOrWhiteSpace(String value) {
return value == null || value.isBlank();
}

/**
* Truncates a string to the number of characters that fit in X bytes avoiding multi byte characters being cut in
* half at the cut off point. Also handles surrogate pairs where 2 characters in the string is actually one literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ protected <TRequest> HttpResponseMessage ExecuteRequestInternal(
//Execute the transformation of the XSL-FO source content to Binary PDF format...
var pdfRenderResult = fopHelper.renderPdfResult(xslFOBodyContent, config.isGzipResponseEnabled());

//Add some contextual Logging so we can know if the PDF bytes were rendered...
logger.info(MessageFormat.format("[SUCCESS] Successfully Rendered PDF with [{0}] bytes.", pdfRenderResult.getPdfBytes().length));

//Render the PDF Response (or EventLog Dump if specified)...
var response = config.isEventLogDumpModeEnabled()
? responseBuilder.BuildEventLogDumpResponse(pdfRenderResult, config)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.cajuncoding.apachefop.serverless.web;

import com.cajuncoding.apachefop.serverless.http.HttpEncodings;
import com.cajuncoding.apachefop.serverless.utils.TextUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.UnsupportedEncodingException;
Expand All @@ -19,10 +20,10 @@ public SafeHeader(String value, String encoding) throws UnsupportedEncodingExcep
public String getValue() { return value; }

protected String sanitizeTextForHttpHeader(String value, String encoding) throws UnsupportedEncodingException {
if(TextUtils.isNullOrWhiteSpace(value))
if(StringUtils.isBlank(value))
return value;

if(encoding != null && encoding != HttpEncodings.IDENTITY_ENCODING)
if(StringUtils.isNotBlank(encoding) && !encoding.equalsIgnoreCase(HttpEncodings.IDENTITY_ENCODING))
return value;

//BBernard - 09/29/2021
Expand Down

0 comments on commit f8d07f8

Please sign in to comment.