com.google.api.client.json.JsonToken Java Examples

The following examples show how to use com.google.api.client.json.JsonToken. 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: AndroidJsonParser.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public JsonParser skipChildren() throws IOException {
  if (currentToken != null) {
    switch (currentToken) {
      case START_ARRAY:
        reader.skipValue();
        currentText = "]";
        currentToken = JsonToken.END_ARRAY;
        break;
      case START_OBJECT:
        reader.skipValue();
        currentText = "}";
        currentToken = JsonToken.END_OBJECT;
        break;
      default:
        break;
    }
  }
  return this;
}
 
Example #2
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void testNextToken() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_FEED);
  assertEquals(JsonToken.START_OBJECT, parser.nextToken());
  assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
  assertEquals(JsonToken.START_ARRAY, parser.nextToken());
  assertEquals(JsonToken.START_OBJECT, parser.nextToken());
  assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
  assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
  assertEquals(JsonToken.END_OBJECT, parser.nextToken());
  assertEquals(JsonToken.START_OBJECT, parser.nextToken());
  assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
  assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
  assertEquals(JsonToken.END_OBJECT, parser.nextToken());
  assertEquals(JsonToken.END_ARRAY, parser.nextToken());
  assertEquals(JsonToken.END_OBJECT, parser.nextToken());
  assertNull(parser.nextToken());
}
 
Example #3
Source File: GsonParser.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public JsonParser skipChildren() throws IOException {
  if (currentToken != null) {
    switch (currentToken) {
      case START_ARRAY:
        reader.skipValue();
        currentText = "]";
        currentToken = JsonToken.END_ARRAY;
        break;
      case START_OBJECT:
        reader.skipValue();
        currentText = "}";
        currentToken = JsonToken.END_OBJECT;
        break;
      default:
        break;
    }
  }
  return this;
}
 
Example #4
Source File: GooglePublicKeysManager.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Forces a refresh of the public certificates downloaded from {@link #getPublicCertsEncodedUrl}.
 *
 * <p>
 * This method is automatically called from {@link #getPublicKeys()} if the public keys have not
 * yet been initialized or if the expiration time is very close, so normally this doesn't need to
 * be called. Only call this method to explicitly force the public keys to be updated.
 * </p>
 */
public GooglePublicKeysManager refresh() throws GeneralSecurityException, IOException {
  lock.lock();
  try {
    publicKeys = new ArrayList<PublicKey>();
    // HTTP request to public endpoint
    CertificateFactory factory = SecurityUtils.getX509CertificateFactory();
    HttpResponse certsResponse = transport.createRequestFactory()
        .buildGetRequest(new GenericUrl(publicCertsEncodedUrl)).execute();
    expirationTimeMilliseconds =
        clock.currentTimeMillis() + getCacheTimeInSec(certsResponse.getHeaders()) * 1000;
    // parse each public key in the JSON response
    JsonParser parser = jsonFactory.createJsonParser(certsResponse.getContent());
    JsonToken currentToken = parser.getCurrentToken();
    // token is null at start, so get next token
    if (currentToken == null) {
      currentToken = parser.nextToken();
    }
    Preconditions.checkArgument(currentToken == JsonToken.START_OBJECT);
    try {
      while (parser.nextToken() != JsonToken.END_OBJECT) {
        parser.nextToken();
        String certValue = parser.getText();
        X509Certificate x509Cert = (X509Certificate) factory.generateCertificate(
            new ByteArrayInputStream(StringUtils.getBytesUtf8(certValue)));
        publicKeys.add(x509Cert.getPublicKey());
      }
      publicKeys = Collections.unmodifiableList(publicKeys);
    } finally {
      parser.close();
    }
    return this;
  } finally {
    lock.unlock();
  }
}
 
Example #5
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final void testGson() throws Exception {
  byte[] asciiJson = Charsets.UTF_8.encode("{ \"foo\": 123 }").array();
  JsonParser jp =
      newFactory().createJsonParser(new ByteArrayInputStream(asciiJson), Charsets.UTF_8);
  assertEquals(com.google.api.client.json.JsonToken.START_OBJECT, jp.nextToken());
  assertEquals(com.google.api.client.json.JsonToken.FIELD_NAME, jp.nextToken());
  assertEquals(com.google.api.client.json.JsonToken.VALUE_NUMBER_INT, jp.nextToken());
  assertEquals(123, jp.getIntValue());
  assertEquals(com.google.api.client.json.JsonToken.END_OBJECT, jp.nextToken());
}
 
Example #6
Source File: JacksonFactory.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
static JsonToken convert(com.fasterxml.jackson.core.JsonToken token) {
  if (token == null) {
    return null;
  }
  switch (token) {
    case END_ARRAY:
      return JsonToken.END_ARRAY;
    case START_ARRAY:
      return JsonToken.START_ARRAY;
    case END_OBJECT:
      return JsonToken.END_OBJECT;
    case START_OBJECT:
      return JsonToken.START_OBJECT;
    case VALUE_FALSE:
      return JsonToken.VALUE_FALSE;
    case VALUE_TRUE:
      return JsonToken.VALUE_TRUE;
    case VALUE_NULL:
      return JsonToken.VALUE_NULL;
    case VALUE_STRING:
      return JsonToken.VALUE_STRING;
    case VALUE_NUMBER_FLOAT:
      return JsonToken.VALUE_NUMBER_FLOAT;
    case VALUE_NUMBER_INT:
      return JsonToken.VALUE_NUMBER_INT;
    case FIELD_NAME:
      return JsonToken.FIELD_NAME;
    default:
      return JsonToken.NOT_AVAILABLE;
  }
}
 
Example #7
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testCurrentToken() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_FEED);
  assertNull(parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_ARRAY, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertNull(parser.getCurrentToken());
}
 
Example #8
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipChildren_array() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_FEED);
  parser.nextToken();
  parser.skipToKey("entries");
  parser.skipChildren();
  assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
}
 
