Example usage for java.util.zip Deflater DEFLATED

List of usage examples for java.util.zip Deflater DEFLATED

Introduction

In this page you can find the example usage for java.util.zip Deflater DEFLATED.

Prototype

int DEFLATED

To view the source code for java.util.zip Deflater DEFLATED.

Click Source Link

Document

Compression method for the deflate algorithm (the only one currently supported).

Usage

From source file:org.wso2.identity.scenarios.commons.SAML2SSOTestBase.java

/**
 * Base64 encoding of the SAML request./*from   w w  w .ja  v a  2s .c  o  m*/
 *
 * @param requestMessage SAML authentication request.
 * @param binding SAML binding type.
 * @return Base64 encoded SAML request.
 * @throws Exception
 */
protected String encodeRequestMessage(SignableSAMLObject requestMessage, String binding) throws Exception {

    doBootstrap();
    System.setProperty(XML_DOCUMENT_BUILDER_FACTORY, XML_DOCUMENT_BUILDER_FACTORY_IMPL);
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;
    authDOM = marshaller.marshall(requestMessage);
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);

    if (SAMLConstants.SAML2_REDIRECT_BINDING_URI.equals(binding)) {
        //Compress the message, Base 64 encode and URL encode
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        deflaterOutputStream.write(rspWrt.toString().getBytes(Charset.forName(StandardCharsets.UTF_8.name())));
        deflaterOutputStream.close();
        String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                Base64.DONT_BREAK_LINES);
        return URLEncoder.encode(encodedRequestMessage, StandardCharsets.UTF_8.name()).trim();
    } else if (SAMLConstants.SAML2_POST_BINDING_URI.equals(binding)) {
        return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
    } else {
        log.warn("Unsupported SAML2 HTTP Binding. Defaulting to " + SAMLConstants.SAML2_POST_BINDING_URI);
        return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
    }
}