Example usage for java.util.zip DeflaterOutputStream close

List of usage examples for java.util.zip DeflaterOutputStream close

Introduction

In this page you can find the example usage for java.util.zip DeflaterOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

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 www .j  a v a2 s . c o m*/
    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 w  ww.j a  v  a2 s.  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();

    /* Encoding the compressed message */
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);/*  w w  w.j a v a  2  s  .  c  om*/
    return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();
}

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

/**
 * Compresses the response String//from  w  ww.  jav  a  2 s. c om
 *
 * @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();
    }
}

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

/**
 * Base64 encoding of the SAML request.//w  ww  .j a va  2s.co 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);
    }
}

From source file:rascal.storage.loose.AbstractLooseStorageNodeChannelIntegrationTest.java

@Before
public void setUpTestData() throws IOException {
    testData = RandomTestDataUtils.createRandomData();
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DeflaterOutputStream out = new DeflaterOutputStream(outBuffer, new Deflater(DEFLATED_DATA_COMPRESS_LEVER));
    out.write(testData);/*from w ww  .j av a2 s.  co m*/
    out.close();
    deflatedTestData = outBuffer.toByteArray();
}

From source file:util.RequestUtil.java

/**
 * Generates an encoded and compressed String from the specified XML-formatted
 * String. The String is encoded in the following order:
 * <p>/*from  www  . ja va  2 s .  c  om*/
 * 1. URL encode <br>
 * 2. Base64 encode <br>
 * 3. Deflate <br>
 * 
 * @param xmlString XML-formatted String that is to be encoded
 * @return String containing the encoded contents of the specified XML String
 */
public static String encodeMessage(String xmlString) throws IOException, UnsupportedEncodingException {
    // first DEFLATE compress the document (saml-bindings-2.0,
    // section 3.4.4.1)
    byte[] xmlBytes = xmlString.getBytes("UTF-8");
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream);
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();

    // next, base64 encode it
    Base64 base64Encoder = new Base64();
    byte[] base64EncodedByteArray = base64Encoder.encode(byteOutputStream.toByteArray());
    String base64EncodedMessage = new String(base64EncodedByteArray);

    // finally, URL encode it
    String urlEncodedMessage = URLEncoder.encode(base64EncodedMessage, "UTF-8");

    return urlEncodedMessage;
}

From source file:weave.servlets.GenericServlet.java

protected void serializeCompressedAmf3(Object objToSerialize, ServletOutputStream servletOutputStream) {
    try {//from w w w  .  j a  v  a 2s  . co  m
        SerializationContext context = getSerializationContext();

        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(servletOutputStream);

        Amf3Output amf3Output = new Amf3Output(context);
        amf3Output.setOutputStream(deflaterOutputStream); // compress
        amf3Output.writeObject(objToSerialize);
        amf3Output.flush();

        deflaterOutputStream.close(); // this is necessary to finish the compression

        /*
         * Do not call amf3Output.close() because that will
         * send a 'reset' packet and cause the response to fail.
         * 
         * http://viveklakhanpal.wordpress.com/2010/07/01/error-2032ioerror/
         */
    } catch (Exception e) {
        e.printStackTrace();
    }
}