Example #9
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipChildren_string() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("title");
  parser.skipChildren();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example #10
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipToKey_startWithFieldName() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.nextToken();
  parser.skipToKey("title");
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example #11
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipToKey_found() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("title");
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example #12
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_missingEmpty() throws Exception {
  JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT);
  parser.nextToken();
  parser.skipToKey("missing");
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example #13
Source File: OpenIdConnectAuthenticator.java    From fess with Apache License 2.0 4 votes vote down vote up
protected void parseJwtClaim(final String jwtClaim, final Map<String, Object> attributes) throws IOException {
    try (final JsonParser jsonParser = jsonFactory.createJsonParser(jwtClaim)) {
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            final String name = jsonParser.getCurrentName();
            if (name != null) {
                jsonParser.nextToken();

                // TODO other parameters
                switch (name) {
                case "iss":
                    attributes.put("iss", jsonParser.getText());
                    break;
                case "sub":
                    attributes.put("sub", jsonParser.getText());
                    break;
                case "azp":
                    attributes.put("azp", jsonParser.getText());
                    break;
                case "email":
                    attributes.put("email", jsonParser.getText());
                    break;
                case "at_hash":
                    attributes.put("at_hash", jsonParser.getText());
                    break;
                case "email_verified":
                    attributes.put("email_verified", jsonParser.getText());
                    break;
                case "aud":
                    attributes.put("aud", jsonParser.getText());
                    break;
                case "iat":
                    attributes.put("iat", jsonParser.getText());
                    break;
                case "exp":
                    attributes.put("exp", jsonParser.getText());
                    break;
                }
            }
        }
    }
}
 
