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.appserver.webapp.security.utils.SSOUtils.java

/**
 * Encodes the SAML 2.0 based request XML object into its corresponding Base64 notation, based on the type of
 * SAML 2.0 binding./*from   ww w.jav  a2s.com*/
 *
 * @param requestMessage the {@link RequestAbstractType} XML object to be encoded
 * @param binding        the SAML 2.0 binding type
 * @return encoded {@link String} corresponding to the request XML object
 * @throws SSOException if an error occurs while encoding SAML request
 */
public static String encodeRequestMessage(RequestAbstractType requestMessage, String binding)
        throws SSOException {
    Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory()
            .getMarshaller(requestMessage);
    Element authDOM = null;
    try {
        //  marshall this element, and its children, and root them in a newly created Document
        if (marshaller != null) {
            authDOM = marshaller.marshall(requestMessage);
        }
    } catch (MarshallingException e) {
        throw new SSOException(
                "Error occurred while encoding SAML 2.0 Request, failed to marshall the SAML 2.0. "
                        + "Request element XMLObject to its corresponding W3C DOM element",
                e);
    }

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    //  writes the node out to an output stream using the DOM
    if (authDOM != null) {
        SerializeSupport.writeNode(authDOM, outputStream);
    }

    if (SAMLConstants.SAML2_REDIRECT_BINDING_URI.equals(binding)) {
        //  compresses the message using default DEFLATE encoding, Base 64 encode and URL encode
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream,
                deflater)) {
            deflaterOutputStream.write(outputStream.toByteArray());
        } catch (IOException e) {
            throw new SSOException("Error occurred while deflate encoding SAML 2.0 request", e);
        }

        String encodedRequestMessage = Base64Support.encode(byteArrayOutputStream.toByteArray(), false);
        try {
            return URLEncoder.encode(encodedRequestMessage, StandardCharsets.UTF_8.name()).trim();
        } catch (UnsupportedEncodingException e) {
            throw new SSOException("Error occurred while encoding SAML 2.0 request", e);
        }
    } else {
        //  if the binding type encountered is HTTP-POST binding or an unsupported binding type
        return Base64Support.encode(outputStream.toByteArray(), false);
    }
}

From source file:org.wso2.carbon.apim.sso.Util.java

/**
 * @param xmlString String to be encoded
 * @return/*from   w ww . j  av a  2  s. com*/
 */
public static String deflateAndEncode(String xmlString) throws Exception {
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

    deflaterOutputStream.write(xmlString.getBytes());
    deflaterOutputStream.close();

    // Encoding the compressed message
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    return encodedRequestMessage.trim();

}

From source file:org.wso2.carbon.appmgt.gateway.handlers.security.saml2.SAMLUtils.java

/**
 * Returns the marshalled and encoded SAML request.
 *
 * @param request/*  ww  w.j a v a 2  s  . c om*/
 * @return
 * @throws SAMLException
 */
public static String marshallAndEncodeSAMLRequest(RequestAbstractType request) throws SAMLException {

    try {
        String marshalledRequest = SAMLSSOUtil.marshall(request);

        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        deflaterOutputStream.write(marshalledRequest.getBytes("UTF-8"));
        deflaterOutputStream.close();

        String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                Base64.DONT_BREAK_LINES);
        return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();

    } catch (IdentityException e) {
        throw new SAMLException("Can't marshall and encode SAML response", e);
    } catch (IOException e) {
        throw new SAMLException("Can't marshall and encode SAML response", e);
    }
}

From source file:org.wso2.carbon.identity.application.authenticator.samlsso.manager.DefaultSAML2SSOManager.java

private String encodeRequestMessage(RequestAbstractType requestMessage) throws SAMLSSOException {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;/* www .j  av a2 s  .com*/
    try {
        authDOM = marshaller.marshall(requestMessage);

        /* Compress the message */
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        StringWriter rspWrt = new StringWriter();
        XMLHelper.writeNode(authDOM, rspWrt);
        deflaterOutputStream.write(rspWrt.toString().getBytes());
        deflaterOutputStream.close();

        /* Encoding the compressed message */
        String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                Base64.DONT_BREAK_LINES);

        byteArrayOutputStream.write(byteArrayOutputStream.toByteArray());
        byteArrayOutputStream.toString();

        // log saml
        if (log.isDebugEnabled()) {
            log.debug("SAML Request  :  " + rspWrt.toString());
        }

        return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();

    } catch (MarshallingException e) {
        throw new SAMLSSOException("Error occurred while encoding SAML request", e);
    } catch (UnsupportedEncodingException e) {
        throw new SAMLSSOException("Error occurred while encoding SAML request", e);
    } catch (IOException e) {
        throw new SAMLSSOException("Error occurred while encoding SAML request", e);
    }
}

