[java-opensaml] branch master updated: OSJ-284: HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder ...

Brent Putman putmanb at georgetown.edu
Wed Sep 25 18:14:33 EDT 2019


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

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

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=bc04d3df2dc07a95a21fcee387e53d581996612d

The following commit(s) were added to refs/heads/master by this push:
       new  bc04d3d   OSJ-284: HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder ...
bc04d3d is described below

commit bc04d3df2dc07a95a21fcee387e53d581996612d
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed Sep 25 18:10:20 2019 -0400

    OSJ-284: HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder ...
    
    HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder should clean
    up resources appropriately
---
 .../decoding/impl/HTTPRedirectDeflateDecoder.java  | 51 ++++++++++++++++------
 .../encoding/impl/HTTPRedirectDeflateEncoder.java  | 41 ++++++++++++++---
 2 files changed, 73 insertions(+), 19 deletions(-)

diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/decoding/impl/HTTPRedirectDeflateDecoder.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/decoding/impl/HTTPRedirectDeflateDecoder.java
index db3e19d..c61ffec 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/decoding/impl/HTTPRedirectDeflateDecoder.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/decoding/impl/HTTPRedirectDeflateDecoder.java
@@ -18,6 +18,7 @@
 package org.opensaml.saml.saml2.binding.decoding.impl;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.zip.Inflater;
 import java.util.zip.InflaterInputStream;
@@ -98,22 +99,24 @@ public class HTTPRedirectDeflateDecoder extends BaseHttpServletRequestXMLMessage
         log.debug("Decoded RelayState: {}", relayState);
         SAMLBindingSupport.setRelayState(messageContext, relayState);
 
-        final InputStream samlMessageIns;
-        if (!Strings.isNullOrEmpty(request.getParameter("SAMLRequest"))) {
-            samlMessageIns = decodeMessage(request.getParameter("SAMLRequest"));
-        } else if (!Strings.isNullOrEmpty(request.getParameter("SAMLResponse"))) {
-            samlMessageIns = decodeMessage(request.getParameter("SAMLResponse"));
+        final String samlMessageEncoded = !Strings.isNullOrEmpty(request.getParameter("SAMLRequest"))
+                ? request.getParameter("SAMLRequest") : request.getParameter("SAMLResponse");
+
+        if (samlMessageEncoded != null) {
+            try (final InputStream samlMessageIns = decodeMessage(samlMessageEncoded)) {
+                final SAMLObject samlMessage = (SAMLObject) unmarshallMessage(samlMessageIns);
+                messageContext.setMessage(samlMessage);
+                log.debug("Decoded SAML message");
+            } catch (final IOException e) {
+                throw new MessageDecodingException("InputStream exception decoding SAML message", e);
+            }
         } else {
             throw new MessageDecodingException(
                     "No SAMLRequest or SAMLResponse query path parameter, invalid SAML 2 HTTP Redirect message");
         }
 
-        final SAMLObject samlMessage = (SAMLObject) unmarshallMessage(samlMessageIns);
-        messageContext.setMessage(samlMessage);
-        log.debug("Decoded SAML message");
-
         populateBindingContext(messageContext);
-        
+
         setMessageContext(messageContext);
     }
 
@@ -136,9 +139,7 @@ public class HTTPRedirectDeflateDecoder extends BaseHttpServletRequestXMLMessage
         }
         
         try {
-            final ByteArrayInputStream bytesIn = new ByteArrayInputStream(decodedBytes);
-            final InflaterInputStream inflater = new InflaterInputStream(bytesIn, new Inflater(true));
-            return inflater;
+            return new NoWrapAutoEndInflaterInputStream(new ByteArrayInputStream(decodedBytes));
         } catch (final Exception e) {
             log.error("Unable to Base64 decode and inflate SAML message", e);
             throw new MessageDecodingException("Unable to Base64 decode and inflate SAML message", e);
@@ -159,4 +160,28 @@ public class HTTPRedirectDeflateDecoder extends BaseHttpServletRequestXMLMessage
         bindingContext.setIntendedDestinationEndpointURIRequired(SAMLBindingSupport.isMessageSigned(messageContext));
     }
     
+    /** A subclass of {@link InflaterInputStream} which defaults in a no-wrap {@link Inflater} instance and
+     * closes it when the stream is closed.
+     */
+    private class NoWrapAutoEndInflaterInputStream extends InflaterInputStream {
+
+        /**
+         * Creates a new input stream with a default no-wrap decompressor and buffer size.
+         *
+         * @param is the input stream
+         */
+        public NoWrapAutoEndInflaterInputStream(final InputStream is) {
+            super(is, new Inflater(true));
+        }
+
+        /** {@inheritDoc} */
+        public void close() throws IOException {
+            if (inf != null) {
+                inf.end();
+            }
+            super.close();
+        }
+
+    }
+
 }
\ No newline at end of file
diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/encoding/impl/HTTPRedirectDeflateEncoder.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/encoding/impl/HTTPRedirectDeflateEncoder.java
index d51471f..239fa3b 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/encoding/impl/HTTPRedirectDeflateEncoder.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/saml2/binding/encoding/impl/HTTPRedirectDeflateEncoder.java
@@ -19,6 +19,7 @@ package org.opensaml.saml.saml2.binding.encoding.impl;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -138,13 +139,15 @@ public class HTTPRedirectDeflateEncoder extends BaseSAML2MessageEncoder {
         try {
             final String messageStr = SerializeSupport.nodeToString(marshallMessage(message));
 
-            final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
-            final Deflater deflater = new Deflater(Deflater.DEFLATED, true);
-            final DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
-            deflaterStream.write(messageStr.getBytes("UTF-8"));
-            deflaterStream.finish();
+            try (final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+                    final DeflaterOutputStream deflaterStream =
+                            new NoWrapAutoEndDeflaterOutputStream(bytesOut, Deflater.DEFLATED)) {
 
-            return Base64Support.encode(bytesOut.toByteArray(), Base64Support.UNCHUNKED);
+                deflaterStream.write(messageStr.getBytes("UTF-8"));
+                deflaterStream.finish();
+
+                return Base64Support.encode(bytesOut.toByteArray(), Base64Support.UNCHUNKED);
+            }
         } catch (final IOException e) {
             throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
         }
@@ -293,4 +296,30 @@ public class HTTPRedirectDeflateEncoder extends BaseSAML2MessageEncoder {
 
         return b64Signature;
     }
+
+    /** A subclass of {@link DeflaterOutputStream} which defaults in a no-wrap {@link Deflater} instance and
+     * closes it when the stream is closed.
+     */
+    private class NoWrapAutoEndDeflaterOutputStream extends DeflaterOutputStream {
+
+        /**
+         * Creates a new output stream with a default no-wrap compressor and buffer size,
+         * and the specified compression level.
+         *
+         * @param os the output stream
+         * @param level the compression level (0-9)
+         */
+        public NoWrapAutoEndDeflaterOutputStream(final OutputStream os, final int level) {
+            super(os, new Deflater(level, true));
+        }
+
+        /** {@inheritDoc} */
+        public void close() throws IOException {
+            if (def != null) {
+                def.end();
+            }
+            super.close();
+        }
+
+    }
 }
\ No newline at end of file

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


More information about the commits mailing list