use of com.google.api.client.http.json.JsonHttpContent 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.api.client.http.json.JsonHttpContent in project google-cloud-java by GoogleCloudPlatform.
the class HttpTranslateRpc method detect.
@Override
public List<List<DetectionsResourceItems>> detect(List<String> texts) {
try {
Map<String, ?> content = ImmutableMap.of("q", texts);
HttpRequest httpRequest = translate.getRequestFactory().buildPostRequest(buildTargetUrl("detect"), new JsonHttpContent(translate.getJsonFactory(), content)).setParser(translate.getObjectParser());
List<List<DetectionsResourceItems>> detections = httpRequest.execute().parseAs(DetectionsListResponse.class).getDetections();
// translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
return detections != null ? detections : ImmutableList.<List<DetectionsResourceItems>>of();
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.api.client.http.json.JsonHttpContent in project google-cloud-java by GoogleCloudPlatform.
the class HttpBigQueryRpc method open.
@Override
public String open(JobConfiguration configuration) {
try {
Job loadJob = new Job().setConfiguration(configuration);
StringBuilder builder = new StringBuilder().append(BASE_RESUMABLE_URI).append(options.getProjectId()).append("/jobs");
GenericUrl url = new GenericUrl(builder.toString());
url.set("uploadType", "resumable");
JsonFactory jsonFactory = bigquery.getJsonFactory();
HttpRequestFactory requestFactory = bigquery.getRequestFactory();
HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob));
httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream");
HttpResponse response = httpRequest.execute();
return response.getHeaders().getLocation();
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.api.client.http.json.JsonHttpContent in project google-cloud-java by GoogleCloudPlatform.
the class HttpTranslateRpc method listSupportedLanguages.
@Override
public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap) {
try {
Map<String, ?> content = ImmutableMap.of("target", firstNonNull(Option.TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()));
HttpRequest httpRequest = translate.getRequestFactory().buildPostRequest(buildTargetUrl("languages"), new JsonHttpContent(translate.getJsonFactory(), content)).setParser(translate.getObjectParser());
List<LanguagesResource> languages = httpRequest.execute().parseAs(LanguagesListResponse.class).getLanguages();
// .execute().getLanguages();
return languages != null ? languages : ImmutableList.<LanguagesResource>of();
} catch (IOException ex) {
throw translate(ex);
}
}
Aggregations