From source file:org.wso2.carbon.identity.auth.saml2.common.SAML2AuthUtils.java

public static String encodeForRedirect(RequestAbstractType requestMessage) {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;//from w w  w  .  j a  v  a 2 s .  c  o  m
    try {
        authDOM = marshaller.marshall(requestMessage);

        /* Compress the message */
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        StringWriter rspWrt = new StringWriter();
        XMLHelper.writeNode(authDOM, rspWrt);
        deflaterOutputStream.write(rspWrt.toString().getBytes(StandardCharsets.UTF_8));
        deflaterOutputStream.close();

        /* Encoding the compressed message */
        String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                Base64.DONT_BREAK_LINES);

        byteArrayOutputStream.write(byteArrayOutputStream.toByteArray());
        byteArrayOutputStream.toString(StandardCharsets.UTF_8.toString());

        // logger saml
        if (logger.isDebugEnabled()) {
            logger.debug("SAML Request  :  " + rspWrt.toString());
        }

        return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();

    } catch (MarshallingException | IOException e) {
        throw new IdentityRuntimeException("Error occurred while encoding SAML request", e);
    }
}

From source file:org.wso2.carbon.identity.saml.inbound.util.SAMLSSOUtil.java

/**
 * Compresses the response String/*from  www .j  a  v a 2  s  .co m*/
 *
 * @param response
 * @return
 * @throws IOException
 */
public static String compressResponse(String response) throws IOException {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    try {
        deflaterOutputStream.write(response.getBytes(StandardCharsets.UTF_8));
    } finally {
        deflaterOutputStream.close();
    }
    return Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
}

From source file:org.wso2.carbon.identity.sso.agent.saml.SAML2SSOManager.java

protected String encodeRequestMessage(RequestAbstractType requestMessage, String binding)
        throws SSOAgentException {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;//from  ww  w .j  a  v  a 2 s  .c  om
    try {
        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("UTF-8")));
            deflaterOutputStream.close();
            String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                    Base64.DONT_BREAK_LINES);
            return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();
        } else if (SAMLConstants.SAML2_POST_BINDING_URI.equals(binding)) {
            return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
        } else {
            LOGGER.log(Level.FINE,
                    "Unsupported SAML2 HTTP Binding. Defaulting to " + SAMLConstants.SAML2_POST_BINDING_URI);
            return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
        }
    } catch (MarshallingException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    } catch (UnsupportedEncodingException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    } catch (IOException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    }
}

From source file:org.wso2.carbon.identity.sso.saml.SAMLTestRequestBuilder.java

public static String encodeRequestMessage(RequestAbstractType requestMessage)
        throws MarshallingException, IOException, ConfigurationException {
    DefaultBootstrap.bootstrap();//from ww w. j  a v  a 2s. c  o  m
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
            "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;
    authDOM = marshaller.marshall(requestMessage);

    /* Compress the message */
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);
    deflaterOutputStream.write(rspWrt.toString().getBytes());
    deflaterOutputStream.close();

    /* Encoding the compressed message */
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);

    byteArrayOutputStream.write(byteArrayOutputStream.toByteArray());
    byteArrayOutputStream.toString();

    return encodedRequestMessage;
}

From source file:org.wso2.carbon.identity.sso.saml.tomcat.agent.SSOManager.java

private String encodeRequestMessage(RequestAbstractType requestMessage)
        throws MarshallingException, IOException {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = marshaller.marshall(requestMessage);

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);
    deflaterOutputStream.write(rspWrt.toString().getBytes());
    deflaterOutputStream.close();/*from www .j a va 2 s .com*/

    /* Encoding the compressed message */
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil.java

/**
 * Compresses the response String//from   ww w.  j  a va2  s . co  m
 *
 * @param response
 * @return
 * @throws IOException
 */
public static String compressResponse(String response) throws IOException {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    try {
        deflaterOutputStream.write(response.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
    } finally {
        deflaterOutputStream.close();
    }
}