Example usage for org.apache.commons.codec.binary Base64 encodeBase64

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64.

Prototype

public static byte[] encodeBase64(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:com.rackspacecloud.blueflood.io.serializers.EnumRollupSerializationTest.java

@Test
public void testEnumV1RoundTrip() throws IOException {
    BluefloodEnumRollup e0 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L)
            .withEnumValue("enumValue2", 5L);
    BluefloodEnumRollup e1 = new BluefloodEnumRollup().withEnumValue(
            "t4.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met",
            34454722343L)//w  w  w  .j  a  v a 2s. c o m
            .withEnumValue(
                    "t5.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met",
                    34454722343L)
            .withEnumValue("enumValue2", 10L);
    BluefloodEnumRollup e2 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L)
            .withEnumValue("enumValue1", 1L);
    BluefloodEnumRollup er = BluefloodEnumRollup
            .buildRollupFromEnumRollups(Rollups.asPoints(BluefloodEnumRollup.class, 0, 300, e0, e1, e2));
    Assert.assertEquals(4, er.getCount());
    Map<Long, Long> map = er.getHashedEnumValuesWithCounts();
    Assert.assertTrue(map.get((long) "enumValue1".hashCode()) == 3L);
    Assert.assertTrue(map.get((long) "enumValue2".hashCode()) == 15L);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e0).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e1).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e2).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(er).array()));
    baos.write("\n".getBytes());
    baos.close();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));

    ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee0 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e0, ee0);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee1 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e1, ee1);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee2 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e2, ee2);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee3 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(er, ee3);

    Assert.assertFalse(ee0.equals(ee1));
}

From source file:Crypto.ChiffreDES.java

@Override
public String crypte(String plainTextObj) // passer un String et return des bytes
{
    Security.addProvider(new BouncyCastleProvider());
    String plainText = (String) plainTextObj;
    String textCr = null;// www  .  j a v a 2s .  c  o  m
    byte[] texteCrypte = null;
    byte[] encodedBytes = null;
    try {
        Cipher chiffrement = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");
        chiffrement.init(Cipher.ENCRYPT_MODE, this.Cle.getSecretKey());
        byte[] texteClair = plainText.getBytes();
        byte[] ciphertext = chiffrement.doFinal(texteClair);
        encodedBytes = Base64.encodeBase64(ciphertext);
        System.out.println("EncodedBytes" + new String(encodedBytes));
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchProviderException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    }

    return new String(encodedBytes);
}

From source file:com.rackspacecloud.blueflood.io.serializers.astyanax.EnumRollupSerializationTest.java

@Test
public void testEnumV1RoundTrip() throws IOException {
    BluefloodEnumRollup e0 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L)
            .withEnumValue("enumValue2", 5L);
    BluefloodEnumRollup e1 = new BluefloodEnumRollup().withEnumValue(
            "t4.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met",
            34454722343L)//from   ww  w . j a v a 2 s. c o m
            .withEnumValue(
                    "t5.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met",
                    34454722343L)
            .withEnumValue("enumValue2", 10L);
    BluefloodEnumRollup e2 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L)
            .withEnumValue("enumValue1", 1L);
    BluefloodEnumRollup er = BluefloodEnumRollup
            .buildRollupFromEnumRollups(Rollups.asPoints(BluefloodEnumRollup.class, 0, 300, e0, e1, e2));
    Assert.assertEquals(4, er.getCount());
    Map<Long, Long> map = er.getHashedEnumValuesWithCounts();
    Assert.assertTrue(map.get((long) "enumValue1".hashCode()) == 3L);
    Assert.assertTrue(map.get((long) "enumValue2".hashCode()) == 15L);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(Base64.encodeBase64(Serializers.enumRollupInstance.toByteBuffer(e0).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(Serializers.enumRollupInstance.toByteBuffer(e1).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(Serializers.enumRollupInstance.toByteBuffer(e2).array()));
    baos.write("\n".getBytes());
    baos.write(Base64.encodeBase64(Serializers.enumRollupInstance.toByteBuffer(er).array()));
    baos.write("\n".getBytes());
    baos.close();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));

    ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee0 = Serializers.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e0, ee0);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee1 = Serializers.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e1, ee1);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee2 = Serializers.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(e2, ee2);

    bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodEnumRollup ee3 = Serializers.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb);
    Assert.assertEquals(er, ee3);

    Assert.assertFalse(ee0.equals(ee1));
}

