com.google.api.client.util.ObjectParser Java Examples

The following examples show how to use com.google.api.client.util.ObjectParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BatchRequestTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void subTestExecuteWithVoidCallback(boolean testServerError) throws IOException {
  MockTransport transport = new MockTransport(testServerError, false,false, false, false);
  MockGoogleClient client = new MockGoogleClient.Builder(
      transport, ROOT_URL, SERVICE_PATH, null, null).setApplicationName("Test Application")
      .build();
  MockGoogleClientRequest<String> jsonHttpRequest1 =
      new MockGoogleClientRequest<String>(client, METHOD1, URI_TEMPLATE1, null, String.class);
  MockGoogleClientRequest<String> jsonHttpRequest2 =
      new MockGoogleClientRequest<String>(client, METHOD2, URI_TEMPLATE2, null, String.class);
  ObjectParser parser = new JsonObjectParser(new JacksonFactory());
  BatchRequest batchRequest =
      new BatchRequest(transport, null).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
  HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
  request1.setParser(parser);
  HttpRequest request2 = jsonHttpRequest2.buildHttpRequest();
  request2.setParser(parser);
  batchRequest.queue(request1, MockDataClass1.class, GoogleJsonErrorContainer.class, callback1);
  batchRequest.queue(request2, Void.class, Void.class, callback3);
  batchRequest.execute();
  // Assert transport called expected number of times.
  assertEquals(1, transport.actualCalls);
}
 
Example #2
Source File: GitHubRequest.java    From android-oauth-client with Apache License 2.0 6 votes vote down vote up
@Override
public T execute() throws IOException {
    HttpResponse response = super.executeUnparsed();
    ObjectParser parser = response.getRequest().getParser();
    // This will degrade parsing performance but is an inevitable workaround
    // for the inability to parse JSON arrays.
    String content = response.parseAsString();
    if (response.isSuccessStatusCode()
            && !TextUtils.isEmpty(content)
            && content.charAt(0) == '[') {
        content = TextUtils.concat("{\"", GitHubResponse.KEY_DATA, "\":", content, "}")
                .toString();
    }
    Reader reader = new StringReader(content);
    T parsedResponse = parser.parseAndClose(reader, getResponseClass());

    // parse pagination from Link header
    if (parsedResponse instanceof GitHubResponse) {
        Pagination pagination =
                new Pagination(response.getHeaders().getFirstHeaderStringValue("Link"));
        ((GitHubResponse) parsedResponse).setPagination(pagination);
    }

    return parsedResponse;
}
 
Example #3
Source File: TwitterRequest.java    From android-oauth-client with Apache License 2.0 6 votes vote down vote up
@Override
public T execute() throws IOException {
    HttpResponse response = super.executeUnparsed();
    ObjectParser parser = response.getRequest().getParser();
    // This will degrade parsing performance but is an inevitable workaround
    // for the inability to parse JSON arrays.
    String content = response.parseAsString();
    if (response.isSuccessStatusCode()
            && !TextUtils.isEmpty(content)
            && content.charAt(0) == '[') {
        content = TextUtils.concat("{\"", TwitterResponse.KEY_DATA, "\":", content, "}")
                .toString();
    }
    Reader reader = new StringReader(content);
    return parser.parseAndClose(reader, getResponseClass());
}
 
Example #4
Source File: TypedNotificationCallback.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns an {@link ObjectParser} which can be used to parse this notification. */
protected abstract ObjectParser getObjectParser() throws IOException;
 
