Skip to content

Commit eb1b9e1

Browse files
committed
updated XMLUtils in response to review comments
1 parent 02180c4 commit eb1b9e1

1 file changed

Lines changed: 54 additions & 41 deletions

File tree

src/main/java/org/apache/xml/security/utils/XMLUtils.java

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* DOM and XML accessibility and comfort functions.
5858
*
5959
* @implNote
60-
* Following system properties affect XML formatting:
60+
* The following system properties affect XML formatting:
6161
* <ul>
6262
* <li>{@systemProperty org.apache.xml.security.ignoreLineBreaks} - ignores all line breaks,
6363
* making a single-line document. Overrides all other formatting options. Default: false</li>
@@ -66,47 +66,22 @@
6666
* <li>{@systemProperty org.apache.xml.security.base64.lineSeparator} - Sets the line separator sequence in base64Binary values.
6767
* Possible values: crlf, lf. Default: crlf</li>
6868
* <li>{@systemProperty org.apache.xml.security.base64.lineLength} - Sets maximum line length in base64Binary values.
69-
* The value is rounded down to nearest multiple of 4. Values less than 4 are ignored. Default: 76</li>
69+
* The value is rounded down to the nearest multiple of 4. Values less than 4 are ignored. Default: 76</li>
7070
* </ul>
7171
*/
7272
public final class XMLUtils {
7373

7474
private static final Logger LOG = System.getLogger(XMLUtils.class.getName());
7575

7676
private static final String IGNORE_LINE_BREAKS_PROP = "org.apache.xml.security.ignoreLineBreaks";
77-
private static final String BASE64_IGNORE_LINE_BREAKS_PROP = "org.apache.xml.security.base64.ignoreLineBreaks";
78-
private static final String BASE64_LINE_SEPARATOR_PROP = "org.apache.xml.security.base64.lineSeparator";
79-
private static final String BASE64_LINE_LENGTH_PROP = "org.apache.xml.security.base64.lineLength";
8077

8178
private static boolean ignoreLineBreaks =
8279
AccessController.doPrivileged(
8380
(PrivilegedAction<Boolean>) () -> Boolean.getBoolean(IGNORE_LINE_BREAKS_PROP));
8481

8582
private static Base64FormattingOptions base64Formatting =
86-
AccessController.doPrivileged((PrivilegedAction<Base64FormattingOptions>) () -> {
87-
Base64FormattingOptions options = new Base64FormattingOptions();
88-
options.setIgnoreLineBreaks(Boolean.getBoolean(BASE64_IGNORE_LINE_BREAKS_PROP));
89-
90-
String lineSeparator = System.getProperty(BASE64_LINE_SEPARATOR_PROP);
91-
if (lineSeparator != null) {
92-
try {
93-
options.setLineSeparator(Base64LineSeparator.valueOf(lineSeparator.toUpperCase()));
94-
} catch (IllegalArgumentException e) {
95-
LOG.log(Level.WARNING, "Illegal value of {0} property ignored: {1}",
96-
BASE64_LINE_SEPARATOR_PROP, lineSeparator);
97-
}
98-
}
99-
100-
Integer lineLength = Integer.getInteger(BASE64_LINE_LENGTH_PROP);
101-
if (lineLength != null && lineLength >= 4) {
102-
options.setLineLength(lineLength);
103-
} else if (lineLength != null) {
104-
LOG.log(Level.WARNING, "Illegal value of {0} property ignored: {1}",
105-
BASE64_LINE_LENGTH_PROP, lineLength);
106-
}
107-
108-
return options;
109-
});
83+
AccessController.doPrivileged(
84+
(PrivilegedAction<Base64FormattingOptions>) () -> new Base64FormattingOptions());
11085

11186
private static Base64.Encoder base64Encoder = (ignoreLineBreaks || base64Formatting.isIgnoreLineBreaks()) ?
11287
Base64.getEncoder() :
@@ -1152,33 +1127,71 @@ public static byte[] getBytes(BigInteger big, int bitlen) {
11521127
* Aggregates formatting options for base64Binary values.
11531128
*/
11541129
static class Base64FormattingOptions {
1130+
private static final String BASE64_IGNORE_LINE_BREAKS_PROP = "org.apache.xml.security.base64.ignoreLineBreaks";
1131+
private static final String BASE64_LINE_SEPARATOR_PROP = "org.apache.xml.security.base64.lineSeparator";
1132+
private static final String BASE64_LINE_LENGTH_PROP = "org.apache.xml.security.base64.lineLength";
1133+
11551134
private boolean ignoreLineBreaks = false;
11561135
private Base64LineSeparator lineSeparator = Base64LineSeparator.CRLF;
11571136
private int lineLength = 76;
11581137

1159-
public boolean isIgnoreLineBreaks() {
1160-
return ignoreLineBreaks;
1138+
/**
1139+
* Creates new formatting options by reading system properties.
1140+
*/
1141+
public Base64FormattingOptions() {
1142+
String ignoreLineBreaksProp = System.getProperty(BASE64_IGNORE_LINE_BREAKS_PROP);
1143+
ignoreLineBreaks = Boolean.parseBoolean(ignoreLineBreaksProp);
1144+
if (XMLUtils.ignoreLineBreaks && ignoreLineBreaksProp != null && !ignoreLineBreaks) {
1145+
LOG.log(Level.WARNING, "{0} property takes precedence over {1}, line breaks will be ignored",
1146+
IGNORE_LINE_BREAKS_PROP, BASE64_IGNORE_LINE_BREAKS_PROP);
1147+
}
1148+
1149+
String lineSeparatorProp = System.getProperty(BASE64_LINE_SEPARATOR_PROP);
1150+
if (lineSeparatorProp != null) {
1151+
try {
1152+
lineSeparator = Base64LineSeparator.valueOf(lineSeparatorProp.toUpperCase());
1153+
if (XMLUtils.ignoreLineBreaks || ignoreLineBreaks) {
1154+
LOG.log(Level.WARNING, "Property {0} has no effect since line breaks are ignored",
1155+
BASE64_LINE_SEPARATOR_PROP);
1156+
}
1157+
} catch (IllegalArgumentException e) {
1158+
LOG.log(Level.WARNING, "Illegal value of {0} property is ignored: {1}",
1159+
BASE64_LINE_SEPARATOR_PROP, lineSeparatorProp);
1160+
}
1161+
}
1162+
1163+
String lineLengthProp = System.getProperty(BASE64_LINE_LENGTH_PROP);
1164+
if (lineLengthProp != null) {
1165+
try {
1166+
int lineLength = Integer.parseInt(lineLengthProp);
1167+
if (lineLength >= 4) {
1168+
this.lineLength = lineLength;
1169+
if (XMLUtils.ignoreLineBreaks || ignoreLineBreaks) {
1170+
LOG.log(Level.WARNING, "Property {0} has no effect since line breaks are ignored",
1171+
BASE64_LINE_LENGTH_PROP);
1172+
}
1173+
} else {
1174+
LOG.log(Level.WARNING, "Illegal value of {0} property is ignored: {1}",
1175+
BASE64_LINE_LENGTH_PROP, lineLengthProp);
1176+
}
1177+
} catch (NumberFormatException e) {
1178+
LOG.log(Level.WARNING, "Illegal value of {0} property is ignored: {1}",
1179+
BASE64_LINE_LENGTH_PROP, lineLengthProp);
1180+
}
1181+
}
11611182
}
11621183

1163-
public void setIgnoreLineBreaks(boolean ignoreLineBreaks) {
1164-
this.ignoreLineBreaks = ignoreLineBreaks;
1184+
public boolean isIgnoreLineBreaks() {
1185+
return ignoreLineBreaks;
11651186
}
11661187

11671188
public Base64LineSeparator getLineSeparator() {
11681189
return lineSeparator;
11691190
}
11701191

1171-
public void setLineSeparator(Base64LineSeparator lineSeparator) {
1172-
this.lineSeparator = lineSeparator;
1173-
}
1174-
11751192
public int getLineLength() {
11761193
return lineLength;
11771194
}
1178-
1179-
public void setLineLength(int lineLength) {
1180-
this.lineLength = lineLength;
1181-
}
11821195
}
11831196

11841197
enum Base64LineSeparator {

0 commit comments

Comments
 (0)