From source file:com.googlecode.jtiger.modules.ecside.util.ECSideUtils.java

public static String encodeFileName(String fileName, String agent) throws UnsupportedEncodingException {
    String codedfilename = null;/*from w w  w.  j  av a 2s  .c om*/
    fileName = fileName.replaceAll("\n|\r", " ").trim();

    if (null != agent && -1 != agent.indexOf("MSIE")) {

        codedfilename = URLEncoder.encode(fileName, "UTF8");
        //         String prefix = fileName.lastIndexOf(".") != -1 ? fileName
        //            .substring(0, fileName.lastIndexOf(".")) : fileName;
        //         String extension = fileName.lastIndexOf(".") != -1 ? fileName
        //            .substring(fileName.lastIndexOf(".")) : "";
        //         int limit = 150 - extension.length();
        //         if (codedfilename.length() > limit) {
        //            codedfilename = java.net.URLEncoder.encode(prefix.substring(0, Math.min(
        //                  prefix.length(), limit / 9)), "UTF-8");
        //            codedfilename = codedfilename + extension;
        //         }
    } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
        codedfilename = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
    } else {
        codedfilename = fileName;
    }
    return codedfilename;
}

From source file:davmail.util.IOUtil.java

/**
 * Base64 encode value./*from  w  w  w.  ja  va 2  s.  c  o  m*/
 *
 * @param value input value
 * @return base64  value
 */
public static byte[] encodeBase64(byte[] value) {
    return Base64.encodeBase64(value);
}

From source file:com.liferay.ide.server.remote.RemoteLogStream.java

protected static InputStream openInputStream(IServerManagerConnection remote, URL url) throws IOException {
    String username = remote.getUsername();
    String password = remote.getPassword();

    String authString = username + ":" + password; //$NON-NLS-1$
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    final IProxyService proxyService = LiferayCore.getProxyService();
    URLConnection conn = null;/*from w ww. j a va  2s.c o m*/

    try {
        URI uri = new URI("HTTP://" + url.getHost() + ":" + url.getPort()); //$NON-NLS-1$ //$NON-NLS-2$
        IProxyData[] proxyDataForHost = proxyService.select(uri);

        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                System.setProperty("http.proxyHost", data.getHost()); //$NON-NLS-1$
                System.setProperty("http.proxyPort", String.valueOf(data.getPort())); //$NON-NLS-1$

                break;
            }
        }

        uri = new URI("SOCKS://" + url.getHost() + ":" + url.getPort()); //$NON-NLS-1$ //$NON-NLS-2$
        proxyDataForHost = proxyService.select(uri);

        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                System.setProperty("socksProxyHost", data.getHost()); //$NON-NLS-1$
                System.setProperty("socksProxyPort", String.valueOf(data.getPort())); //$NON-NLS-1$

                break;
            }
        }
    } catch (URISyntaxException e) {
        LiferayServerCore.logError("Could not read proxy data", e); //$NON-NLS-1$
    }

    conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + authStringEnc); //$NON-NLS-1$ //$NON-NLS-2$
    Authenticator.setDefault(null);
    conn.setAllowUserInteraction(false);

    return conn.getInputStream();
}

From source file:com.eviware.soapui.impl.wsdl.mock.WsdlMockResponseTest.java

