use of com.google.common.io.BaseEncoding in project google-cloud-java by GoogleCloudPlatform.
the class HttpStorageRpc method open.
@Override
public String open(StorageObject object, Map<Option, ?> options) {
try {
Insert req = storage.objects().insert(object.getBucket(), object);
GenericUrl url = req.buildHttpRequest().getUrl();
String scheme = url.getScheme();
String host = url.getHost();
String path = "/upload" + url.getRawPath();
url = new GenericUrl(scheme + "://" + host + path);
url.set("uploadType", "resumable");
url.set("name", object.getName());
for (Option option : options.keySet()) {
Object content = option.get(options);
if (content != null) {
url.set(option.value(), content.toString());
}
}
JsonFactory jsonFactory = storage.getJsonFactory();
HttpRequestFactory requestFactory = storage.getRequestFactory();
HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
HttpHeaders requestHeaders = httpRequest.getHeaders();
requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
if (key != null) {
BaseEncoding base64 = BaseEncoding.base64();
HashFunction hashFunction = Hashing.sha256();
requestHeaders.set("x-goog-encryption-algorithm", "AES256");
requestHeaders.set("x-goog-encryption-key", key);
requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
}
HttpResponse response = httpRequest.execute();
if (response.getStatusCode() != 200) {
GoogleJsonError error = new GoogleJsonError();
error.setCode(response.getStatusCode());
error.setMessage(response.getStatusMessage());
throw translate(error);
}
return response.getHeaders().getLocation();
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.common.io.BaseEncoding in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannel method createFirebaseToken.
/**
* Create a secure JWT token for the given userId.
*/
public String createFirebaseToken(Game game, String userId) {
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final BaseEncoding base64 = BaseEncoding.base64();
String header = base64.encode("{\"typ\":\"JWT\",\"alg\":\"RS256\"}".getBytes());
// Construct the claim
String channelKey = game.getChannelKey(userId);
String clientEmail = appIdentity.getServiceAccountName();
long epochTime = System.currentTimeMillis() / 1000;
// an hour from now
long expire = epochTime + 60 * 60;
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("iss", clientEmail);
claims.put("sub", clientEmail);
claims.put("aud", IDENTITY_ENDPOINT);
claims.put("uid", channelKey);
claims.put("iat", epochTime);
claims.put("exp", expire);
String payload = base64.encode(new Gson().toJson(claims).getBytes());
String toSign = String.format("%s.%s", header, payload);
AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes());
return String.format("%s.%s", toSign, base64.encode(result.getSignature()));
}
use of com.google.common.io.BaseEncoding in project controller by opendaylight.
the class SimpleBinaryAttributeReadingStrategy method postprocessParsedValue.
@Override
protected Object postprocessParsedValue(final String textContent) {
BaseEncoding en = BaseEncoding.base64();
byte[] decode = en.decode(textContent);
List<String> parsed = Lists.newArrayListWithCapacity(decode.length);
for (byte b : decode) {
parsed.add(Byte.toString(b));
}
return parsed;
}
use of com.google.common.io.BaseEncoding in project controller by opendaylight.
the class LLDPTLVTest method testCreateCustomTLVValue.
/**
* Test method for
* {@link org.opendaylight.controller.liblldp.LLDPTLV#createCustomTLVValue(java.lang.String)}
* .
*/
@Test
public void testCreateCustomTLVValue() {
byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
byte[] expectedCustomTlv = Bytes.concat(new byte[] { // openflow OUI
0x00, 0x26, (byte) 0xe1, // subtype
0x00 }, // custom value
CUSTOM_TLV_ULTIMATE_BIN);
BaseEncoding be = BaseEncoding.base16().withSeparator(" ", 2).lowerCase();
LOG.debug("expected: {}", be.encode(expectedCustomTlv));
LOG.debug("actual : {}", be.encode(tlv));
Assert.assertArrayEquals(expectedCustomTlv, tlv);
}
use of com.google.common.io.BaseEncoding in project openflowplugin by opendaylight.
the class LLDPTLVTest method testCreateCustomTLVValue.
/**
* Test method for
* {@link org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV#createCustomTLVValue(java.lang.String)}
* .
*/
@Test
public void testCreateCustomTLVValue() {
byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
byte[] expectedCustomTlv = Bytes.concat(new byte[] { // openflow OUI
0x00, 0x26, (byte) 0xe1, // subtype
0x00 }, // custom value
CUSTOM_TLV_ULTIMATE_BIN);
BaseEncoding be = BaseEncoding.base16().withSeparator(" ", 2).lowerCase();
LOG.debug("expected: {}", be.encode(expectedCustomTlv));
LOG.debug("actual : {}", be.encode(tlv));
Assert.assertArrayEquals(expectedCustomTlv, tlv);
}
Aggregations