use of com.google.api.client.json.JsonFactory in project zeppelin by apache.
the class BigQueryInterpreter method createAuthorizedClient.
//Function that Creates an authorized client to Google Bigquery.
private static Bigquery createAuthorizedClient() throws IOException {
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> bigqueryScopes = BigqueryScopes.all();
credential = credential.createScoped(bigqueryScopes);
}
return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName("Zeppelin/1.0 (GPN:Apache Zeppelin;)").build();
}
use of com.google.api.client.json.JsonFactory in project mobile-android by photo.
the class AccountTroveboxApiTest method testSignInViaGoogle.
public void testSignInViaGoogle() throws ClientProtocolException, IllegalStateException, IOException, JSONException, UserRecoverableAuthException, GoogleAuthException, GeneralSecurityException {
// how to setup environment
// http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html
String[] names = getAccountNames();
assertTrue(names != null && names.length > 0);
String accountName = names[0];
String audience = CommonUtils.getStringResource(R.string.google_auth_server_client_id);
String SCOPE = "audience:server:client_id:" + audience;
String tokenString = GoogleAuthUtil.getToken(getContext(), accountName, SCOPE);
// token verification part, this should be done on server side
GoogleIdTokenVerifier mVerifier;
JsonFactory mJFactory;
NetHttpTransport transport = new NetHttpTransport();
mJFactory = new GsonFactory();
mVerifier = new GoogleIdTokenVerifier(transport, mJFactory);
GoogleIdToken token = GoogleIdToken.parse(mJFactory, tokenString);
assertTrue(mVerifier.verify(token));
GoogleIdToken.Payload tempPayload = token.getPayload();
assertTrue(tempPayload.getAudience().equals(audience));
assertNotNull(tempPayload.getEmail());
// end of token verification part
AccountTroveboxResponse response = mApi.signInViaGoogle(tokenString);
assertNotNull(response);
assertTrue(response.isSuccess());
Credentials[] credentials = response.getCredentials();
assertNotNull(credentials);
assertTrue(credentials.length > 0);
Credentials c = credentials[0];
checkoAuthString(c.getoAuthConsumerKey());
checkoAuthString(c.getoAuthConsumerSecret());
checkoAuthString(c.getoAuthToken());
checkoAuthString(c.getoAuthConsumerSecret());
}
use of com.google.api.client.json.JsonFactory in project beam by apache.
the class GcsUtilTest method googleJsonResponseException.
/**
* Builds a fake GoogleJsonResponseException for testing API error handling.
*/
private static GoogleJsonResponseException googleJsonResponseException(final int status, final String reason, final String message) throws IOException {
final JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setReason(reason);
errorInfo.setMessage(message);
errorInfo.setFactory(jsonFactory);
GenericJson error = new GenericJson();
error.set("code", status);
error.set("errors", Arrays.asList(errorInfo));
error.setFactory(jsonFactory);
GenericJson errorResponse = new GenericJson();
errorResponse.set("error", error);
errorResponse.setFactory(jsonFactory);
return new MockLowLevelHttpRequest().setResponse(new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString()).setContentType(Json.MEDIA_TYPE).setStatusCode(status));
}
};
HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
return GoogleJsonResponseException.from(jsonFactory, response);
}
use of com.google.api.client.json.JsonFactory in project beam by apache.
the class GcsUtilTest method testGetSizeBytesWhenFileNotFoundBatch.
@Test
public void testGetSizeBytesWhenFileNotFoundBatch() throws Exception {
JsonFactory jsonFactory = new JacksonFactory();
String contentBoundary = "batch_foobarbaz";
String contentBoundaryLine = "--" + contentBoundary;
String endOfContentBoundaryLine = "--" + contentBoundary + "--";
GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
error.setFactory(jsonFactory);
String content = contentBoundaryLine + "\n" + "Content-Type: application/http\n" + "\n" + "HTTP/1.1 404 Not Found\n" + "Content-Length: -1\n" + "\n" + error.toString() + "\n" + "\n" + endOfContentBoundaryLine + "\n";
thrown.expect(FileNotFoundException.class);
MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse().setContentType("multipart/mixed; boundary=" + contentBoundary).setContent(content).setStatusCode(HttpStatusCodes.STATUS_CODE_OK);
MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();
GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null));
gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}
use of com.google.api.client.json.JsonFactory 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);
}
}
Aggregations