Example #14
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_noMatch() throws Exception {
  JsonParser parser = createParser(JSON_THREE_ELEMENTS);
  assertEquals(null, parser.skipToKey(ImmutableSet.of("foo", "bar", "num")));
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example #15
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipChildren_object() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipChildren();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example #16
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_missing() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("missing");
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example #17
Source File: AndroidJsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken getCurrentToken() {
  return currentToken;
}
 
Example #18
Source File: GsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken nextToken() throws IOException {
  if (currentToken != null) {
    switch (currentToken) {
      case START_ARRAY:
        reader.beginArray();
        currentNameStack.add(null);
        break;
      case START_OBJECT:
        reader.beginObject();
        currentNameStack.add(null);
        break;
      default:
        break;
    }
  }
  // work around bug in GSON parser that it throws an EOFException for an empty document
  // see https://github.com/google/gson/issues/330
  com.google.gson.stream.JsonToken peek;
  try {
    peek = reader.peek();
  } catch (EOFException e) {
    peek = com.google.gson.stream.JsonToken.END_DOCUMENT;
  }
  switch (peek) {
    case BEGIN_ARRAY:
      currentText = "[";
      currentToken = JsonToken.START_ARRAY;
      break;
    case END_ARRAY:
      currentText = "]";
      currentToken = JsonToken.END_ARRAY;
      currentNameStack.remove(currentNameStack.size() - 1);
      reader.endArray();
      break;
    case BEGIN_OBJECT:
      currentText = "{";
      currentToken = JsonToken.START_OBJECT;
      break;
    case END_OBJECT:
      currentText = "}";
      currentToken = JsonToken.END_OBJECT;
      currentNameStack.remove(currentNameStack.size() - 1);
      reader.endObject();
      break;
    case BOOLEAN:
      if (reader.nextBoolean()) {
        currentText = "true";
        currentToken = JsonToken.VALUE_TRUE;
      } else {
        currentText = "false";
        currentToken = JsonToken.VALUE_FALSE;
      }
      break;
    case NULL:
      currentText = "null";
      currentToken = JsonToken.VALUE_NULL;
      reader.nextNull();
      break;
    case STRING:
      currentText = reader.nextString();
      currentToken = JsonToken.VALUE_STRING;
      break;
    case NUMBER:
      currentText = reader.nextString();
      currentToken =
          currentText.indexOf('.') == -1
              ? JsonToken.VALUE_NUMBER_INT
              : JsonToken.VALUE_NUMBER_FLOAT;
      break;
    case NAME:
      currentText = reader.nextName();
      currentToken = JsonToken.FIELD_NAME;
      currentNameStack.set(currentNameStack.size() - 1, currentText);
      break;
    default:
      currentText = null;
      currentToken = null;
  }
  return currentToken;
}
 
Example #19
Source File: GsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
private void checkNumber() {
  Preconditions.checkArgument(
      currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT);
}
 
Example #20
Source File: GsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken getCurrentToken() {
  return currentToken;
}
 
Example #21
Source File: MockJsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken getCurrentToken() {
  return null;
}
 
Example #22
Source File: MockJsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken nextToken() throws IOException {
  return null;
}
 
Example #23
Source File: JacksonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken getCurrentToken() {
  return JacksonFactory.convert(parser.getCurrentToken());
}
 
Example #24
Source File: JacksonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken nextToken() throws IOException {
  return JacksonFactory.convert(parser.nextToken());
}
 
Example #25
Source File: AndroidJsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonToken nextToken() throws IOException {
  if (currentToken != null) {
    switch (currentToken) {
      case START_ARRAY:
        reader.beginArray();
        currentNameStack.add(null);
        break;
      case START_OBJECT:
        reader.beginObject();
        currentNameStack.add(null);
        break;
      default:
        break;
    }
  }
  // work around bug in GSON parser that it throws an EOFException for an empty document
  // see http://github.com/google/gson/issues/330
  android.util.JsonToken peek;
  try {
    peek = reader.peek();
  } catch (EOFException e) {
    peek = android.util.JsonToken.END_DOCUMENT;
  }
  switch (peek) {
    case BEGIN_ARRAY:
      currentText = "[";
      currentToken = JsonToken.START_ARRAY;
      break;
    case END_ARRAY:
      currentText = "]";
      currentToken = JsonToken.END_ARRAY;
      currentNameStack.remove(currentNameStack.size() - 1);
      reader.endArray();
      break;
    case BEGIN_OBJECT:
      currentText = "{";
      currentToken = JsonToken.START_OBJECT;
      break;
    case END_OBJECT:
      currentText = "}";
      currentToken = JsonToken.END_OBJECT;
      currentNameStack.remove(currentNameStack.size() - 1);
      reader.endObject();
      break;
    case BOOLEAN:
      if (reader.nextBoolean()) {
        currentText = "true";
        currentToken = JsonToken.VALUE_TRUE;
      } else {
        currentText = "false";
        currentToken = JsonToken.VALUE_FALSE;
      }
      break;
    case NULL:
      currentText = "null";
      currentToken = JsonToken.VALUE_NULL;
      reader.nextNull();
      break;
    case STRING:
      currentText = reader.nextString();
      currentToken = JsonToken.VALUE_STRING;
      break;
    case NUMBER:
      currentText = reader.nextString();
      currentToken =
          currentText.indexOf('.') == -1
              ? JsonToken.VALUE_NUMBER_INT
              : JsonToken.VALUE_NUMBER_FLOAT;
      break;
    case NAME:
      currentText = reader.nextName();
      currentToken = JsonToken.FIELD_NAME;
      currentNameStack.set(currentNameStack.size() - 1, currentText);
      break;
    default:
      currentText = null;
      currentToken = null;
  }
  return currentToken;
}
 
Example #26
Source File: AndroidJsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
private void checkNumber() {
  Preconditions.checkArgument(
      currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT);
}