use of com.google.gson.stream.JsonReader in project cdap by caskdata.
the class JsonStructuredRecordDatumReader method decodeRecord.
@Override
protected StructuredRecord decodeRecord(Decoder decoder, Schema schema) throws IOException {
StructuredRecord.Builder builder = StructuredRecord.builder(schema);
JsonReader jsonReader = getJsonReader(decoder);
jsonReader.beginObject();
while (jsonReader.peek() != JsonToken.END_OBJECT) {
Schema.Field field = schema.getField(jsonReader.nextName());
if (field == null) {
// Ignore unrecognized fields
jsonReader.skipValue();
continue;
}
builder.set(field.getName(), decode(decoder, field.getSchema()));
}
jsonReader.endObject();
return builder.build();
}
use of com.google.gson.stream.JsonReader in project cdap by caskdata.
the class JsonStructuredRecordDatumReader method decodeArray.
@Override
protected Collection<?> decodeArray(Decoder decoder, Schema elementSchema) throws IOException {
List<Object> array = new ArrayList<>();
JsonReader jsonReader = getJsonReader(decoder);
jsonReader.beginArray();
while (jsonReader.peek() != JsonToken.END_ARRAY) {
array.add(decode(decoder, elementSchema));
}
jsonReader.endArray();
return array;
}
use of com.google.gson.stream.JsonReader in project cdap by caskdata.
the class StreamClient method getEvents.
/**
* Reads events from a stream
*
* @param streamId ID of the stream
* @param start Timestamp in milliseconds or now-xs format to start reading event from (inclusive)
* @param end Timestamp in milliseconds or now-xs format for the last event to read (exclusive)
* @param limit Maximum number of events to read
* @param callback Callback to invoke for each stream event read. If the callback function returns {@code false}
* upon invocation, it will stops the reading
* @throws IOException If fails to read from stream
* @throws StreamNotFoundException If the given stream does not exists
*/
public void getEvents(StreamId streamId, String start, String end, int limit, Function<? super StreamEvent, Boolean> callback) throws IOException, StreamNotFoundException, UnauthenticatedException {
long startTime = TimeMathParser.parseTime(start, TimeUnit.MILLISECONDS);
long endTime = TimeMathParser.parseTime(end, TimeUnit.MILLISECONDS);
URL url = config.resolveNamespacedURLV3(streamId.getParent(), String.format("streams/%s/events?start=%d&end=%d&limit=%d", streamId.getStream(), startTime, endTime, limit));
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
AccessToken accessToken = config.getAccessToken();
if (accessToken != null) {
urlConn.setRequestProperty(HttpHeaders.AUTHORIZATION, accessToken.getTokenType() + " " + accessToken.getValue());
}
if (urlConn instanceof HttpsURLConnection && !config.isVerifySSLCert()) {
try {
HttpRequests.disableCertCheck((HttpsURLConnection) urlConn);
} catch (Exception e) {
// TODO: Log "Got exception while disabling SSL certificate check for request.getURL()"
}
}
try {
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new UnauthenticatedException("Unauthorized status code received from the server.");
}
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(streamId);
}
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
return;
}
// The response is an array of stream event object
InputStream inputStream = urlConn.getInputStream();
JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8));
jsonReader.beginArray();
while (jsonReader.peek() != JsonToken.END_ARRAY) {
Boolean result = callback.apply(GSON.<StreamEvent>fromJson(jsonReader, StreamEvent.class));
if (result == null || !result) {
break;
}
}
drain(inputStream);
// No need to close reader, the urlConn.disconnect in finally will close all underlying streams
} finally {
urlConn.disconnect();
}
}
use of com.google.gson.stream.JsonReader in project Bookshelf by Darkhax-Minecraft.
the class PlayerUtils method getPlayerNameFromUUID.
/**
* Attempts to get the username associated with a UUID from Mojang. If no username is
* detected or an exception takes place, the exception message will be returned.
*
* @param id The UUID to search for.
* @return The name of the player associated to that uuid.
*/
public static String getPlayerNameFromUUID(UUID uuid) {
if (PROFILE_CACHE.containsValue(uuid)) {
return PROFILE_CACHE.inverse().get(uuid);
}
String name = null;
try {
final BufferedReader reader = Resources.asCharSource(new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "")), StandardCharsets.UTF_8).openBufferedStream();
final JsonReader json = new JsonReader(reader);
json.beginObject();
while (json.hasNext()) {
if ("name".equals(json.nextName())) {
name = json.nextString();
} else {
json.skipValue();
}
}
json.endObject();
json.close();
reader.close();
} catch (final Exception exception) {
Constants.LOG.warn("Could not get name for " + uuid, exception);
name = exception.getMessage();
}
return name;
}
use of com.google.gson.stream.JsonReader in project sling by apache.
the class HttpOsgiClient method findSourceReferences.
@Override
public List<SourceReference> findSourceReferences() throws OsgiClientException {
GetMethod method = new GetMethod(repositoryInfo.appendPath("system/sling/tooling/sourceReferences.json"));
HttpClient client = getHttpClient();
try {
int result = client.executeMethod(method);
if (result != HttpStatus.SC_OK) {
throw new HttpException("Got status code " + result + " for call to " + method.getURI());
}
Gson gson = new Gson();
List<SourceReference> refs = new ArrayList<>();
try (JsonReader jsonReader = new JsonReader(new InputStreamReader(method.getResponseBodyAsStream(), StandardCharsets.US_ASCII))) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
if (jsonReader.nextName().equals("sourceReference")) {
SourceReferenceFromJson sourceReference = gson.fromJson(jsonReader, SourceReference.class);
if (sourceReference.isMavenType()) {
refs.add(sourceReference.getMavenSourceReference());
}
} else {
jsonReader.skipValue();
}
}
jsonReader.endArray();
return refs;
}
} catch (IOException e) {
throw new OsgiClientException(e);
} finally {
method.releaseConnection();
}
}
Aggregations