Search in sources :

Example 46 with JsonReader

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();
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) JsonReader(com.google.gson.stream.JsonReader) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord)

Example 47 with JsonReader

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;
}
Also used : ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader)

Example 48 with JsonReader

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) URL(java.net.URL) IOException(java.io.IOException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HttpURLConnection(java.net.HttpURLConnection) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) JsonReader(com.google.gson.stream.JsonReader) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 49 with JsonReader

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;
}
Also used : BufferedReader(java.io.BufferedReader) JsonReader(com.google.gson.stream.JsonReader) URL(java.net.URL)

Example 50 with JsonReader

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) SourceReference(org.apache.sling.ide.osgi.SourceReference) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonReader(com.google.gson.stream.JsonReader) HttpException(org.apache.commons.httpclient.HttpException)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)100 StringReader (java.io.StringReader)38 JsonElement (com.google.gson.JsonElement)31 IOException (java.io.IOException)20 JsonObject (com.google.gson.JsonObject)19 Test (org.junit.Test)19 InputStreamReader (java.io.InputStreamReader)18 JsonParser (com.google.gson.JsonParser)12 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)10 JsonWriter (com.google.gson.stream.JsonWriter)10 TypeToken (com.google.gson.reflect.TypeToken)8 Map (java.util.Map)8 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 GsonBuilder (com.google.gson.GsonBuilder)4