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.apache.hadoop.yarn.server.resourcemanager.scheduler.AppSchedulingInfo.java

public AppSchedulingInfo(ApplicationAttemptId appAttemptId, String user, Queue queue,
        ActiveUsersManager activeUsersManager, long epoch, ResourceUsage appResourceUsage,
        RMContext rmContext) {/* w  w w.ja  va  2  s.c om*/
    this.applicationAttemptId = appAttemptId;
    this.applicationId = appAttemptId.getApplicationId();
    this.queue = queue;
    this.user = user;
    this.activeUsersManager = activeUsersManager;
    this.containerIdCounter = new AtomicLong(epoch << EPOCH_BIT_SHIFT);
    this.appResourceUsage = appResourceUsage;
    try {
        MessageDigest digest = MessageDigest.getInstance(rmContext.getUserFolderHashAlgo());
        byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
        byte[] hashBase = ArrayUtils.addAll(userBytes, rmContext.getSeed());
        byte[] hash = digest.digest(hashBase);
        userFolder = Base64.encodeBase64URLSafeString(hash);
    } catch (NoSuchAlgorithmException ex) {
        LOG.error("error while creating userFolder random string", ex);
        throw new Error("error while creating userFolder random string", ex);
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesAppsModification.java

public void testAppSubmit(String acceptMedia, String contentMedia) throws Exception {

    // create a test app and submit it via rest(after getting an app-id) then
    // get the app details from the rmcontext and check that everything matches

    client().addFilter(new LoggingFilter(System.out));
    String lrKey = "example";
    String queueName = "testqueue";
    String appName = "test";
    String appType = "test-type";
    String urlPath = "apps";
    String appId = testGetNewApplication(acceptMedia);
    List<String> commands = new ArrayList<>();
    commands.add("/bin/sleep 5");
    HashMap<String, String> environment = new HashMap<>();
    environment.put("APP_VAR", "ENV_SETTING");
    HashMap<ApplicationAccessType, String> acls = new HashMap<>();
    acls.put(ApplicationAccessType.MODIFY_APP, "testuser1, testuser2");
    acls.put(ApplicationAccessType.VIEW_APP, "testuser3, testuser4");
    Set<String> tags = new HashSet<>();
    tags.add("tag1");
    tags.add("tag 2");
    CredentialsInfo credentials = new CredentialsInfo();
    HashMap<String, String> tokens = new HashMap<>();
    HashMap<String, String> secrets = new HashMap<>();
    secrets.put("secret1", Base64.encodeBase64String("mysecret".getBytes("UTF8")));
    credentials.setSecrets(secrets);//from  w  ww.j a  va  2s.com
    credentials.setTokens(tokens);
    ApplicationSubmissionContextInfo appInfo = new ApplicationSubmissionContextInfo();
    appInfo.setApplicationId(appId);
    appInfo.setApplicationName(appName);
    appInfo.setMaxAppAttempts(2);
    appInfo.setQueue(queueName);
    appInfo.setApplicationType(appType);
    appInfo.setPriority(0);
    HashMap<String, LocalResourceInfo> lr = new HashMap<>();
    LocalResourceInfo y = new LocalResourceInfo();
    y.setUrl(new URI("http://www.test.com/file.txt"));
    y.setSize(100);
    y.setTimestamp(System.currentTimeMillis());
    y.setType(LocalResourceType.FILE);
    y.setVisibility(LocalResourceVisibility.APPLICATION);
    lr.put(lrKey, y);
    appInfo.getContainerLaunchContextInfo().setResources(lr);
    appInfo.getContainerLaunchContextInfo().setCommands(commands);
    appInfo.getContainerLaunchContextInfo().setEnvironment(environment);
    appInfo.getContainerLaunchContextInfo().setAcls(acls);
    appInfo.getContainerLaunchContextInfo().getAuxillaryServiceData().put("test",
            Base64.encodeBase64URLSafeString("value12".getBytes("UTF8")));
    appInfo.getContainerLaunchContextInfo().setCredentials(credentials);
    appInfo.getResource().setMemory(1024);
    appInfo.getResource().setvCores(1);
    appInfo.setApplicationTags(tags);

    ClientResponse response = this.constructWebResource(urlPath).accept(acceptMedia)
            .entity(appInfo, contentMedia).post(ClientResponse.class);

    if (!this.isAuthenticationEnabled()) {
        assertEquals(Status.UNAUTHORIZED, response.getClientResponseStatus());
        return;
    }
    assertEquals(Status.ACCEPTED, response.getClientResponseStatus());
    assertTrue(!response.getHeaders().getFirst(HttpHeaders.LOCATION).isEmpty());
    String locURL = response.getHeaders().getFirst(HttpHeaders.LOCATION);
    assertTrue(locURL.contains("/apps/application"));
    appId = locURL.substring(locURL.indexOf("/apps/") + "/apps/".length());

    WebResource res = resource().uri(new URI(locURL));
    res = res.queryParam("user.name", webserviceUserName);
    response = res.get(ClientResponse.class);
    assertEquals(Status.OK, response.getClientResponseStatus());

    RMApp app = rm.getRMContext().getRMApps().get(ApplicationId.fromString(appId));
    assertEquals(appName, app.getName());
    assertEquals(webserviceUserName, app.getUser());
    assertEquals(2, app.getMaxAppAttempts());
    if (app.getQueue().contains("root.")) {
        queueName = "root." + queueName;
    }
    assertEquals(queueName, app.getQueue());
    assertEquals(appType, app.getApplicationType());
    assertEquals(tags, app.getApplicationTags());
    ContainerLaunchContext ctx = app.getApplicationSubmissionContext().getAMContainerSpec();
    assertEquals(commands, ctx.getCommands());
    assertEquals(environment, ctx.getEnvironment());
    assertEquals(acls, ctx.getApplicationACLs());
    Map<String, LocalResource> appLRs = ctx.getLocalResources();
    assertTrue(appLRs.containsKey(lrKey));
    LocalResource exampleLR = appLRs.get(lrKey);
    assertEquals(URL.fromURI(y.getUrl()), exampleLR.getResource());
    assertEquals(y.getSize(), exampleLR.getSize());
    assertEquals(y.getTimestamp(), exampleLR.getTimestamp());
    assertEquals(y.getType(), exampleLR.getType());
    assertEquals(y.getPattern(), exampleLR.getPattern());
    assertEquals(y.getVisibility(), exampleLR.getVisibility());
    Credentials cs = new Credentials();
    ByteArrayInputStream str = new ByteArrayInputStream(
            app.getApplicationSubmissionContext().getAMContainerSpec().getTokens().array());
    DataInputStream di = new DataInputStream(str);
    cs.readTokenStorageStream(di);
    Text key = new Text("secret1");
    assertTrue("Secrets missing from credentials object", cs.getAllSecretKeys().contains(key));
    assertEquals("mysecret", new String(cs.getSecretKey(key), "UTF-8"));

    response = this.constructWebResource("apps", appId).accept(acceptMedia).get(ClientResponse.class);
    assertEquals(Status.OK, response.getClientResponseStatus());
}

From source file:org.apache.hive.hcatalog.api.repl.ReplicationUtils.java

/**
 * Command implements Writable, but that's not terribly easy to use compared
 * to String, even if it plugs in easily into the rest of Hadoop. Provide
 * utility methods to easily serialize and deserialize Commands
 *
 * serializeCommand returns a base64 String representation of given command
 *///from   w  w w .  j  av a2 s  . c  o  m
public static String serializeCommand(Command command) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutput dataOutput = new DataOutputStream(baos);
    ReaderWriter.writeDatum(dataOutput, command.getClass().getName());
    command.write(dataOutput);
    return Base64.encodeBase64URLSafeString(baos.toByteArray());
}

From source file:org.apache.hive.jdbc.HiveStatement.java

/**
 * Returns the Yarn ATS GUID.//from  w w w.j  a v  a2s .com
 * This method is a public API for usage outside of Hive, although it is not part of the
 * interface java.sql.Statement.
 * @return Yarn ATS GUID or null if it hasn't been created yet.
 */
public String getYarnATSGuid() {
    if (stmtHandle != null) {
        // Set on the server side.
        // @see org.apache.hive.service.cli.operation.SQLOperation#prepare
        String guid64 = Base64.encodeBase64URLSafeString(stmtHandle.getOperationId().getGuid()).trim();
        return guid64;
    }
    return null;
}

From source file:org.apache.hive.service.cli.operation.SQLOperation.java

/***
 * Compile the query and extract metadata
 * @param sqlOperationConf//from w  w w  . j  a v  a2 s .co m
 * @throws HiveSQLException
 */
public void prepare(HiveConf sqlOperationConf) throws HiveSQLException {
    setState(OperationState.RUNNING);

    try {
        driver = new Driver(sqlOperationConf, getParentSession().getUserName());

        // set the operation handle information in Driver, so that thrift API users
        // can use the operation handle they receive, to lookup query information in
        // Yarn ATS
        String guid64 = Base64
                .encodeBase64URLSafeString(getHandle().getHandleIdentifier().toTHandleIdentifier().getGuid())
                .trim();
        driver.setOperationId(guid64);

        // In Hive server mode, we are not able to retry in the FetchTask
        // case, when calling fetch queries since execute() has returned.
        // For now, we disable the test attempts.
        driver.setTryCount(Integer.MAX_VALUE);

        String subStatement = new VariableSubstitution().substitute(sqlOperationConf, statement);
        response = driver.compileAndRespond(subStatement);
        if (0 != response.getResponseCode()) {
            throw toSQLException("Error while compiling statement", response);
        }

        mResultSchema = driver.getSchema();

        // hasResultSet should be true only if the query has a FetchTask
        // "explain" is an exception for now
        if (driver.getPlan().getFetchTask() != null) {
            //Schema has to be set
            if (mResultSchema == null || !mResultSchema.isSetFieldSchemas()) {
                throw new HiveSQLException("Error compiling query: Schema and FieldSchema "
                        + "should be set when query plan has a FetchTask");
            }
            resultSchema = new TableSchema(mResultSchema);
            setHasResultSet(true);
        } else {
            setHasResultSet(false);
        }
        // Set hasResultSet true if the plan has ExplainTask
        // TODO explain should use a FetchTask for reading
        for (Task<? extends Serializable> task : driver.getPlan().getRootTasks()) {
            if (task.getClass() == ExplainTask.class) {
                resultSchema = new TableSchema(mResultSchema);
                setHasResultSet(true);
                break;
            }
        }
    } catch (HiveSQLException e) {
        setState(OperationState.ERROR);
        throw e;
    } catch (Exception e) {
        setState(OperationState.ERROR);
        throw new HiveSQLException("Error running query: " + e.toString(), e);
    }
}

From source file:org.apache.nifi.processors.standard.util.crypto.CipherUtility.java

public static String encodeBase64NoPadding(final byte[] bytes) {
    String base64UrlNoPadding = Base64.encodeBase64URLSafeString(bytes);
    base64UrlNoPadding = base64UrlNoPadding.replaceAll("-", "+");
    base64UrlNoPadding = base64UrlNoPadding.replaceAll("_", "/");
    return base64UrlNoPadding;
}

From source file:org.apache.nifi.web.security.jwt.JwtServiceTest.java

private String generateHS256Token(String rawHeader, String rawPayload, String hmacSecret, boolean isValid,
        boolean isSigned) {
    try {/* w w  w .  j  av  a 2s . c om*/
        logger.info("Generating token for " + rawHeader + " + " + rawPayload);

        String base64Header = Base64.encodeBase64URLSafeString(rawHeader.getBytes(CharEncoding.UTF_8));
        String base64Payload = Base64.encodeBase64URLSafeString(rawPayload.getBytes(CharEncoding.UTF_8));
        // TODO: Support valid/invalid manipulation

        final String body = base64Header + TOKEN_DELIMITER + base64Payload;

        String signature = generateHMAC(hmacSecret, body);

        return body + TOKEN_DELIMITER + signature;
    } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
        final String errorMessage = "Could not generate the token";
        logger.error(errorMessage, e);
        fail(errorMessage);
        return null;
    }
}

