cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [1/2] cxf-fediz git commit: Some try with resources changes
Date Thu, 28 May 2015 13:28:45 GMT
Repository: cxf-fediz
Updated Branches:
  refs/heads/master 6f3178bfa -> 1b6ee30e4


Some try with resources changes


Project: http://git-wip-us.apache.org/repos/asf/cxf-fediz/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-fediz/commit/007666fb
Tree: http://git-wip-us.apache.org/repos/asf/cxf-fediz/tree/007666fb
Diff: http://git-wip-us.apache.org/repos/asf/cxf-fediz/diff/007666fb

Branch: refs/heads/master
Commit: 007666fbc171d65f4627285a648989ec9a352d55
Parents: 6f3178b
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Thu May 28 11:55:15 2015 +0100
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Thu May 28 11:55:15 2015 +0100

----------------------------------------------------------------------
 .../cxf/fediz/core/metadata/MetadataWriter.java | 23 ++++++++++----------
 .../org/apache/cxf/fediz/common/STSUtil.java    |  7 +++---
 .../service/idp/beans/STSClientAction.java      |  9 +++++---
 .../service/idp/metadata/IdpMetadataWriter.java | 17 +++++++--------
 .../TrustedIdpWSFedProtocolHandler.java         |  9 +++++---
 .../apache/cxf/fediz/sts/AbstractSTSTest.java   |  7 +++---
 .../fediz/integrationtests/KerberosTest.java    |  6 ++---
 7 files changed, 41 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/plugins/core/src/main/java/org/apache/cxf/fediz/core/metadata/MetadataWriter.java