@Test
public void testGetResponseContentWithGZIPCompression() throws Exception {
    when(compressedStringConfig.getCompression()).thenReturn("gzip");

    byte[] gZippedBytes = CompressionSupport.compress("gzip", "Awesomeness".getBytes());
    gZippedBytes = Base64.encodeBase64(gZippedBytes);
    when(compressedStringConfig.getStringValue()).thenReturn(new String(gZippedBytes));

    assertThat(mockResponse.getResponseContent(), is("Awesomeness"));
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.OGCEntityResolver.java

@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

    // TODO: temporary basic auth hack
    // protected schema files
    if (systemId != null && systemId.startsWith("https://") && useBasicAuth) {
        HttpResponse response = null;/*from www  .ja  v  a  2 s .  c  o m*/
        LOGGER.debug("resolving protected schema: {}", systemId);
        try {
            HttpGet httpGet = new HttpGet(systemId);

            String basic_auth = new String(Base64.encodeBase64((user + ":" + password).getBytes()));
            httpGet.addHeader("Authorization", "Basic " + basic_auth);

            response = untrustedSslHttpClient.execute(httpGet, new BasicHttpContext());
            String stringFromStream = CharStreams
                    .toString(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            InputSource is = new InputSource(new StringReader(stringFromStream));
            is.setSystemId(systemId);

            return is;

        } catch (IOException ex) {
            ex.printStackTrace();
            LOGGER.error("Error parsing application schema. {}", ex);
            throw new SchemaParseException("Error parsing application schema. {}", ex.getMessage());
        } finally {
            if (response != null) {
                EntityUtils.consumeQuietly(response.getEntity());
            }
        }
    }

    //LOGGER.info(" --- {} --- {} ", systemId, publicId);
    if (publicId != null && publicId.equals("http://www.opengis.net/gml")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://schemas.opengis.net/gml/3.1.1/base/gml.xsd");
        }
    }
    if (publicId != null && publicId.equals("http://www.opengis.net/gml/3.2")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://schemas.opengis.net/gml/3.2.1/gml.xsd");
        }
    }
    if (publicId != null && publicId.equals("http://www.w3.org/1999/xlink")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://www.w3.org/1999/xlink.xsd");
        }
    }

    if (publicId != null && publicId.equals("http://www.aixm.aero/schema/5.1")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://www.aixm.aero/gallery/content/public/schema/5.1/AIXM_Features.xsd");
        }
    }

    if (systemId != null) {
        // Workaround for broken Schema in dwd-WFS
        if (systemId.endsWith("gml.xsd") && systemId.contains("kunden.dwd.de")) {
            return new InputSource("http://schemas.opengis.net/gml/3.2.1/gml.xsd");
        }

        /*if (systemId.endsWith("basicTypes.xsd") && redirect.contains("http://www.opengis.net/gml/3.2")) {
         return new InputSource("http://schemas.opengis.net/gml/3.2.1/basicTypes.xsd");
         }
                
         if (systemId.endsWith("xlinks.xsd") && redirect.contains("http://www.w3.org/1999/xlink")) {
         return new InputSource("http://www.w3.org/1999/xlink.xsd");
         }*/
        // workaround for A4I DescribeFeatureType (seen in 10.2.1)
        // also occurs with native XtraServer, moved to general workarounds
        if (publicId == null
                && systemId.contains("&REQUEST=DescribeFeatureType&TYPENAMES=ns:AbstractFeature")) {
            return createFakeSchema("http://www.opengis.net/gml/3.2");
        }

        // A4I workarounds
        if (systemId.contains("/exts/InspireFeatureDownload/service")) {
            String url = systemId;
            // workaround for A4I 10.1 SP1 (Patch1) blank encoding bug in GET parameters
            if (url.contains("OUTPUT_FORMAT=")) {
                int start = url.indexOf("OUTPUT_FORMAT=") + 13;
                int end = url.indexOf("&", start);
                String out = url.substring(start, end).replaceAll("%20", "");
                url = url.substring(0, start) + out + url.substring(end);
            }

            if (!url.equals(systemId)) {
                LOGGER.debug("original systemId: {}", systemId);
                LOGGER.debug("changed systemId: {}", url);
                return new InputSource(url);
            }
        }
    }

    // ignore multiple imports into the same namespace
    if (publicId != null) {
        if (!uris.containsKey(publicId)) {
            uris.put(publicId, systemId);
        }
        if (systemId != null && !systemId.equals(uris.get(publicId))) {
            return createFakeSchema(publicId);
        }
    }

    return null;
}

From source file:com.networknt.basicauth.BasicAuthHandlerTest.java

private static String encodeCredentialsFullFormat(String username, String password, String separator) {
    String cred;/*from ww  w . j  a v a2  s  .  c o m*/
    if (password != null) {
        cred = username + separator + password;
    } else {
        cred = username;
    }
    String encodedValue;
    byte[] encodedBytes = Base64.encodeBase64(cred.getBytes(UTF_8));
    encodedValue = new String(encodedBytes, UTF_8);
    return encodedValue;
}

From source file:com.networknt.basic.BasicAuthHandlerTest.java

private static String encodeCredentials(String username, String password) {
    String cred;/*from  w w  w. j  a va  2s  . c  om*/
    if (password != null) {
        cred = username + ":" + password;
    } else {
        cred = username;
    }
    String encodedValue;
    byte[] encodedBytes = Base64.encodeBase64(cred.getBytes(UTF_8));
    encodedValue = new String(encodedBytes, UTF_8);
    return encodedValue;
}