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

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

Introduction

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

Prototype

public Base64(final int lineLength) 

Source Link

Document

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

Usage

From source file:com.leclercb.commons.api.license.LicenseManager.java

public void writeLicense(License license, File file) throws Exception {
    byte[] message = license.licenseToString().getBytes("UTF-8");
    byte[] signature = this.encryptionManager.sign(message);
    byte[] data = ArrayUtils.addAll(signature, message);

    Base64 base64 = new Base64(40);
    FileUtils.writeByteArrayToFile(file, base64.encode(data));
}

From source file:com.buddycloud.mediaserver.download.DownloadImageTest.java

@Test
public void downloadImageParamAuth() throws Exception {
    Base64 encoder = new Base64(true);
    String authStr = BASE_USER + ":" + BASE_TOKEN;

    ClientResource client = new ClientResource(URL + "?auth=" + new String(encoder.encode(authStr.getBytes())));

    File file = new File(TEST_OUTPUT_DIR + File.separator + "downloaded.jpg");
    FileOutputStream outputStream = FileUtils.openOutputStream(file);
    client.get().write(outputStream);// w  w w.j a  v  a 2s .  c  om

    Assert.assertTrue(file.exists());

    // Delete downloaded file
    FileUtils.deleteDirectory(new File(TEST_OUTPUT_DIR));
    outputStream.close();
}

From source file:com.buddycloud.mediaserver.download.DownloadVideoTest.java

@Test
public void downloadVideoParamAuth() throws Exception {
    Base64 encoder = new Base64(true);
    String authStr = BASE_USER + ":" + BASE_TOKEN;

    ClientResource client = new ClientResource(URL + "?auth=" + new String(encoder.encode(authStr.getBytes())));

    File file = new File(TEST_OUTPUT_DIR + File.separator + "downloaded.avi");
    FileOutputStream outputStream = FileUtils.openOutputStream(file);
    client.get().write(outputStream);/* w  w w.  j a va 2 s.c  om*/

    Assert.assertTrue(file.exists());

    // Delete downloaded file
    FileUtils.deleteDirectory(new File(TEST_OUTPUT_DIR));
    outputStream.close();
}

From source file:com.saggezza.jtracker.track.PayloadMapC.java

private String base64encode(String string) throws UnsupportedEncodingException {
    Base64 b64 = new Base64(true);
    String safe_str = b64.encodeBase64String(string.getBytes(Charset.forName("US-ASCII")));
    return safe_str;
}

From source file:com.bigstep.datalake.KerberosIdentityAuthenticator.java

/**
 * Performs SPNEGO authentication against the specified URL.
 * <p>//from   w w w  .j  av a  2  s  .  co m
 * If a token is given it does a NOP and returns the given token.
 * <p>
 * If no token is given, it will perform the SPNEGO authentication sequence using an
 * HTTP <code>OPTIONS</code> request.
 *
 * @param url   the URl to authenticate against.
 * @param token the authentication token being used for the user.
 * @throws IOException             if an IO error occurred.
 * @throws AuthenticationException if an authentication error occurred.
 */
@Override
public void authenticate(URL url, AuthenticatedURL.Token token) throws IOException, AuthenticationException {
    if (!token.isSet()) {
        this.url = url;
        base64 = new Base64(0);
        conn = (HttpURLConnection) url.openConnection();
        if (connConfigurator != null) {
            conn = connConfigurator.configure(conn);
        }
        conn.setRequestMethod(AUTH_HTTP_METHOD);
        conn.connect();

        boolean needFallback = false;
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            LOG.debug("JDK performed authentication on our behalf.");
            // If the JDK already did the SPNEGO back-and-forth for
            // us, just pull out the token.
            AuthenticatedURL.extractToken(conn, token);
            if (isTokenKerberos(token)) {
                return;
            }
            needFallback = true;
        }
        if (!needFallback && isNegotiate()) {
            LOG.debug("Performing our own SPNEGO sequence.");
            doSpnegoSequence(token);
        } else {
            LOG.debug("Using fallback authenticator sequence.");
            Authenticator auth = getFallBackAuthenticator();
            // Make sure that the fall back authenticator have the same
            // ConnectionConfigurator, since the method might be overridden.
            // Otherwise the fall back authenticator might not have the information
            // to make the connection (e.g., SSL certificates)
            auth.setConnectionConfigurator(connConfigurator);
            auth.authenticate(url, token);
        }
    }
}

