From a1b18c4a223eec7b3a159696957be449485f30da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:13:59 +0900 Subject: [PATCH 1/3] Encode with UTF-8 if getString has funny characters --- .../com/genexus/internet/HttpClientJavaLib.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java index 875cbb51f..c476d9adb 100644 --- a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java +++ b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java @@ -724,23 +724,16 @@ public String getString() { return ""; try { this.setEntity(); - ContentType contentType = ContentType.getOrDefault(entity); - Charset charset; - if (contentType.equals(ContentType.DEFAULT_TEXT)) { - charset = StandardCharsets.UTF_8; - } else { - charset = contentType.getCharset(); - if (charset == null) { - charset = StandardCharsets.UTF_8; - } - } + Charset charset = ContentType.getOrDefault(entity).getCharset(); String res = EntityUtils.toString(entity, charset); + if (res.matches(".*[Ã-ÿ].*")) { + res = EntityUtils.toString(entity, StandardCharsets.UTF_8); + } eof = true; return res; } catch (IOException e) { setExceptionsCatch(e); - } catch (IllegalArgumentException e) { - } + } catch (IllegalArgumentException e) {} return ""; } From e242950a90dd1e73de60c9adba29325f5ea84035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Sat, 30 Nov 2024 21:39:57 +0700 Subject: [PATCH 2/3] Fix streams concurrency issues Issue:202643 --- .../com/genexus/reports/PDFReportPDFBox.java | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java b/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java index 0522cae00..779fa7ac5 100644 --- a/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java +++ b/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java @@ -28,7 +28,6 @@ import org.apache.pdfbox.pdmodel.*; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.*; -import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode; @@ -61,6 +60,8 @@ public class PDFReportPDFBox extends GXReportPDFCommons{ ConcurrentHashMap documentImages; public int runDirection = 0; private int page; + private PDPageContentStream auxContentStream; + private boolean useAuxContentStream; private final float DEFAULT_PDFBOX_LEADING = 1.2f; @@ -228,41 +229,41 @@ private void roundRectangle(PDPageContentStream cb, float x, float y, float w, f public void GxDrawRect(int left, int top, int right, int bottom, int pen, int foreRed, int foreGreen, int foreBlue, int backMode, int backRed, int backGreen, int backBlue, int styleTop, int styleBottom, int styleRight, int styleLeft, int cornerRadioTL, int cornerRadioTR, int cornerRadioBL, int cornerRadioBR) { - try (PDPageContentStream cb = getNewPDPageContentStream()){ - - float penAux = (float)convertScale(pen); - float rightAux = (float)convertScale(right); - float bottomAux = (float)convertScale(bottom); - float leftAux = (float)convertScale(left); - float topAux = (float)convertScale(top); + PDPageContentStream cb = null; + try{ + cb = useAuxContentStream ? auxContentStream : getNewPDPageContentStream(); + float penAux = (float) convertScale(pen); + float rightAux = (float) convertScale(right); + float bottomAux = (float) convertScale(bottom); + float leftAux = (float) convertScale(left); + float topAux = (float) convertScale(top); cb.saveGraphicsState(); float x1, y1, x2, y2; x1 = leftAux + leftMargin; - y1 = pageSize.getUpperRightY() - bottomAux - topMargin -bottomMargin; + y1 = pageSize.getUpperRightY() - bottomAux - topMargin - bottomMargin; x2 = rightAux + leftMargin; - y2 = pageSize.getUpperRightY() - topAux - topMargin -bottomMargin; + y2 = pageSize.getUpperRightY() - topAux - topMargin - bottomMargin; cb.setLineWidth(penAux); cb.setLineCapStyle(2); - if (cornerRadioBL==0 && cornerRadioBR==0 && cornerRadioTL==0 && cornerRadioTR==0 && styleBottom==0 && styleLeft==0 && styleRight==0 && styleTop==0) { + if (cornerRadioBL == 0 && cornerRadioBR == 0 && cornerRadioTL == 0 && cornerRadioTR == 0 && styleBottom == 0 && styleLeft == 0 && styleRight == 0 && styleTop == 0) { if (pen > 0) cb.setStrokingColor(foreRed, foreGreen, foreBlue); else - cb.setStrokingColor (backRed, backGreen, backBlue); + cb.setStrokingColor(backRed, backGreen, backBlue); cb.addRect(x1, y1, x2 - x1, y2 - y1); - if (backMode!=0) { + if (backMode != 0) { cb.setNonStrokingColor(new Color(backRed, backGreen, backBlue)); cb.fillAndStroke(); } cb.closePath(); cb.stroke(); - } - else { + } else { float w = x2 - x1; float h = y2 - y1; if (w < 0) { @@ -274,18 +275,18 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo h = -h; } - float cRadioTL = (float)convertScale(cornerRadioTL); - float cRadioTR = (float)convertScale(cornerRadioTR); - float cRadioBL = (float)convertScale(cornerRadioBL); - float cRadioBR = (float)convertScale(cornerRadioBR); + float cRadioTL = (float) convertScale(cornerRadioTL); + float cRadioTR = (float) convertScale(cornerRadioTR); + float cRadioBL = (float) convertScale(cornerRadioBL); + float cRadioBR = (float) convertScale(cornerRadioBR); - int max = (int)Math.min(w, h); - cRadioTL = Math.max(0, Math.min(cRadioTL, max/2)); - cRadioTR = Math.max(0, Math.min(cRadioTR, max/2)); - cRadioBL = Math.max(0, Math.min(cRadioBL, max/2)); - cRadioBR = Math.max(0, Math.min(cRadioBR, max/2)); + int max = (int) Math.min(w, h); + cRadioTL = Math.max(0, Math.min(cRadioTL, max / 2)); + cRadioTR = Math.max(0, Math.min(cRadioTR, max / 2)); + cRadioBL = Math.max(0, Math.min(cRadioBL, max / 2)); + cRadioBR = Math.max(0, Math.min(cRadioBR, max / 2)); - if (backMode!=0) { + if (backMode != 0) { cb.setStrokingColor(backRed, backGreen, backBlue); cb.setLineWidth(0); roundRectangle(cb, x1, y1, w, h, @@ -308,6 +309,15 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo log.debug("GxDrawRect -> (" + left + "," + top + ") - (" + right + "," + bottom + ") BackMode: " + backMode + " Pen:" + pen); } catch (Exception e) { log.error("GxDrawRect failed: ", e); + } finally { + try { + if (cb != null && !useAuxContentStream) + cb.close(); + else if (useAuxContentStream) + useAuxContentStream = false; + } catch (IOException ioe) { + log.error("Failed to close content stream", ioe); + } } } @@ -574,6 +584,7 @@ public void setAsianFont(String fontName, String style) { log.error("setAsianFont failed: ", e); } } + public void GxDrawText(String sTxt, int left, int top, int right, int bottom, int align, int htmlformat, int border, int valign) { PDPageContentStream cb = null; try { @@ -583,6 +594,8 @@ public void GxDrawText(String sTxt, int left, int top, int right, int bottom, in printRectangle = true; if (printRectangle && (border == 1 || backFill)) { + auxContentStream = cb; + useAuxContentStream = true; GxDrawRect(left, top, right, bottom, border, foreColor.getRed(), foreColor.getGreen(), foreColor.getBlue(), backFill ? 1 : 0, backColor.getRed(), backColor.getGreen(), backColor.getBlue(), 0, 0); } @@ -739,11 +752,9 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value()) rectangle.setUpperRightY(this.pageSize.getUpperRightY() - topAux - topMargin -bottomMargin); break; } - PDPageContentStream contentStream = getNewPDPageContentStream(); - contentStream.setNonStrokingColor(backColor); - contentStream.addRect(rectangle.getLowerLeftX(), rectangle.getLowerLeftY(),rectangle.getWidth(), rectangle.getHeight()); - contentStream.fill(); - contentStream.close(); + cb.setNonStrokingColor(backColor); + cb.addRect(rectangle.getLowerLeftX(), rectangle.getLowerLeftY(),rectangle.getWidth(), rectangle.getHeight()); + cb.fill(); } float underlineSeparation = lineHeight / 5; @@ -1075,6 +1086,8 @@ private float renderHTMLContent(PDPageContentStream contentStream, String text, private void resolveTextStyling(PDPageContentStream contentStream, String text, float x, float y, boolean isWrapped){ try { + contentStream.setNonStrokingColor(foreColor); + contentStream.setRenderingMode(RenderingMode.FILL); if (this.fontBold && this.fontItalic){ contentStream.setStrokingColor(foreColor); contentStream.setLineWidth(fontSize * 0.05f); From 6cc8edc4b89ec1804ca6f1b87239726208cabfc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Sat, 30 Nov 2024 21:45:04 +0700 Subject: [PATCH 3/3] Revert "Encode with UTF-8 if getString has funny characters" This reverts commit a1b18c4a223eec7b3a159696957be449485f30da. --- .../com/genexus/internet/HttpClientJavaLib.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java index 9927fd1f7..608ed9285 100644 --- a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java +++ b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java @@ -718,16 +718,23 @@ public String getString() { return ""; try { this.setEntity(); - Charset charset = ContentType.getOrDefault(entity).getCharset(); - String res = EntityUtils.toString(entity, charset); - if (res.matches(".*[Ã-ÿ].*")) { - res = EntityUtils.toString(entity, StandardCharsets.UTF_8); + ContentType contentType = ContentType.getOrDefault(entity); + Charset charset; + if (contentType.equals(ContentType.DEFAULT_TEXT)) { + charset = StandardCharsets.UTF_8; + } else { + charset = contentType.getCharset(); + if (charset == null) { + charset = StandardCharsets.UTF_8; + } } + String res = EntityUtils.toString(entity, charset); eof = true; return res; } catch (IOException e) { setExceptionsCatch(e); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException e) { + } return ""; }