[java-identity-provider] branch master updated: OSJ-285: Ensure that Closeable instances are actually closed after use

Brent Putman putmanb at georgetown.edu
Fri Feb 14 21:59:52 EST 2020


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=70eced7e80ea68116fb4056f6b95ba774eaef23b

The following commit(s) were added to refs/heads/master by this push:
       new  70eced7   OSJ-285: Ensure that Closeable instances are actually closed after use
70eced7 is described below

commit 70eced7e80ea68116fb4056f6b95ba774eaef23b
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Feb 14 21:59:39 2020 -0500

    OSJ-285: Ensure that Closeable instances are actually closed after use
---
 .../consent/storage/impl/CollectionSerializer.java | 27 +++++++++++-----------
 .../saml/nameid/impl/TransientIdParameters.java    | 21 ++++++++---------
 .../idp/session/AbstractSPSessionSerializer.java   |  7 ++----
 .../impl/StorageBackedIdPSessionSerializer.java    |  9 ++++----
 4 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/idp-consent-impl/src/main/java/net/shibboleth/idp/consent/storage/impl/CollectionSerializer.java b/idp-consent-impl/src/main/java/net/shibboleth/idp/consent/storage/impl/CollectionSerializer.java
index 887c1f4..21e981e 100644
--- a/idp-consent-impl/src/main/java/net/shibboleth/idp/consent/storage/impl/CollectionSerializer.java
+++ b/idp-consent-impl/src/main/java/net/shibboleth/idp/consent/storage/impl/CollectionSerializer.java
@@ -94,23 +94,24 @@ public class CollectionSerializer extends AbstractInitializableComponent impleme
             @Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key,
             @Nonnull @NotEmpty final String value, @Nullable final Long expiration) throws IOException {
 
-        final JsonReader reader = readerFactory.createReader(new StringReader(value));
-        final JsonStructure st = reader.read();
-        if (!(st instanceof JsonArray)) {
-            throw new IOException("Found invalid data structure");
-        }
+        try (final JsonReader reader = readerFactory.createReader(new StringReader(value))) {
+            final JsonStructure st = reader.read();
+            if (!(st instanceof JsonArray)) {
+                throw new IOException("Found invalid data structure");
+            }
 
-        final Collection<String> collection = new ArrayList<>();
+            final Collection<String> collection = new ArrayList<>();
 
-        for (final JsonValue arrayValue : (JsonArray) st) {
-            if (arrayValue.getValueType().equals(ValueType.STRING)) {
-                collection.add(((JsonString) arrayValue).getString());
+            for (final JsonValue arrayValue : (JsonArray) st) {
+                if (arrayValue.getValueType().equals(ValueType.STRING)) {
+                    collection.add(((JsonString) arrayValue).getString());
+                }
             }
-        }
 
-        log.debug("Deserialized context '{}' key '{}' value '{}' expiration '{}' as '{}'", new Object[] {context, key,
-                value, expiration, collection,});
-        return collection;
+            log.debug("Deserialized context '{}' key '{}' value '{}' expiration '{}' as '{}'", new Object[] {context, key,
+                    value, expiration, collection,});
+            return collection;
+        }
     }
 
 }
diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/nameid/impl/TransientIdParameters.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/nameid/impl/TransientIdParameters.java
index 82f2a89..c536b95 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/nameid/impl/TransientIdParameters.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/nameid/impl/TransientIdParameters.java
@@ -80,16 +80,17 @@ public class TransientIdParameters {
     public TransientIdParameters(@Nonnull @NotEmpty final String encoded) throws IOException {
         Constraint.isNotNull(StringSupport.trimOrNull(encoded), "encoded data must not be null or empty");
 
-        final JsonReader reader = Json.createReader(new StringReader(encoded));
-        final JsonStructure st = reader.read();
+        try (final JsonReader reader = Json.createReader(new StringReader(encoded))) {
+            final JsonStructure st = reader.read();
 
-        if (!(st instanceof JsonObject)) {
-            throw new IOException("Found invalid data structure while parsing IdPSession");
-        }
-        final JsonObject jsonObj = (JsonObject) st;
+            if (!(st instanceof JsonObject)) {
+                throw new IOException("Found invalid data structure while parsing IdPSession");
+            }
+            final JsonObject jsonObj = (JsonObject) st;
 
-        principal = jsonObj.getString(PRINCIPAL_FIELD);
-        attributeRecipient = jsonObj.getString(ATTRIBUTE_RECIPIENT_FIELD);
+            principal = jsonObj.getString(PRINCIPAL_FIELD);
+            attributeRecipient = jsonObj.getString(ATTRIBUTE_RECIPIENT_FIELD);
+        }
     }
 
     /**
@@ -117,9 +118,7 @@ public class TransientIdParameters {
      * @throws IOException if encoding failed
      */
     @Nonnull public String encode() throws IOException {
-        try {
-            final StringWriter sink = new StringWriter(128);
-            final JsonGenerator gen = Json.createGenerator(sink);
+        try (final StringWriter sink = new StringWriter(128); final JsonGenerator gen = Json.createGenerator(sink)) {
             gen.writeStartObject().write(ATTRIBUTE_RECIPIENT_FIELD, getAttributeRecipient())
                     .write(PRINCIPAL_FIELD, getPrincipal());
             gen.writeEnd().close();
diff --git a/idp-session-api/src/main/java/net/shibboleth/idp/session/AbstractSPSessionSerializer.java b/idp-session-api/src/main/java/net/shibboleth/idp/session/AbstractSPSessionSerializer.java
index b2b910f..30321bc 100644
--- a/idp-session-api/src/main/java/net/shibboleth/idp/session/AbstractSPSessionSerializer.java
+++ b/idp-session-api/src/main/java/net/shibboleth/idp/session/AbstractSPSessionSerializer.java
@@ -71,9 +71,7 @@ public abstract class AbstractSPSessionSerializer extends AbstractInitializableC
 
     /** {@inheritDoc} */
     @Nonnull @NotEmpty public String serialize(@Nonnull final SPSession instance) throws IOException {
-        try {
-            final StringWriter sink = new StringWriter(128);
-            final JsonGenerator gen = Json.createGenerator(sink);
+        try (final StringWriter sink = new StringWriter(128); final JsonGenerator gen = Json.createGenerator(sink)) {
             gen.writeStartObject()
                 .write(SERVICE_ID_FIELD, instance.getId())
                 .write(CREATION_INSTANT_FIELD, instance.getCreationInstant().toEpochMilli());
@@ -98,8 +96,7 @@ public abstract class AbstractSPSessionSerializer extends AbstractInitializableC
             throw new IOException("SPSession objects must have an expiration");
         }
 
-        try {
-            final JsonReader reader = Json.createReader(new StringReader(value));
+        try (final JsonReader reader = Json.createReader(new StringReader(value))) {
             final JsonStructure st = reader.read();
             if (!(st instanceof JsonObject)) {
                 throw new IOException("Found invalid data structure while parsing SPSession");
diff --git a/idp-session-impl/src/main/java/net/shibboleth/idp/session/impl/StorageBackedIdPSessionSerializer.java b/idp-session-impl/src/main/java/net/shibboleth/idp/session/impl/StorageBackedIdPSessionSerializer.java
index 946b7ea..eefe6c3 100644
--- a/idp-session-impl/src/main/java/net/shibboleth/idp/session/impl/StorageBackedIdPSessionSerializer.java
+++ b/idp-session-impl/src/main/java/net/shibboleth/idp/session/impl/StorageBackedIdPSessionSerializer.java
@@ -101,9 +101,9 @@ public class StorageBackedIdPSessionSerializer extends AbstractInitializableComp
     @Override @Nonnull @NotEmpty public String serialize(@Nonnull final StorageBackedIdPSession instance)
             throws IOException {
 
-        try {
-            final StringWriter sink = new StringWriter(128);
-            final JsonGenerator gen = jsonProvider.createGenerator(sink);
+        try (final StringWriter sink = new StringWriter(128);
+                final JsonGenerator gen = jsonProvider.createGenerator(sink)) {
+            
             gen.writeStartObject().write(CREATION_INSTANT_FIELD, instance.getCreationInstant().toEpochMilli())
                     .write(PRINCIPAL_NAME_FIELD, instance.getPrincipalName());
 
@@ -158,8 +158,7 @@ public class StorageBackedIdPSessionSerializer extends AbstractInitializableComp
             throw new IOException("IdPSession objects must have an expiration");
         }
 
-        try {
-            final JsonReader reader = jsonProvider.createReader(new StringReader(value));
+        try (final JsonReader reader = jsonProvider.createReader(new StringReader(value))) {
             final JsonStructure st = reader.read();
             if (!(st instanceof JsonObject)) {
                 throw new IOException("Found invalid data structure while parsing IdPSession");

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list