----------------------------------------------------------------------
diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/metadata/MetadataWriter.java
b/plugins/core/src/main/java/org/apache/cxf/fediz/core/metadata/MetadataWriter.java
index d55cdcf..5bc6a73 100644
--- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/metadata/MetadataWriter.java
+++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/metadata/MetadataWriter.java
@@ -72,8 +72,7 @@ public class MetadataWriter {
         HttpServletRequest request, FedizContext config
     ) throws ProcessingException {
 
-        try {
-            ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
+        try (ByteArrayOutputStream bout = new ByteArrayOutputStream(4096)) {
             Writer streamWriter = new OutputStreamWriter(bout, "UTF-8");
             XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(streamWriter);
 
@@ -119,8 +118,6 @@ public class MetadataWriter {
                 LOG.debug("***************** unsigned ****************");
             }
 
-            InputStream is = new ByteArrayInputStream(bout.toByteArray());
-            
             boolean hasSigningKey = false;
             try {
                 if (config.getSigningKey().getCrypto() != null) {
@@ -129,16 +126,18 @@ public class MetadataWriter {
             } catch (Exception ex) {
                 LOG.info("No signingKey element found in config: " + ex.getMessage());
             }
-            if (hasSigningKey) {
-                Document result = SignatureUtils.signMetaInfo(
-                    config.getSigningKey().getCrypto(), config.getSigningKey().getKeyAlias(),
config.getSigningKey().getKeyPassword(), is, referenceID);
-                if (result != null) {
-                    return result;
-                } else {
-                    throw new ProcessingException("Failed to sign the metadata document:
result=null");
+            try (InputStream is = new ByteArrayInputStream(bout.toByteArray())) {
+                if (hasSigningKey) {
+                    Document result = SignatureUtils.signMetaInfo(
+                        config.getSigningKey().getCrypto(), config.getSigningKey().getKeyAlias(),
config.getSigningKey().getKeyPassword(), is, referenceID);
+                    if (result != null) {
+                        return result;
+                    } else {
+                        throw new ProcessingException("Failed to sign the metadata document:
result=null");
+                    }
                 }
+                return DOMUtils.readXml(is);
             }
-            return DOMUtils.readXml(is);
         } catch (ProcessingException e) {
             throw e;
         } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/plugins/core/src/test/java/org/apache/cxf/fediz/common/STSUtil.java
----------------------------------------------------------------------
diff --git a/plugins/core/src/test/java/org/apache/cxf/fediz/common/STSUtil.java b/plugins/core/src/test/java/org/apache/cxf/fediz/common/STSUtil.java
index d3955f4..3698672 100644
--- a/plugins/core/src/test/java/org/apache/cxf/fediz/common/STSUtil.java
+++ b/plugins/core/src/test/java/org/apache/cxf/fediz/common/STSUtil.java
@@ -69,9 +69,10 @@ public class STSUtil {
      * Convert an XML document as a String to a org.w3c.dom.Document.
      */
     public static org.w3c.dom.Document toSOAPPart(String xml) throws Exception {
-        InputStream in = new ByteArrayInputStream(xml.getBytes());
-        DocumentBuilder builder = factory.newDocumentBuilder();
-        return builder.parse(in);
+        try (InputStream in = new ByteArrayInputStream(xml.getBytes())) {
+            DocumentBuilder builder = factory.newDocumentBuilder();
+            return builder.parse(in);
+        }
     }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/beans/STSClientAction.java
----------------------------------------------------------------------
diff --git a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/beans/STSClientAction.java
b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/beans/STSClientAction.java
index 948c557..d1a2f27 100644
--- a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/beans/STSClientAction.java
+++ b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/beans/STSClientAction.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.fediz.service.idp.beans;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
 import java.net.MalformedURLException;
@@ -299,9 +300,11 @@ public class STSClientAction {
         return StringEscapeUtils.escapeXml11(rpToken);
     }
     
-    private String getIdFromToken(String token) throws XMLStreamException {
-        InputStream is = new ByteArrayInputStream(token.getBytes());
-        Document doc = StaxUtils.read(is);
+    private String getIdFromToken(String token) throws IOException, XMLStreamException {
+        Document doc = null;
+        try (InputStream is = new ByteArrayInputStream(token.getBytes())) {
+            doc = StaxUtils.read(is);
+        }
         NodeList nd = doc.getElementsByTagNameNS(WSConstants.SAML2_NS, "Assertion");
         
         String identifier = "ID";

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/metadata/IdpMetadataWriter.java
----------------------------------------------------------------------
diff --git a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/metadata/IdpMetadataWriter.java
b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/metadata/IdpMetadataWriter.java
index cb2f779..2717426 100644
--- a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/metadata/IdpMetadataWriter.java
+++ b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/metadata/IdpMetadataWriter.java
@@ -61,10 +61,9 @@ public class IdpMetadataWriter {
     //CHECKSTYLE:OFF
     public Document getMetaData(Idp config) throws RuntimeException {
         //Return as text/xml
-        try {
+        try (ByteArrayOutputStream bout = new ByteArrayOutputStream(4096)) {
             Crypto crypto = CertsUtils.createCrypto(config.getCertificate());
             
-            ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
             Writer streamWriter = new OutputStreamWriter(bout, "UTF-8");
             XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(streamWriter);
 
@@ -97,13 +96,13 @@ public class IdpMetadataWriter {
                 LOG.debug("***************** unsigned ****************");
             }
             
-            InputStream is = new ByteArrayInputStream(bout.toByteArray());
-            
-            Document result = SignatureUtils.signMetaInfo(crypto, null, config.getCertificatePassword(),
is, referenceID);
-            if (result != null) {
-                return result;
-            } else {
-                throw new RuntimeException("Failed to sign the metadata document: result=null");
+            try (InputStream is = new ByteArrayInputStream(bout.toByteArray())) {
+                Document result = SignatureUtils.signMetaInfo(crypto, null, config.getCertificatePassword(),
is, referenceID);
+                if (result != null) {
+                    return result;
+                } else {
+                    throw new RuntimeException("Failed to sign the metadata document: result=null");
+                }
             }
         } catch (RuntimeException e) {
             throw e;

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/protocols/TrustedIdpWSFedProtocolHandler.java
----------------------------------------------------------------------
diff --git a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/protocols/TrustedIdpWSFedProtocolHandler.java
b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/protocols/TrustedIdpWSFedProtocolHandler.java
index 0572921..1f9da57 100644
--- a/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/protocols/TrustedIdpWSFedProtocolHandler.java
+++ b/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/protocols/TrustedIdpWSFedProtocolHandler.java
@@ -20,6 +20,8 @@
 package org.apache.cxf.fediz.service.idp.protocols;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -234,14 +236,15 @@ public class TrustedIdpWSFedProtocolHandler implements TrustedIdpProtocolHandler
     }
     
     private X509Certificate parseCertificate(String certificate)
-        throws CertificateException, Base64DecodingException {
+        throws CertificateException, Base64DecodingException, IOException {
         
         //before decoding we need to get rod off the prefix and suffix
         byte [] decoded = Base64.decode(certificate.replaceAll("-----BEGIN CERTIFICATE-----",
"").
                                         replaceAll("-----END CERTIFICATE-----", ""));
 
-        return (X509Certificate)CertificateFactory.getInstance("X.509").
-            generateCertificate(new ByteArrayInputStream(decoded));
+        try (InputStream is = new ByteArrayInputStream(decoded)) {
+            return (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(is);
+        }
     }
     
 

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/services/sts/src/test/java/org/apache/cxf/fediz/sts/AbstractSTSTest.java
----------------------------------------------------------------------
diff --git a/services/sts/src/test/java/org/apache/cxf/fediz/sts/AbstractSTSTest.java b/services/sts/src/test/java/org/apache/cxf/fediz/sts/AbstractSTSTest.java
index 57ff904..0c33431 100644
--- a/services/sts/src/test/java/org/apache/cxf/fediz/sts/AbstractSTSTest.java
+++ b/services/sts/src/test/java/org/apache/cxf/fediz/sts/AbstractSTSTest.java
@@ -36,7 +36,6 @@ import javax.xml.bind.JAXBException;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 
-import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 import org.apache.cxf.Bus;
@@ -86,9 +85,9 @@ public abstract class AbstractSTSTest {
         StringWriter writer = new StringWriter();
         jaxbContext.createMarshaller().marshal(supportingToken, writer);
         writer.flush();
-        InputStream is = new ByteArrayInputStream(writer.toString().getBytes());
-        Document doc = StaxUtils.read(is);
-        return doc.getDocumentElement();
+        try (InputStream is = new ByteArrayInputStream(writer.toString().getBytes())) {
+            return StaxUtils.read(is).getDocumentElement();
+        }
     }
 
     protected JAXBElement<UsernameTokenType> createUsernameToken(String name, String
password) {

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/007666fb/systests/kerberos/src/test/java/org/apache/cxf/fediz/integrationtests/KerberosTest.java
----------------------------------------------------------------------
diff --git a/systests/kerberos/src/test/java/org/apache/cxf/fediz/integrationtests/KerberosTest.java
b/systests/kerberos/src/test/java/org/apache/cxf/fediz/integrationtests/KerberosTest.java
index 8379b3e..5ff15f6 100644
--- a/systests/kerberos/src/test/java/org/apache/cxf/fediz/integrationtests/KerberosTest.java
+++ b/systests/kerberos/src/test/java/org/apache/cxf/fediz/integrationtests/KerberosTest.java
@@ -159,9 +159,9 @@ public class KerberosTest extends AbstractLdapTestUnit {
             content = content.replaceAll("port", "" + super.getKdcServer().getTransports()[0].getPort());
             
             File f2 = new File(basedir + "/target/test-classes/fediz.kerberos.krb5.conf");
-            FileOutputStream outputStream = new FileOutputStream(f2);
-            IOUtils.write(content, outputStream, "UTF-8");
-            outputStream.close();
+            try (FileOutputStream outputStream = new FileOutputStream(f2)) {
+                IOUtils.write(content, outputStream, "UTF-8");
+            }
             
             System.setProperty("java.security.krb5.conf", f2.getPath());
             portUpdated = true;


Mime
View raw message