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

Commit 282cc09

Browse files
committed
Some minor refactoring
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1876669 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5137bd0 commit 282cc09

2 files changed

Lines changed: 34 additions & 65 deletions

File tree

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

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,14 +1177,9 @@ private EncryptedData encryptData(
11771177
LOG.debug("Actual cipher.outputSize = "
11781178
+ Integer.toString(encryptedBytes.length));
11791179
}
1180-
} catch (IllegalStateException ise) {
1181-
throw new XMLEncryptionException(ise);
1182-
} catch (IllegalBlockSizeException ibse) {
1183-
throw new XMLEncryptionException(ibse);
1184-
} catch (BadPaddingException bpe) {
1185-
throw new XMLEncryptionException(bpe);
1186-
} catch (UnsupportedEncodingException uee) {
1187-
throw new XMLEncryptionException(uee);
1180+
} catch (IllegalStateException | IllegalBlockSizeException
1181+
| BadPaddingException | UnsupportedEncodingException e) {
1182+
throw new XMLEncryptionException(e);
11881183
}
11891184

11901185
// Get IV from Cipher Object. If this is null (see BouncyCastle issue BJA-473) then use
@@ -1404,11 +1399,7 @@ public EncryptedKey encryptKey(
14041399
}
14051400
}
14061401
encryptedBytes = c.wrap(key);
1407-
} catch (InvalidKeyException ike) {
1408-
throw new XMLEncryptionException(ike);
1409-
} catch (IllegalBlockSizeException ibse) {
1410-
throw new XMLEncryptionException(ibse);
1411-
} catch (InvalidAlgorithmParameterException e) {
1402+
} catch (InvalidKeyException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
14121403
throw new XMLEncryptionException(e);
14131404
}
14141405

@@ -1512,11 +1503,7 @@ public Key decryptKey(EncryptedKey encryptedKey, String algorithm)
15121503
c.init(Cipher.UNWRAP_MODE, key, oaepParameters);
15131504
}
15141505
ret = c.unwrap(encryptedBytes, jceKeyAlgorithm, Cipher.SECRET_KEY);
1515-
} catch (InvalidKeyException ike) {
1516-
throw new XMLEncryptionException(ike);
1517-
} catch (NoSuchAlgorithmException nsae) {
1518-
throw new XMLEncryptionException( nsae);
1519-
} catch (InvalidAlgorithmParameterException e) {
1506+
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
15201507
throw new XMLEncryptionException(e);
15211508
}
15221509
LOG.debug("Decryption of key type {} OK", algorithm);
@@ -1580,10 +1567,8 @@ private Cipher constructCipher(String algorithm, String digestAlgorithm) throws
15801567
// Check to see if an RSA OAEP MGF-1 with SHA-1 algorithm was requested
15811568
// Some JDKs don't support RSA/ECB/OAEPPadding
15821569
c = constructCipher(algorithm, digestAlgorithm, nsae);
1583-
} catch (NoSuchProviderException nspre) {
1584-
throw new XMLEncryptionException(nspre);
1585-
} catch (NoSuchPaddingException nspae) {
1586-
throw new XMLEncryptionException(nspae);
1570+
} catch (NoSuchProviderException | NoSuchPaddingException e) {
1571+
throw new XMLEncryptionException(e);
15871572
}
15881573

15891574
return c;
@@ -1791,12 +1776,8 @@ public byte[] decryptToByteArray(Element element) throws XMLEncryptionException
17911776
} else {
17921777
c = Cipher.getInstance(jceAlgorithm, requestedJCEProvider);
17931778
}
1794-
} catch (NoSuchAlgorithmException nsae) {
1795-
throw new XMLEncryptionException(nsae);
1796-
} catch (NoSuchProviderException nspre) {
1797-
throw new XMLEncryptionException(nspre);
1798-
} catch (NoSuchPaddingException nspae) {
1799-
throw new XMLEncryptionException(nspae);
1779+
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
1780+
throw new XMLEncryptionException(e);
18001781
}
18011782

18021783
int ivLen = JCEMapper.getIVLengthFromURI(encMethodAlgorithm) / 8;
@@ -1817,18 +1798,14 @@ public byte[] decryptToByteArray(Element element) throws XMLEncryptionException
18171798

18181799
try {
18191800
c.init(cipherMode, key, paramSpec);
1820-
} catch (InvalidKeyException ike) {
1821-
throw new XMLEncryptionException(ike);
1822-
} catch (InvalidAlgorithmParameterException iape) {
1823-
throw new XMLEncryptionException(iape);
1801+
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
1802+
throw new XMLEncryptionException(e);
18241803
}
18251804

18261805
try {
18271806
return c.doFinal(encryptedBytes, ivLen, encryptedBytes.length - ivLen);
1828-
} catch (IllegalBlockSizeException ibse) {
1829-
throw new XMLEncryptionException(ibse);
1830-
} catch (BadPaddingException bpe) {
1831-
throw new XMLEncryptionException(bpe);
1807+
} catch (IllegalBlockSizeException | BadPaddingException e) {
1808+
throw new XMLEncryptionException(e);
18321809
}
18331810
}
18341811

@@ -2209,12 +2186,8 @@ CipherReference newCipherReference(Element element) throws XMLEncryptionExceptio
22092186
LOG.debug("Creating a DSIG based Transforms element");
22102187
try {
22112188
result.setTransforms(new TransformsImpl(transformsElement));
2212-
} catch (XMLSignatureException xse) {
2213-
throw new XMLEncryptionException(xse);
2214-
} catch (InvalidTransformException ite) {
2215-
throw new XMLEncryptionException(ite);
2216-
} catch (XMLSecurityException xse) {
2217-
throw new XMLEncryptionException(xse);
2189+
} catch (XMLSecurityException e) {
2190+
throw new XMLEncryptionException(e);
22182191
}
22192192
}
22202193

