[java-opensaml] branch maint-3.4 updated: OSJ-284: HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder ...
Brent Putman
putmanb at georgetown.edu
Wed Sep 25 18:14:25 EDT 2019
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch maint-3.4
in repository java-opensaml.
View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=7b8d137fd6537cf6e1c95a4113931892e7d33eb7
The following commit(s) were added to refs/heads/maint-3.4 by this push:
new 7b8d137 OSJ-284: HTTPRedirectDeflateDecoder and HTTPRedirectDeflateEncoder ...
7b8d137 is described below
commit 7b8d137fd6537cf6e1c95a4113931892e7d33eb7
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 4d68159..3984a66 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;
@@ -99,22 +100,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);
}
@@ -137,9 +140,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);
@@ -160,4 +161,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 b13a606..77d9f2e 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;
@@ -135,13 +136,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);
}
@@ -291,4 +294,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