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

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

Introduction

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

Prototype

public static String encodeBase64URLSafeString(final byte[] binaryData) 

Source Link

Document

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.

Usage

From source file:org.nordapp.web.servlet.AbstractControlImpl.java

/**
 * Generates the public key of a certificate into certID
 */// ww w  . j  av  a  2 s. co  m
public void generateCert() {
    UUID uuid = UUID.randomUUID();
    byte[] buffer = ByteBuffer.allocate(16).putLong(0, uuid.getMostSignificantBits())
            .putLong(8, uuid.getLeastSignificantBits()).array();

    field0 = buffer;
    certID = Base64.encodeBase64URLSafeString(buffer);
}

From source file:org.nordapp.web.servlet.AbstractControlImpl.java

/**
 * Converts the buffer to an URL save base 64 String.
 * /*from   w ww . j  av  a 2  s.c  o  m*/
 * @param buffer The byte array to convert
 * @return The converted buffer as an URL save base 64 String
 */
public String toBase64(byte[] buffer) {
    return Base64.encodeBase64URLSafeString(buffer);
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Safely encode a string for use as a query parameter.
 * //from  w w w.  ja va 2 s. c om
 * @param rawString
 * @return encoded string
 */
public static String safeEncode(String rawString) {
    if (rawString == null || rawString.length() == 0) {
        return null;
    }

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(rawString.getBytes(CharEncoding.UTF_8));
        gzip.finish();
        gzip.close();
        String candidate = Base64.encodeBase64URLSafeString(out.toByteArray());
        return candidate;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    }
}

From source file:org.openinfinity.core.crypto.CryptoSupport.java

/**
 * Encrypts input bytes and encodes Base64 presentation of the encrypted bytes.
 * /*from  ww  w.j  a  va  2  s . c o  m*/
 * @param input Represents the plain input byte array.
 * @param encoding Represents the character encoding for byte array.
 * @return String Represents the Base64 encoded String.
 */
public String encryptAndReturnBase64Presentation(byte[] input, String encoding) {
    ObjectInputStream objectInputStream = null;
    ByteArrayInputStream byteArrayInputStream = null;
    try {
        byteArrayInputStream = new ByteArrayInputStream(input);
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Object plainObject = objectInputStream.readObject();
        if (plainObject instanceof String) {
            String stringObject = (String) plainObject;
            byte[] encodedBytes = stringObject.getBytes(encoding);
            byte[] encryptedBytes = crypter.encrypt(encodedBytes);
            return Base64.encodeBase64URLSafeString(encryptedBytes);
        }
    } catch (Throwable throwable) {
        ExceptionUtil.throwSystemException("Encryption failed: " + throwable.getMessage(), throwable);
    } finally {
        IOUtil.closeStream(byteArrayInputStream);
        IOUtil.closeStream(objectInputStream);
    }
    return null;
}

From source file:org.orcid.core.cli.Base64Encode.java