From source file:com.vmware.o11n.plugin.crypto.model.CryptoUtil.java

/**
 * PEM encode a certificate/*from  w  w w.j  av a 2  s.com*/
 *
 * @param pubKey Public
 * @return PEM encoded certificate string
 * @throws CertificateEncodingException
 */
public static String pemEncode(Certificate cert) throws CertificateEncodingException {
    String toReturn;
    if (cert instanceof X509Certificate) {
        final String certHeader = FIVE_DASH + "BEGIN CERTIFICATE" + FIVE_DASH;
        final String certFooter = FIVE_DASH + "END CERTIFICATE" + FIVE_DASH;
        Base64 encoder = new Base64(64);
        toReturn = String.join("\n", certHeader, new String(encoder.encode(cert.getEncoded())), certFooter);
    } else {
        throw new UnsupportedOperationException(
                "Unknown certificate type.  Only implemented for X509Certificate.");
    }
    return CryptoUtil.fixPemString(toReturn);
}

From source file:com.buddycloud.mediaserver.download.DownloadAvatarTest.java

@Test
public void anonymousSuccessfulDownloadParamAuth() throws Exception {
    Base64 encoder = new Base64(true);
    String authStr = BASE_USER + ":" + BASE_TOKEN;

    ClientResource client = new ClientResource(URL + "?auth=" + new String(encoder.encode(authStr.getBytes())));

    File file = new File(TEST_OUTPUT_DIR + File.separator + "downloadedAvatar.jpg");
    FileOutputStream outputStream = FileUtils.openOutputStream(file);
    client.get().write(outputStream);//from   ww w.j  a  va  2 s. c om

    Assert.assertTrue(file.exists());

    // Delete downloaded file
    FileUtils.deleteDirectory(new File(TEST_OUTPUT_DIR));
    outputStream.close();
}

From source file:com.buddycloud.mediaserver.download.DownloadMediasInfoTest.java

@Test
public void downloadMediasInfoParamAuth() throws Exception {
    Base64 encoder = new Base64(true);
    String authStr = BASE_USER + ":" + BASE_TOKEN;

    ClientResource client = new ClientResource(URL + "?auth=" + new String(encoder.encode(authStr.getBytes())));

    Representation result = client.get(MediaType.APPLICATION_JSON);
    List<Media> medias = gson.fromJson(result.getText(), new TypeToken<List<Media>>() {
    }.getType());//from  w  ww .  j a  v a  2s.  c  o  m

    assertTrue(medias.size() == 2);
}

From source file:com.buddycloud.mediaserver.update.UpdateMediaTest.java

@Test
public void anonymousSuccessfulUpdateParamAuth() throws Exception {
    // file fields
    String title = "New Image";
    String description = "New Description";

    Base64 encoder = new Base64(true);
    String authStr = BASE_USER + ":" + BASE_TOKEN;

    ClientResource client = new ClientResource(URL + "?auth=" + new String(encoder.encode(authStr.getBytes())));

    Form form = createWebForm(null, title, description, null, null);

    Representation result = client.post(form);
    Media media = gson.fromJson(result.getText(), Media.class);

    // verify if resultant media has the passed attributes
    assertEquals(title, media.getTitle());
    assertEquals(description, media.getDescription());
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
    cert.checkValidity();/*  w  w  w.  jav a2  s .  c  om*/
    String signature = getSignature(key);
    document.setSello(signature);
    byte[] bytes = cert.getEncoded();
    Base64 b64 = new Base64(-1);
    String certStr = b64.encodeToString(bytes);
    document.setCertificado(certStr);
    BigInteger bi = cert.getSerialNumber();
    document.setNoCertificado(new String(bi.toByteArray()));
}