public static <T> T get(Map<String, String> conf, String key, Codec<T> codec) throws IOException { String value = conf.get(key); if (value == null) { LOG.trace("De-serializing {} with null value", key); return codec.decode(null); } byte[] encoded = Base64.decodeBase64(value); LOG.trace("De-serializing {} {}", key, Bytes.toStringBinary(encoded)); return codec.decode(encoded); }
/** * * @param accessToken is the access token from Authorization header in HTTP Request * @return the serialized access token identifer * @throws IOException */ public AccessTokenIdentifierPair transform(String accessToken) throws IOException { byte[] decodedAccessToken = Base64.decodeBase64(accessToken); AccessToken accessTokenObj = accessTokenCodec.decode(decodedAccessToken); AccessTokenIdentifier accessTokenIdentifierObj = accessTokenObj.getIdentifier(); byte[] encodedAccessTokenIdentifier = accessTokenIdentifierCodec.encode(accessTokenIdentifierObj); return new AccessTokenIdentifierPair(Base64.encodeBase64String(encodedAccessTokenIdentifier).trim(), accessTokenIdentifierObj); }
public static <T> void set(Map<String, String> conf, String key, Codec<T> codec, T obj) throws IOException { byte[] encoded = codec.encode(obj); LOG.trace("Serializing {} {}", key, Bytes.toStringBinary(encoded)); conf.put(key, Base64.encodeBase64String(encoded)); }
public static <T> void set(Map<String, String> conf, String key, Codec<T> codec, T obj) throws IOException { byte[] encoded = codec.encode(obj); LOG.trace("Serializing {} {}", key, Bytes.toStringBinary(encoded)); conf.put(key, Base64.encodeBase64String(encoded)); }
public static <T> T get(Configuration conf, String key, Codec<T> codec) throws IOException { String value = conf.get(key); if (value == null) { LOG.trace("De-serializing {} with null value", key); return codec.decode(null); } byte[] encoded = Base64.decodeBase64(value); LOG.trace("De-serializing {} {}", key, Bytes.toStringBinary(encoded)); return codec.decode(encoded); }
@Override public void doInit() throws IOException { File keyFile = new File(keyFilePath); String keyFileDirectory = keyFile.getParent(); File keyFileDir = new File(keyFileDirectory); // Create directory for keyfile if it doesn't exist already. if (!keyFileDir.exists() && !keyFileDir.mkdir()) { throw new IOException("Failed to create directory " + keyFileDirectory + " for keyfile storage."); } else { Preconditions.checkState(keyFileDir.isDirectory(), "Configured keyFile directory " + keyFileDirectory + " is not a directory!"); Preconditions.checkState(keyFileDir.canRead(), "Configured keyFile directory " + keyFileDirectory + " exists but is not readable!"); } // Read existing key from file. if (keyFile.exists()) { KeyIdentifier storedKey = keyIdentifierCodec.decode(Files.toByteArray(keyFile)); this.currentKey = storedKey; // the file-based key is considered valid forever allKeys.put(storedKey.getKeyId(), storedKey); } else { Preconditions.checkState(keyFileDir.canWrite(), "Configured keyFile directory " + keyFileDirectory + " exists but is not writable!"); // Create a new key and write to file. generateKey(); keyFile.createNewFile(); Files.write(keyIdentifierCodec.encode(currentKey), keyFile); } }
public static <T> void set(Configuration conf, String key, Codec<T> codec, T obj) throws IOException { byte[] encoded = codec.encode(obj); LOG.trace("Serializing {} {}", key, Bytes.toStringBinary(encoded)); conf.set(key, Base64.encodeBase64String(encoded)); }
public static <T> T get(Map<String, String> conf, String key, Codec<T> codec) throws IOException { String value = conf.get(key); if (value == null) { LOG.trace("De-serializing {} with null value", key); return codec.decode(null); } byte[] encoded = Base64.decodeBase64(value); LOG.trace("De-serializing {} {}", key, Bytes.toStringBinary(encoded)); return codec.decode(encoded); }
@Test public void testKeyIdentifierSerialization() throws Exception { SecretKey nextKey = keyGenerator.generateKey(); Random random = new Random(); KeyIdentifier keyIdentifier = new KeyIdentifier(nextKey, random.nextInt(), Long.MAX_VALUE); byte[] encoded = keyIdentifierCodec.encode(keyIdentifier); KeyIdentifier decodedKeyIdentifier = keyIdentifierCodec.decode(encoded); assertEquals(keyIdentifier, decodedKeyIdentifier); }
public static <T> void set(Configuration conf, String key, Codec<T> codec, T obj) throws IOException { byte[] encoded = codec.encode(obj); LOG.trace("Serializing {} {}", key, Bytes.toStringBinary(encoded)); conf.set(key, Base64.encodeBase64String(encoded)); }
public static <T> T get(Configuration conf, String key, Codec<T> codec) throws IOException { String value = conf.get(key); if (value == null) { LOG.trace("De-serializing {} with null value", key); return codec.decode(null); } byte[] encoded = Base64.decodeBase64(value); LOG.trace("De-serializing {} {}", key, Bytes.toStringBinary(encoded)); return codec.decode(encoded); }
@Test public void testTokenSerialization() throws Exception { ImmutablePair<TokenManager, Codec<AccessToken>> pair = getTokenManagerAndCodec(); TokenManager tokenManager = pair.getFirst(); tokenManager.startAndWait(); Codec<AccessToken> tokenCodec = pair.getSecond(); long now = System.currentTimeMillis(); String user = "testuser"; List<String> groups = Lists.newArrayList("users", "admins"); AccessTokenIdentifier ident1 = new AccessTokenIdentifier(user, groups, now, now + TOKEN_DURATION); AccessToken token1 = tokenManager.signIdentifier(ident1); byte[] tokenBytes = tokenCodec.encode(token1); AccessToken token2 = tokenCodec.decode(tokenBytes); assertEquals(token1, token2); LOG.info("Deserialized token is: " + Bytes.toStringBinary(tokenCodec.encode(token2))); // should be valid since we just signed it tokenManager.validateSecret(token2); tokenManager.stopAndWait(); } }
try { final T data = dataSupplier.get(); Futures.addCallback(zkClient.setData(path, codec.encode(data)), new FutureCallback<Stat>() { @Override public void onSuccess(Stat state) {
@Override public void onSuccess(NodeData result) { try { RouteConfig route = ROUTE_CONFIG_CODEC.decode(result.getData()); settableFuture.set(route); // Replace the future in the routeConfigMap in order to reflect the route config changes routeConfigMap.replace(serviceId, oldSettableFuture, settableFuture); } catch (Exception ex) { LOG.debug("Unable to deserialize the config for service {}. Got data {}", serviceId, result.getData()); // Need to remove the future from the map since later calls will continue to use this future and will think // that there is an exception routeConfigMap.remove(serviceId, settableFuture); settableFuture.setException(ex); } }
try { final T data = dataSupplier.get(); Futures.addCallback(zkClient.setData(path, codec.encode(data)), new FutureCallback<Stat>() { @Override public void onSuccess(Stat state) {
@Override public void onSuccess(NodeData result) { try { RouteConfig route = ROUTE_CONFIG_CODEC.decode(result.getData()); settableFuture.set(route); // Replace the future in the routeConfigMap in order to reflect the route config changes routeConfigMap.replace(serviceId, oldSettableFuture, settableFuture); } catch (Exception ex) { LOG.debug("Unable to deserialize the config for service {}. Got data {}", serviceId, result.getData()); // Need to remove the future from the map since later calls will continue to use this future and will think // that there is an exception routeConfigMap.remove(serviceId, settableFuture); settableFuture.setException(ex); } }
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); if (data == null) { updateAndNotify(name, null); } else { try { updateAndNotify(name, codec.decode(data)); } catch (IOException e) { LOG.error("Failed to decode property data for {}: {}", name, Bytes.toStringBinary(data), e); notifyError(name, e); } } }
byte[] encodedIdentifier = Base64.encodeBase64(tokenCodec.encode(token)); json.addProperty(ExternalAuthenticationServer.ResponseFields.ACCESS_TOKEN, new String(encodedIdentifier, Charsets.UTF_8));
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); if (data == null) { updateAndNotify(name, null); } else { try { updateAndNotify(name, codec.decode(data)); } catch (IOException e) { LOG.error("Failed to decode property data for {}: {}", name, Bytes.toStringBinary(data), e); notifyError(name, e); } } }