@@ -2554,7 +2527,7 @@ public AgreementMethodImpl(String algorithm) {
25542527
tmpAlgorithm = new URI(algorithm);
25552528
} catch (URISyntaxException ex) {
25562529
throw (IllegalArgumentException)
2557-
new IllegalArgumentException().initCause(ex);
2530+
new IllegalArgumentException().initCause(ex);
25582531
}
25592532
algorithmURI = tmpAlgorithm.toString();
25602533
}
@@ -2970,7 +2943,7 @@ public void setType(String type) {
29702943
tmpType = new URI(type);
29712944
} catch (URISyntaxException ex) {
29722945
throw (IllegalArgumentException)
2973-
new IllegalArgumentException().initCause(ex);
2946+
new IllegalArgumentException().initCause(ex);
29742947
}
29752948
this.type = tmpType.toString();
29762949
}
@@ -3012,7 +2985,7 @@ public void setEncoding(String encoding) {
30122985
tmpEncoding = new URI(encoding);
30132986
} catch (URISyntaxException ex) {
30142987
throw (IllegalArgumentException)
3015-
new IllegalArgumentException().initCause(ex);
2988+
new IllegalArgumentException().initCause(ex);
30162989
}
30172990
this.encoding = tmpEncoding.toString();
30182991
}
@@ -3093,7 +3066,7 @@ public EncryptionMethodImpl(String algorithm) {
30933066
tmpAlgorithm = new URI(algorithm);
30943067
} catch (URISyntaxException ex) {
30953068
throw (IllegalArgumentException)
3096-
new IllegalArgumentException().initCause(ex);
3069+
new IllegalArgumentException().initCause(ex);
30973070
}
30983071
this.algorithm = tmpAlgorithm.toString();
30993072
encryptionMethodInformation = new LinkedList<>();
@@ -3300,7 +3273,7 @@ public void setTarget(String target) {
33003273
tmpTarget = new URI(target);
33013274
} catch (URISyntaxException ex) {
33023275
throw (IllegalArgumentException)
3303-
new IllegalArgumentException().initCause(ex);
3276+
new IllegalArgumentException().initCause(ex);
33043277
}
33053278
this.target = tmpTarget.toString();
33063279
}

src/main/java/org/apache/xml/security/keys/keyresolver/KeyResolver.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public static final X509Certificate getX509Certificate(
8383
for (KeyResolverSpi resolver : resolverList) {
8484
if (resolver == null) {
8585
Object[] exArgs = {
86-
element != null
87-
&& element.getNodeType() == Node.ELEMENT_NODE
88-
? element.getTagName() : "null"
86+
element != null
87+
&& element.getNodeType() == Node.ELEMENT_NODE
88+
? element.getTagName() : "null"
8989
};
9090

9191
throw new KeyResolverException("utils.resolver.noClass", exArgs);
@@ -99,9 +99,9 @@ public static final X509Certificate getX509Certificate(
9999
}
100100

101101
Object[] exArgs = {
102-
element != null && element.getNodeType() == Node.ELEMENT_NODE
103-
? element.getTagName() : "null"
104-
};
102+
element != null && element.getNodeType() == Node.ELEMENT_NODE
103+
? element.getTagName() : "null"
104+
};
105105

106106
throw new KeyResolverException("utils.resolver.noClass", exArgs);
107107
}
@@ -123,9 +123,9 @@ public static final PublicKey getPublicKey(
123123
for (KeyResolverSpi resolver : resolverList) {
124124
if (resolver == null) {
125125
Object[] exArgs = {
126-
element != null
127-
&& element.getNodeType() == Node.ELEMENT_NODE
128-
? element.getTagName() : "null"
126+
element != null
127+
&& element.getNodeType() == Node.ELEMENT_NODE
128+
? element.getTagName() : "null"
129129
};
130130

131131
throw new KeyResolverException("utils.resolver.noClass", exArgs);
@@ -139,9 +139,9 @@ public static final PublicKey getPublicKey(
139139
}
140140

141141
Object[] exArgs = {
142-
element != null && element.getNodeType() == Node.ELEMENT_NODE
143-
? element.getTagName() : "null"
144-
};
142+
element != null && element.getNodeType() == Node.ELEMENT_NODE
143+
? element.getTagName() : "null"
144+
};
145145

146146
throw new KeyResolverException("utils.resolver.noClass", exArgs);
147147
}
@@ -190,17 +190,13 @@ public static void registerAtStart(String className) {
190190
try {
191191
keyResolverSpi = (KeyResolverSpi) ClassLoaderUtils.loadClass(className, KeyResolver.class).newInstance();
192192
register(keyResolverSpi, true);
193-
} catch (ClassNotFoundException e) {
194-
ex = e;
195-
} catch (IllegalAccessException e) {
196-
ex = e;
197-
} catch (InstantiationException e) {
193+
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
198194
ex = e;
199195
}
200196

201197
if (ex != null) {
202198
throw (IllegalArgumentException) new
203-
IllegalArgumentException("Invalid KeyResolver class name").initCause(ex);
199+
IllegalArgumentException("Invalid KeyResolver class name").initCause(ex);
204200
}
205201
}
206202

0 commit comments

Comments
 (0)