From source file:org.apache.nifi.web.security.jwt.JwtServiceTest.java

private String generateHMAC(String hmacSecret, String body)
        throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
    Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(hmacSecret.getBytes("UTF-8"), "HmacSHA256");
    hmacSHA256.init(secret_key);/*  w w  w . j a  v  a 2 s. c  om*/
    return Base64.encodeBase64URLSafeString(hmacSHA256.doFinal(body.getBytes("UTF-8")));
}

From source file:org.apache.nifi.web.security.otp.OtpService.java

/**
 * Hashes the specified authentication token. The resulting value will be used as the one time use token.
 *
 * @param authenticationToken   the authentication token
 * @return                      the one time use token
 *///w  w  w  .j a v a 2s  .  c  o  m
private String hash(final OtpAuthenticationToken authenticationToken) {
    try {
        // input is the user identity and timestamp
        final String input = authenticationToken.getName() + "-" + System.nanoTime();

        // create the secret using secure random
        final SecureRandom secureRandom = new SecureRandom();
        final byte[] randomBytes = new byte[32];
        secureRandom.nextBytes(randomBytes);
        final SecretKeySpec secret = new SecretKeySpec(randomBytes, HMAC_SHA256); // 256 bit

        // hash the input
        final Mac hmacSha256 = Mac.getInstance(HMAC_SHA256);
        hmacSha256.init(secret);
        final byte[] output = hmacSha256.doFinal(input.getBytes(StandardCharsets.UTF_8));

        // return the result as a base 64 string
        return Base64.encodeBase64URLSafeString(output);
    } catch (final NoSuchAlgorithmException | InvalidKeyException e) {
        final String errorMessage = "There was an error generating the OTP";
        logger.error(errorMessage, e);
        throw new IllegalStateException("Unable to generate single use token.");
    }
}

From source file:org.apache.ofbiz.base.crypto.HashCrypt.java

private static String getCryptedBytes(String hashType, String salt, byte[] bytes) {
    try {/*from w ww  . j av a2 s . c o m*/
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);
        messagedigest.update(salt.getBytes(UtilIO.getUtf8()));
        messagedigest.update(bytes);
        return Base64.encodeBase64URLSafeString(messagedigest.digest()).replace('+', '.');
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralRuntimeException("Error while comparing password", e);
    }
}