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

Commit 83ea8fa

Browse files
committed
SANTUARIO-546 - refactor Reference class. Thanks to Alanscut <wp_scut@163.com> for the patch.
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1877775 13f79535-47bb-0310-9956-ffa450edef68
1 parent 487be76 commit 83ea8fa

1 file changed

Lines changed: 21 additions & 40 deletions

File tree

src/main/java/org/apache/xml/security/signature/Reference.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import java.io.OutputStream;
2323
import java.security.AccessController;
2424
import java.security.PrivilegedAction;
25+
import java.util.Collections;
2526
import java.util.HashSet;
2627
import java.util.Iterator;
2728
import java.util.Set;
2829

2930
import org.apache.xml.security.algorithms.Algorithm;
3031
import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
3132
import org.apache.xml.security.c14n.CanonicalizationException;
32-
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
3333
import org.apache.xml.security.exceptions.XMLSecurityException;
3434
import org.apache.xml.security.signature.reference.ReferenceData;
3535
import org.apache.xml.security.signature.reference.ReferenceNodeSetData;
@@ -137,6 +137,19 @@ public class Reference extends SignatureElementProxy {
137137

138138
private ReferenceData referenceData;
139139

140+
private static final Set<String> TRANSFORM_ALGORITHMS;
141+
142+
static {
143+
Set<String> algorithms = new HashSet<>();
144+
algorithms.add(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
145+
algorithms.add(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS);
146+
algorithms.add(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
147+
algorithms.add(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
148+
algorithms.add(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS);
149+
algorithms.add(Transforms.TRANSFORM_C14N11_WITH_COMMENTS);
150+
TRANSFORM_ALGORITHMS = Collections.unmodifiableSet(algorithms);
151+
}
152+
140153
/**
141154
* Constructor Reference
142155
*
@@ -148,7 +161,6 @@ public class Reference extends SignatureElementProxy {
148161
* @param messageDigestAlgorithm {@link MessageDigestAlgorithm Digest algorithm} which is
149162
* applied to the data
150163
* TODO should we throw XMLSignatureException if MessageDigestAlgoURI is wrong?
151-
* @throws XMLSignatureException
152164
*/
153165
protected Reference(
154166
Document doc, String baseURI, String referenceURI, Manifest manifest,
@@ -273,7 +285,7 @@ public MessageDigestAlgorithm getMessageDigestAlgorithm() throws XMLSignatureExc
273285

274286
String uri = digestMethodElem.getAttributeNS(null, Constants._ATT_ALGORITHM);
275287

276-
if ("".equals(uri)) {
288+
if (uri.isEmpty()) {
277289
return null;
278290
}
279291

@@ -340,7 +352,7 @@ public void setType(String type) {
340352
}
341353

342354
/**
343-
* Return the <code>type</code> atttibute of the Reference indicate whether an
355+
* Return the <code>type</code> attribute of the Reference indicate whether an
344356
* <code>ds:Object</code>, <code>ds:SignatureProperty</code>, or <code>ds:Manifest</code>
345357
* element
346358
*
@@ -444,14 +456,6 @@ private XMLSignatureInput getContentsAfterTransformation(
444456
}
445457

446458
return output;
447-
} catch (ResourceResolverException ex) {
448-
throw new XMLSignatureException(ex);
449-
} catch (CanonicalizationException ex) {
450-
throw new XMLSignatureException(ex);
451-
} catch (InvalidCanonicalizerException ex) {
452-
throw new XMLSignatureException(ex);
453-
} catch (TransformationException ex) {
454-
throw new XMLSignatureException(ex);
455459
} catch (XMLSecurityException ex) {
456460
throw new XMLSignatureException(ex);
457461
}
@@ -490,12 +494,7 @@ public XMLSignatureInput getNodesetBeforeFirstCanonicalization()
490494
Transform t = transforms.item(i);
491495
String uri = t.getURI();
492496

493-
if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS)
494-
|| uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS)
495-
|| uri.equals(Transforms.TRANSFORM_C14N_OMIT_COMMENTS)
496-
|| uri.equals(Transforms.TRANSFORM_C14N_WITH_COMMENTS)
497-
|| uri.equals(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS)
498-
|| uri.equals(Transforms.TRANSFORM_C14N11_WITH_COMMENTS)) {
497+
if (TRANSFORM_ALGORITHMS.contains(uri)) {
499498
break;
500499
}
501500

@@ -505,17 +504,7 @@ public XMLSignatureInput getNodesetBeforeFirstCanonicalization()
505504
output.setSourceURI(input.getSourceURI());
506505
}
507506
return output;
508-
} catch (IOException ex) {
509-
throw new XMLSignatureException(ex);
510-
} catch (ResourceResolverException ex) {
511-
throw new XMLSignatureException(ex);
512-
} catch (CanonicalizationException ex) {
513-
throw new XMLSignatureException(ex);
514-
} catch (InvalidCanonicalizerException ex) {
515-
throw new XMLSignatureException(ex);
516-
} catch (TransformationException ex) {
517-
throw new XMLSignatureException(ex);
518-
} catch (XMLSecurityException ex) {
507+
} catch (IOException | XMLSecurityException ex) {
519508
throw new XMLSignatureException(ex);
520509
}
521510
}
@@ -566,10 +555,6 @@ public String getHTMLRepresentation() throws XMLSignatureException {
566555
}
567556

568557
return nodes.getHTMLRepresentation(inclusiveNamespaces);
569-
} catch (TransformationException ex) {
570-
throw new XMLSignatureException(ex);
571-
} catch (InvalidTransformException ex) {
572-
throw new XMLSignatureException(ex);
573558
} catch (XMLSecurityException ex) {
574559
throw new XMLSignatureException(ex);
575560
}
@@ -625,7 +610,7 @@ private void cacheDereferencedElement(XMLSignatureInput input) {
625610
public Iterator<Node> iterator() {
626611
return new Iterator<Node>() {
627612

628-
Iterator<Node> sIterator = s.iterator();
613+
final Iterator<Node> sIterator = s.iterator();
629614

630615
@Override
631616
public boolean hasNext() {
@@ -690,9 +675,7 @@ public byte[] getReferencedBytes()
690675
try {
691676
XMLSignatureInput output = this.dereferenceURIandPerformTransforms(null);
692677
return output.getBytes();
693-
} catch (IOException ex) {
694-
throw new ReferenceNotInitializedException(ex);
695-
} catch (CanonicalizationException ex) {
678+
} catch (IOException | CanonicalizationException ex) {
696679
throw new ReferenceNotInitializedException(ex);
697680
}
698681
}
@@ -748,9 +731,7 @@ private byte[] calculateDigest(boolean validating)
748731
//mda.update(data);
749732

750733
return diOs.getDigestValue();
751-
} catch (XMLSecurityException ex) {
752-
throw new ReferenceNotInitializedException(ex);
753-
} catch (IOException ex) {
734+
} catch (XMLSecurityException | IOException ex) {
754735
throw new ReferenceNotInitializedException(ex);
755736
}
756737
}

0 commit comments

Comments
 (0)