From 04acfb0a80eaab0bde2e16dcea1b07e3ed9a2e61 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 11 Mar 2020 17:52:33 -0500 Subject: [PATCH] Optionally check for, and report, head/tail encoding problems --- .../logmanager/handlers/WriterHandler.java | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java b/core/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java index 726bb377..2d31db5d 100644 --- a/core/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java +++ b/core/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java @@ -36,6 +36,8 @@ public class WriterHandler extends ExtHandler { protected final Object outputLock = new Object(); + private volatile boolean checkHeadEncoding = true; + private volatile boolean checkTailEncoding = true; private Writer writer; /** @@ -118,10 +120,59 @@ public void setWriter(final Writer writer) { } } + /** + * Determine whether head encoding checking is turned on. + * + * @return {@code true} to check and report head encoding problems, or {@code false} to ignore them + */ + public boolean isCheckHeadEncoding() { + return checkHeadEncoding; + } + + /** + * Establish whether head encoding checking is turned on. + * + * @param checkHeadEncoding {@code true} to check and report head encoding problems, or {@code false} to ignore them + * @return this handler + */ + public WriterHandler setCheckHeadEncoding(boolean checkHeadEncoding) { + this.checkHeadEncoding = checkHeadEncoding; + return this; + } + + /** + * Determine whether tail encoding checking is turned on. + * + * @return {@code true} to check and report tail encoding problems, or {@code false} to ignore them + */ + public boolean isCheckTailEncoding() { + return checkTailEncoding; + } + + /** + * Establish whether tail encoding checking is turned on. + * + * @param checkTailEncoding {@code true} to check and report tail encoding problems, or {@code false} to ignore them + * @return this handler + */ + public WriterHandler setCheckTailEncoding(boolean checkTailEncoding) { + this.checkTailEncoding = checkTailEncoding; + return this; + } + private void writeHead(final Writer writer) { try { final Formatter formatter = getFormatter(); - if (formatter != null) writer.write(formatter.getHead(this)); + if (formatter != null) { + final String head = formatter.getHead(this); + if (checkHeadEncoding) { + if (!getCharset().newEncoder().canEncode(head)) { + reportError("Section header cannot be encoded into charset \"" + getCharset().name() + "\"", null, ErrorManager.GENERIC_FAILURE); + return; + } + } + writer.write(head); + } } catch (Exception e) { reportError("Error writing section header", e, ErrorManager.WRITE_FAILURE); } @@ -130,7 +181,16 @@ private void writeHead(final Writer writer) { private void writeTail(final Writer writer) { try { final Formatter formatter = getFormatter(); - if (formatter != null) writer.write(formatter.getTail(this)); + if (formatter != null) { + final String tail = formatter.getTail(this); + if (checkTailEncoding) { + if (!getCharset().newEncoder().canEncode(tail)) { + reportError("Section tail cannot be encoded into charset \"" + getCharset().name() + "\"", null, ErrorManager.GENERIC_FAILURE); + return; + } + } + writer.write(tail); + } } catch (Exception ex) { reportError("Error writing section tail", ex, ErrorManager.WRITE_FAILURE); }