Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit cb6ce07

Browse files
committed
SANTUARIO-522 - Make Serializer final in XMLCipher
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1873251 13f79535-47bb-0310-9956-ffa450edef68
1 parent f1a6c5e commit cb6ce07

6 files changed

Lines changed: 129 additions & 96 deletions

File tree

src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828

2929
import org.apache.xml.security.c14n.Canonicalizer;
30+
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
3031
import org.w3c.dom.Element;
3132
import org.w3c.dom.NamedNodeMap;
3233
import org.w3c.dom.Node;
@@ -39,10 +40,12 @@
3940
*/
4041
public abstract class AbstractSerializer implements Serializer {
4142

42-
private Canonicalizer canon;
43+
private final Canonicalizer canon;
44+
protected final boolean secureValidation;
4345

44-
public void setCanonicalizer(Canonicalizer canon) {
45-
this.canon = canon;
46+
protected AbstractSerializer(String canonAlg, boolean secureValidation) throws InvalidCanonicalizerException {
47+
this.canon = Canonicalizer.getInstance(canonAlg);
48+
this.secureValidation = secureValidation;
4649
}
4750

4851
/**

src/main/java/org/apache/xml/security/encryption/DocumentSerializer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import javax.xml.parsers.ParserConfigurationException;
2626

27+
import org.apache.xml.security.c14n.Canonicalizer;
28+
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
2729
import org.apache.xml.security.utils.XMLUtils;
2830
import org.w3c.dom.Document;
2931
import org.w3c.dom.DocumentFragment;
@@ -36,28 +38,34 @@
3638
*/
3739
public class DocumentSerializer extends AbstractSerializer {
3840

41+
public DocumentSerializer(boolean secureValidation) throws InvalidCanonicalizerException {
42+
this(Canonicalizer.ALGO_ID_C14N_PHYSICAL, secureValidation);
43+
}
44+
45+
public DocumentSerializer(String canonAlg, boolean secureValidation) throws InvalidCanonicalizerException {
46+
super(canonAlg, secureValidation);
47+
}
48+
3949
/**
4050
* @param source
4151
* @param ctx
42-
* @param secureValidation
4352
* @return the Node resulting from the parse of the source
4453
* @throws XMLEncryptionException
4554
*/
46-
public Node deserialize(byte[] source, Node ctx, boolean secureValidation) throws XMLEncryptionException, IOException {
55+
public Node deserialize(byte[] source, Node ctx) throws XMLEncryptionException, IOException {
4756
byte[] fragment = createContext(source, ctx);
4857
try (InputStream is = new ByteArrayInputStream(fragment)) {
49-
return deserialize(ctx, is, secureValidation);
58+
return deserialize(ctx, is);
5059
}
5160
}
5261

5362
/**
5463
* @param ctx
5564
* @param inputStream
56-
* @param secureValidation
5765
* @return the Node resulting from the parse of the source
5866
* @throws XMLEncryptionException
5967
*/
60-
private Node deserialize(Node ctx, InputStream inputStream, boolean secureValidation) throws XMLEncryptionException {
68+
private Node deserialize(Node ctx, InputStream inputStream) throws XMLEncryptionException {
6169
try {
6270
Document d = XMLUtils.read(inputStream, secureValidation);
6371

src/main/java/org/apache/xml/security/encryption/Serializer.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.IOException;
2222

23-
import org.apache.xml.security.c14n.Canonicalizer;
2423
import org.w3c.dom.Element;
2524
import org.w3c.dom.Node;
2625
import org.w3c.dom.NodeList;
@@ -30,11 +29,6 @@
3029
*/
3130
public interface Serializer {
3231

33-
/**
34-
* Set the Canonicalizer object to use.
35-
*/
36-
void setCanonicalizer(Canonicalizer canon);
37-
3832
/**
3933
* Returns a <code>byte[]</code> representation of the specified
4034
* <code>Element</code>.
@@ -60,9 +54,8 @@ public interface Serializer {
6054
/**
6155
* @param source
6256
* @param ctx
63-
* @param secureValidation
6457
* @return the Node resulting from the parse of the source
6558
* @throws XMLEncryptionException
6659
*/
67-
Node deserialize(byte[] source, Node ctx, boolean secureValidation) throws XMLEncryptionException, IOException;
60+
Node deserialize(byte[] source, Node ctx) throws XMLEncryptionException, IOException;
6861
}

src/main/java/org/apache/xml/security/encryption/TransformSerializer.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
import javax.xml.XMLConstants;
2626
import javax.xml.transform.Source;
2727
import javax.xml.transform.Transformer;
28+
import javax.xml.transform.TransformerConfigurationException;
2829
import javax.xml.transform.TransformerFactory;
2930
import javax.xml.transform.dom.DOMResult;
3031
import javax.xml.transform.stream.StreamSource;
3132

33+
import org.apache.xml.security.c14n.Canonicalizer;
34+
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
3235
import org.w3c.dom.Document;
3336
import org.w3c.dom.DocumentFragment;
3437
import org.w3c.dom.Node;
@@ -39,30 +42,47 @@
3942
*/
4043
public class TransformSerializer extends AbstractSerializer {
4144

42-
private TransformerFactory transformerFactory;
45+
private final TransformerFactory transformerFactory;
46+
47+
public TransformSerializer(boolean secureValidation) throws InvalidCanonicalizerException, TransformerConfigurationException {
48+
this(Canonicalizer.ALGO_ID_C14N_PHYSICAL, secureValidation);
49+
}
50+
51+
public TransformSerializer(String canonAlg, boolean secureValidation) throws TransformerConfigurationException, InvalidCanonicalizerException {
52+
super(canonAlg, secureValidation);
53+
54+
transformerFactory = TransformerFactory.newInstance();
55+
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
56+
if (secureValidation) {
57+
try {
58+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
59+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
60+
} catch (IllegalArgumentException ex) {
61+
// ignore
62+
}
63+
}
64+
}
4365

4466
/**
4567
* @param source
4668
* @param ctx
47-
* @param secureValidation
4869
* @return the Node resulting from the parse of the source
4970
* @throws XMLEncryptionException
5071
*/
51-
public Node deserialize(byte[] source, Node ctx, boolean secureValidation) throws XMLEncryptionException, IOException {
72+
public Node deserialize(byte[] source, Node ctx) throws XMLEncryptionException, IOException {
5273
byte[] fragment = createContext(source, ctx);
5374
try (InputStream is = new ByteArrayInputStream(fragment)) {
54-
return deserialize(ctx, new StreamSource(is), secureValidation);
75+
return deserialize(ctx, new StreamSource(is));
5576
}
5677
}
5778

5879
/**
5980
* @param ctx
6081
* @param source
61-
* @param secureValidation
6282
* @return the Node resulting from the parse of the source
6383
* @throws XMLEncryptionException
6484
*/
65-
private Node deserialize(Node ctx, Source source, boolean secureValidation) throws XMLEncryptionException {
85+
private Node deserialize(Node ctx, Source source) throws XMLEncryptionException {
6686
try {
6787
Document contextDocument = null;
6888
if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
@@ -71,18 +91,6 @@ private Node deserialize(Node ctx, Source source, boolean secureValidation) thro
7191
contextDocument = ctx.getOwnerDocument();
7292
}
7393

74-
if (transformerFactory == null) {
75-
transformerFactory = TransformerFactory.newInstance();
76-
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
77-
if (secureValidation) {
78-
try {
79-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
80-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
81-
} catch (IllegalArgumentException ex) {
82-
// ignore
83-
}
84-
}
85-
}
8694
Transformer transformer = transformerFactory.newTransformer();
8795

8896
DOMResult res = new DOMResult();

0 commit comments

Comments
 (0)