Example #5
Source File: BatchRequestTest.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
private BatchRequest getBatchPopulatedWithRequests(boolean testServerError,
    boolean testAuthenticationError,
    boolean returnSuccessAuthenticatedContent,
    boolean testRedirect,
    boolean testBinary,
    boolean testMissingLength) throws IOException {
  transport = new MockTransport(testServerError,
      testAuthenticationError,
      testRedirect,
      testBinary,
      testMissingLength);
  MockGoogleClient client = new MockGoogleClient.Builder(
      transport, ROOT_URL, SERVICE_PATH, null, null).setApplicationName("Test Application")
      .build();
  MockGoogleClientRequest<String> jsonHttpRequest1 =
      new MockGoogleClientRequest<String>(client, METHOD1, URI_TEMPLATE1, null, String.class);
  MockGoogleClientRequest<String> jsonHttpRequest2 =
      new MockGoogleClientRequest<String>(client, METHOD2, URI_TEMPLATE2, null, String.class);
  credential = new MockCredential();

  ObjectParser parser =
      testBinary ? new ProtoObjectParser() : new JsonObjectParser(new JacksonFactory());
  BatchRequest batchRequest =
      new BatchRequest(transport, credential).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
  HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
  request1.setParser(parser);
  HttpRequest request2 = jsonHttpRequest2.buildHttpRequest();
  request2.setParser(parser);
  if (testAuthenticationError) {
    request2.setUnsuccessfulResponseHandler(
        new MockUnsuccessfulResponseHandler(transport, returnSuccessAuthenticatedContent));
  }

  if (testBinary) {
    batchRequest.queue(request1, MockData.Class1.class, ErrorOutput.ErrorBody.class,
        new TestCallback1Adapter(callback1));
    batchRequest.queue(request2, MockData.Class2.class, ErrorOutput.ErrorBody.class,
        new TestCallback2Adapter(callback2));
  } else {
    batchRequest.queue(request1, MockDataClass1.class, GoogleJsonErrorContainer.class, callback1);
    batchRequest.queue(request2, MockDataClass2.class, GoogleJsonErrorContainer.class, callback2);
  }
  return batchRequest;
}
 
Example #6
Source File: AbstractGoogleClient.java    From google-api-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * Returns an instance of a new builder.
 *
 * @param transport The transport to use for requests
 * @param rootUrl root URL of the service. Must end with a "/"
 * @param servicePath service path
 * @param objectParser object parser or {@code null} for none
 * @param httpRequestInitializer HTTP request initializer or {@code null} for none
 */
protected Builder(HttpTransport transport, String rootUrl, String servicePath,
    ObjectParser objectParser, HttpRequestInitializer httpRequestInitializer) {
  this.transport = Preconditions.checkNotNull(transport);
  this.objectParser = objectParser;
  setRootUrl(rootUrl);
  setServicePath(servicePath);
  this.httpRequestInitializer = httpRequestInitializer;
}
 
Example #7
Source File: HttpRequest.java    From google-http-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the {@link ObjectParser} used to parse the response to this request or {@code null} for
 * none.
 *
 * <p>This parser will be preferred over any registered HttpParser.
 *
 * @since 1.10
 */
public HttpRequest setParser(ObjectParser parser) {
  this.objectParser = parser;
  return this;
}
 
Example #8
Source File: HttpRequest.java    From google-http-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link ObjectParser} used to parse the response or {@code null} for none.
 *
 * @since 1.10
 */
public final ObjectParser getParser() {
  return objectParser;
}
 
Example #9
Source File: AbstractGoogleClient.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the object parser or {@code null} for none.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 */
public ObjectParser getObjectParser() {
  return objectParser;
}
 
Example #10
Source File: AbstractGoogleClient.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the object parser or {@code null} for none.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 */
public ObjectParser getObjectParser() {
  return objectParser;
}
 
Example #11
Source File: MockGoogleClient.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * @param transport The transport to use for requests
 * @param rootUrl root URL of the service. Must end with a "/"
 * @param servicePath service path
 * @param objectParser object parser or {@code null} for none
 * @param httpRequestInitializer HTTP request initializer or {@code null} for none
 *
 * @since 1.14
 */
public MockGoogleClient(HttpTransport transport, String rootUrl, String servicePath,
    ObjectParser objectParser, HttpRequestInitializer httpRequestInitializer) {
  this(new Builder(transport, rootUrl, servicePath, objectParser, httpRequestInitializer));
}
 
Example #12
Source File: MockGoogleClient.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * @param transport The transport to use for requests
 * @param rootUrl root URL of the service. Must end with a "/"
 * @param servicePath service path
 * @param objectParser object parser or {@code null} for none
 * @param httpRequestInitializer HTTP request initializer or {@code null} for none
 */
public Builder(HttpTransport transport, String rootUrl, String servicePath,
    ObjectParser objectParser, HttpRequestInitializer httpRequestInitializer) {
  super(transport, rootUrl, servicePath, objectParser, httpRequestInitializer);
}