Search in sources :

Example 1 with BaseEncoding

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);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with BaseEncoding

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()));
}
Also used : AppIdentityService(com.google.appengine.api.appidentity.AppIdentityService) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) BaseEncoding(com.google.common.io.BaseEncoding)

Example 3 with BaseEncoding

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;
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding)

Example 4 with BaseEncoding

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);
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding) Test(org.junit.Test)

Example 5 with BaseEncoding

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);
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding) Test(org.junit.Test)

Aggregations

BaseEncoding (com.google.common.io.BaseEncoding)21 HashFunction (com.google.common.hash.HashFunction)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Insert (com.google.api.services.storage.Storage.Objects.Insert)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 AppIdentityService (com.google.appengine.api.appidentity.AppIdentityService)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)1 Gson (com.google.gson.Gson)1 Salt (edu.stanford.bmir.protege.web.shared.auth.Salt)1