public static void main(String[] args) {
    try {/*from  w  ww.  j  a v  a  2  s.c  o m*/
        System.out.println(Base64.encodeBase64URLSafeString(args[0].getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.orcid.core.manager.impl.AdminManagerImpl.java

/**
 * Encrypts a string// ww w .  j a v a 2s.  co  m
 * 
 * @param unencrypted
 * @return the string encrypted
 * */
private String generateEncryptedLink(String trusted, String managed) {
    Map<String, String> params = new HashMap<String, String>();
    params.put(TRUSTED_USER_PARAM, trusted);
    params.put(MANAGED_USER_PARAM, managed);
    String paramsString = JSON.toString(params);
    if (StringUtils.isNotBlank(paramsString)) {
        String encryptedParams = encryptionManager.encryptForExternalUse(paramsString);
        String base64EncodedParams = Base64.encodeBase64URLSafeString(encryptedParams.getBytes());
        String url = orcidUrlManager.getBaseUrl() + AUTHORIZE_DELEGATION_ACTION + "?key=" + base64EncodedParams;
        return url;
    } else {
        return null;
    }
}

From source file:org.orcid.core.manager.impl.EmailMessageSenderImpl.java

@Override
public EmailMessage createDigest(OrcidProfile orcidProfile, Collection<Notification> notifications,
        Locale locale) {// w  w w  .  jav a 2  s.  c om
    int totalMessageCount = 0;
    int orcidMessageCount = 0;
    int addActivitiesMessageCount = 0;
    int amendedMessageCount = 0;
    int activityCount = 0;
    Set<String> memberIds = new HashSet<>();
    DigestEmail digestEmail = new DigestEmail();
    for (Notification notification : notifications) {
        digestEmail.addNotification(notification);
        totalMessageCount++;
        if (notification.getSource() == null) {
            orcidMessageCount++;
        } else {
            SourceClientId clientId = notification.getSource().getSourceClientId();
            if (clientId != null) {
                memberIds.add(clientId.getPath());
            }
        }
        if (notification instanceof NotificationPermission) {
            addActivitiesMessageCount++;
            NotificationPermission permissionNotification = (NotificationPermission) notification;
            activityCount += permissionNotification.getItems().getItems().size();
            String encryptedPutCode = encryptionManager
                    .encryptForExternalUse(String.valueOf(permissionNotification.getPutCode()));
            try {
                permissionNotification.setEncryptedPutCode(
                        Base64.encodeBase64URLSafeString(encryptedPutCode.getBytes("UTF-8")));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(
                        "Problem base 64 encoding notification put code for notification id = "
                                + permissionNotification.getPutCode(),
                        e);
            }
        }
        if (notification instanceof NotificationAmended) {
            amendedMessageCount++;
        }
    }
    String emailName = notificationManager.deriveEmailFriendlyName(orcidProfile);
    String subject = messages.getMessage("email.subject.digest",
            new String[] { emailName, String.valueOf(totalMessageCount) }, locale);
    Map<String, Object> params = new HashMap<>();
    params.put("locale", locale);
    params.put("messages", messages);
    params.put("messageArgs", new Object[0]);
    params.put("orcidProfile", orcidProfile);
    params.put("emailName", emailName);
    params.put("digestEmail", digestEmail);
    params.put("frequency", orcidProfile.getOrcidInternal().getPreferences().getSendEmailFrequencyDays());
    params.put("totalMessageCount", String.valueOf(totalMessageCount));
    params.put("orcidMessageCount", orcidMessageCount);
    params.put("addActivitiesMessageCount", addActivitiesMessageCount);
    params.put("activityCount", activityCount);
    params.put("amendedMessageCount", amendedMessageCount);
    params.put("memberIdsCount", memberIds.size());
    params.put("baseUri", orcidUrlManager.getBaseUrl());
    params.put("subject", subject);
    String bodyText = templateManager.processTemplate("digest_email.ftl", params, locale);
    String bodyHtml = templateManager.processTemplate("digest_email_html.ftl", params, locale);
    EmailMessage emailMessage = new EmailMessage();

    emailMessage.setSubject(subject);
    emailMessage.setBodyText(bodyText);
    emailMessage.setBodyHtml(bodyHtml);
    return emailMessage;

}

From source file:org.orcid.core.manager.impl.NotificationManagerImpl.java

@Override
public void sendOrcidDeactivateEmail(OrcidProfile orcidToDeactivate) {
    // Create verification url

    Map<String, Object> templateParams = new HashMap<String, Object>();

    String subject = getSubject("email.subject.deactivate", orcidToDeactivate);
    String email = orcidToDeactivate.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue();
    String encryptedEmail = encryptionManager.encryptForExternalUse(email);
    String base64EncodedEmail = Base64.encodeBase64URLSafeString(encryptedEmail.getBytes());
    String deactivateUrlEndpointPath = "/account/confirm-deactivate-orcid";

    String emailFriendlyName = deriveEmailFriendlyName(orcidToDeactivate);
    templateParams.put("emailName", emailFriendlyName);
    templateParams.put("orcid", orcidToDeactivate.getOrcidIdentifier().getPath());
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("deactivateUrlEndpoint", deactivateUrlEndpointPath + "/" + base64EncodedEmail);
    templateParams.put("deactivateUrlEndpointUrl", deactivateUrlEndpointPath);
    templateParams.put("subject", subject);

    addMessageParams(templateParams, orcidToDeactivate);

    // Generate body from template
    String body = templateManager.processTemplate("deactivate_orcid_email.ftl", templateParams);
    // Generate html from template
    String html = templateManager.processTemplate("deactivate_orcid_email_html.ftl", templateParams);

    mailGunManager.sendEmail(DEACTIVATE_NOTIFY_ORCID_ORG, email, subject, body, html);
}

From source file:org.orcid.core.manager.impl.NotificationManagerImpl.java

public String createEmailBaseUrl(String unencryptedParams, String baseUri, String path) {
    // Encrypt and encode params
    String encryptedUrlParams = encryptionManager.encryptForExternalUse(unencryptedParams);
    String base64EncodedParams = null;
    try {/*from   www.  jav a 2s. c  o m*/
        base64EncodedParams = Base64.encodeBase64URLSafeString(encryptedUrlParams.getBytes("UTF-8"));

    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
    return String.format("%s/%s/%s", baseUri, path, base64EncodedParams);
}

From source file:org.panbox.desktop.common.urihandler.PanboxURICmdImportIdentity.java

public static URI getPanboxLink(URI uri, byte[] checksum) {
    StringBuilder b = new StringBuilder();
    b.append(NAME);/*  www .  ja v  a 2s .c o  m*/
    b.append("?");
    b.append(Base64.encodeBase64URLSafeString(
            uri.toString().getBytes(Charset.forName(PanboxConstants.STANDARD_CHARSET))));
    b.append(":");
    b.append(Base64.encodeBase64URLSafeString(checksum));
    return URI.create(PanboxConstants.PANBOX_URL_PREFIX + b.toString());
}