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:com.vmware.identity.samlservice.impl.SamlServiceImpl.java

@Override
public String encodeSAMLObject(SignableSAMLObject signableSAMLObject) throws MarshallingException, IOException {
    log.debug("Encoding SAML Object " + signableSAMLObject);

    // Now we must build our representation to put into the html form to be
    // submitted to the idp
    Marshaller marshaller = org.opensaml.Configuration.getMarshallerFactory().getMarshaller(signableSAMLObject);
    org.w3c.dom.Element authDOM = marshaller.marshall(signableSAMLObject);
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);
    String messageXML = rspWrt.toString();

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(messageXML.getBytes("UTF-8"));
    deflaterOutputStream.close();/*from  www  .  ja v a2 s  .c o m*/
    String samlRequestParameter = Shared.encodeBytes(byteArrayOutputStream.toByteArray());
    return samlRequestParameter;
}

From source file:com.osbitools.ws.shared.auth.SamlSecurityProvider.java

private String deflate(byte[] msg) throws IOException {

    Deflater dfl = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(out, dfl);
    dos.write(msg);/*from   w ww .  j  a  va  2s  . c om*/
    dos.close();

    return encode(out.toByteArray(), true);
}

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

private String encodeRequestMessage(RequestAbstractType requestMessage) {

    try {//from   ww  w.  ja  va 2  s.  c  o  m
        DefaultBootstrap.bootstrap();
    } catch (ConfigurationException e) {
        log.error("Error while initializing opensaml library", e);
        return null;
    }
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;
    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);
        return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();

    } catch (MarshallingException e) {
        log.error("Error occurred while encoding SAML request", e);
    } catch (UnsupportedEncodingException e) {
        log.error("Error occurred while encoding SAML request", e);
    } catch (IOException e) {
        log.error("Error occurred while encoding SAML request", e);
    }
    return null;
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static String encodeSAMLRequest(XMLObject authnRequest) throws MarshallingException, IOException {
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(authnRequest);
    Element authDOM = marshaller.marshall(authnRequest);
    StringWriter requestWriter = new StringWriter();
    XMLHelper.writeNode(authDOM, requestWriter);
    String requestMessage = requestWriter.toString();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(requestMessage.getBytes());
    deflaterOutputStream.close();/*  w  w w .  j  a  v  a  2  s .  c o m*/
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim();
    return encodedRequestMessage;
}

From source file:org.codice.ddf.security.common.jaxrs.RestSecurity.java

/**
 * Deflates a value and Base64 encodes the result.
 *
 * @param value value to deflate and Base64 encode
 * @return String//w w  w .  jav  a2  s  .  co m
 * @throws IOException if the value cannot be converted
 */
public static String deflateAndBase64Encode(String value) throws IOException {
    ByteArrayOutputStream valueBytes = new ByteArrayOutputStream();
    try (OutputStream tokenStream = new DeflaterOutputStream(valueBytes,
            new Deflater(Deflater.DEFLATED, GZIP_COMPATIBLE))) {
        tokenStream.write(value.getBytes(StandardCharsets.UTF_8));
        tokenStream.close();

        return Base64.getEncoder().encodeToString(valueBytes.toByteArray());
    }
}

From source file:org.commoncrawl.service.queryserver.master.S3Helper.java

static int scanForGZIPHeader(ByteBuffer byteBuffer) throws IOException {

    LOG.info("*** SCANNING FOR GZIP MAGIC Bytes:" + Byte.toString((byte) StreamingArcFileReader.GZIP_MAGIC)
            + " " + Byte.toString((byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) + " BufferSize is:"
            + byteBuffer.limit() + " Remaining:" + byteBuffer.remaining());
    int limit = byteBuffer.limit();

    while (byteBuffer.position() + 2 < limit) {
        //LOG.info("Reading Byte At:"+ byteBuffer.position());
        int b = byteBuffer.get();
        //LOG.info("First Byte is:"+ b);
        if (b == (byte) (StreamingArcFileReader.GZIP_MAGIC)) {

            byteBuffer.mark();// w ww .  j a v a 2 s  . co m

            byte b2 = byteBuffer.get();
            //LOG.info("Second Byte is:"+ b2);
            if (b2 == (byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) {

                byte b3 = byteBuffer.get();
                if (b3 == Deflater.DEFLATED) {
                    LOG.info("Found GZip Magic at:" + (byteBuffer.position() - 3));
                    return byteBuffer.position() - 3;
                }
            }
            byteBuffer.reset();
        }
    }
    LOG.error("Failed to Find GZIP Magic!!");
    //LOG.error(Arrays.toString(byteBuffer.array()));
    return -1;
}

From source file:org.samlsnort.util.EncodingTool.java

public static String deflateToBase64(String inflated) {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    deflater.setInput(inflated.getBytes(CHARSET));
    deflater.finish();/*from   w ww .  j a  va2 s.  c o  m*/
    byte[] deflatedBytes = new byte[2048];
    int len = deflater.deflate(deflatedBytes);
    return new String(Base64.encodeBase64(Arrays.copyOf(deflatedBytes, len)), CHARSET);
}

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/*w  w  w.  j  a v a  2s. com*/
 * @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.j  a  v  a  2s .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;/* www . j  ava2  s  .  c  om*/
    // 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;
}