Example usage for java.util.zip DeflaterOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:org.openmrs.module.shr.contenthandler.api.ContentTest.java

private static byte[] compressDeflate(String content) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream deflateOut = new DeflaterOutputStream(out, new Deflater(0, true));
    deflateOut.write(content.getBytes());
    IOUtils.closeQuietly(deflateOut);//from w ww.j a  v  a 2s.  c  om
    return out.toByteArray();
}

From source file:org.openmrs.module.shr.contenthandler.api.ContentTest.java

private static byte[] compressZLib(String content) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream zlibOut = new DeflaterOutputStream(out);
    zlibOut.write(content.getBytes());
    IOUtils.closeQuietly(zlibOut);//w w w  .jav  a  2  s . c  o m
    return out.toByteArray();
}

From source file:org.simbasecurity.core.saml.SAMLServiceImpl.java

protected String encodeSAMLRequest(byte[] pSAMLRequest) throws RuntimeException {
    Base64 base64Encoder = new Base64();

    try {// w ww.jav  a 2s.  c om
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);

        DeflaterOutputStream def = new DeflaterOutputStream(byteArray, deflater);
        def.write(pSAMLRequest);
        def.close();
        byteArray.close();

        String stream = new String(base64Encoder.encode(byteArray.toByteArray()));

        return stream.trim();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.slc.sli.api.security.saml.SamlHelper.java

/**
 * Converts plain-text xml to SAML-spec compliant string for HTTP-Redirect binding
 *
 * @param xml/*from www .  j  av  a  2  s  . co m*/
 * @return
 * @throws IOException
 */
private String xmlToEncodedString(String xml) throws IOException {
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(xml.getBytes());
    deflaterOutputStream.close();
    String base64 = Base64.encodeBase64String(byteArrayOutputStream.toByteArray());
    return URLEncoder.encode(base64, "UTF-8");
}

From source file:org.socraticgrid.workbench.security.wso2.Saml2Util.java

/**
 * Compressing and Encoding the response
 *
 * @param xmlString String to be encoded
 * @return compressed and encoded String
 *//*from ww w. java  2 s  . c  o  m*/
public static String encode(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.socraticgrid.workbench.security.wso2.SamlConsumer.java

@SuppressWarnings("deprecation")
private String encodeAuthnRequest(AuthnRequest authnRequest) throws MarshallingException, IOException {
    String requestMessage;//from   w w w. ja  v a  2s .  co m
    // Pass authnRequest object to a DOM element
    Marshaller marshaller = org.opensaml.Configuration.getMarshallerFactory().getMarshaller(authnRequest);
    org.w3c.dom.Element authDOM = null;

    authDOM = marshaller.marshall(authnRequest);

    // Get the string
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);
    requestMessage = rspWrt.toString();

    // DEFLATE compression of the message, byteArrayOutputStream will holds
    // the compressed bytes
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

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

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

    return encodedRequestMessage;
}

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

/**
 * @param xmlString String to be encoded
 * @return//from w w w  .ja  v  a2 s.c  om
 */
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//from   w  w 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  a  va 2  s .c  om*/
    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 .  jav a2  s  .c om*/
    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);
    }
}