Search in sources :

Example 81 with JsonParseException

use of com.google.gson.JsonParseException in project cloudstack by apache.

the class Request method getCommands.

public Command[] getCommands() {
    if (_cmds == null) {
        try {
            StringReader reader = new StringReader(_content);
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);
            _cmds = s_gson.fromJson(jsonReader, (Type) Command[].class);
        } catch (JsonParseException e) {
            _cmds = new Command[] { new BadCommand() };
        } catch (RuntimeException e) {
            s_logger.error("Caught problem with " + _content, e);
            throw e;
        }
    }
    return _cmds;
}
Also used : Type(java.lang.reflect.Type) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BadCommand(com.cloud.agent.api.BadCommand) Command(com.cloud.agent.api.Command) BadCommand(com.cloud.agent.api.BadCommand) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) JsonParseException(com.google.gson.JsonParseException)

Example 82 with JsonParseException

use of com.google.gson.JsonParseException in project cloudstack by apache.

the class RoutingConfigAdapter method deserialize.

@Override
public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    if (!jsonObject.has("type")) {
        throw new JsonParseException("Deserializing as a RoutingConfig, but no type present in the json object");
    }
    final String routingConfigType = jsonObject.get("type").getAsString();
    if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
        return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
    } else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
        return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
    }
    throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 83 with JsonParseException

use of com.google.gson.JsonParseException in project cloudstack by apache.

the class ConsoleProxyManagerImpl method hasPreviousSession.

private boolean hasPreviousSession(ConsoleProxyVO proxy, VMInstanceVO vm) {
    ConsoleProxyStatus status = null;
    try {
        byte[] detailsInBytes = proxy.getSessionDetails();
        String details = detailsInBytes != null ? new String(detailsInBytes, Charset.forName("US-ASCII")) : null;
        status = parseJsonToConsoleProxyStatus(details);
    } catch (JsonParseException e) {
        s_logger.warn(String.format("Unable to parse proxy [%s] session details [%s] due to [%s].", proxy.toString(), Arrays.toString(proxy.getSessionDetails()), e.getMessage()), e);
    }
    if (status != null && status.getConnections() != null) {
        ConsoleProxyConnectionInfo[] connections = status.getConnections();
        for (ConsoleProxyConnectionInfo connection : connections) {
            long taggedVmId = 0;
            if (connection.tag != null) {
                try {
                    taggedVmId = Long.parseLong(connection.tag);
                } catch (NumberFormatException e) {
                    s_logger.warn(String.format("Unable to parse console proxy connection info passed through tag [%s] due to [%s].", connection.tag, e.getMessage()), e);
                }
            }
            if (taggedVmId == vm.getId()) {
                return true;
            }
        }
        return DateUtil.currentGMTTime().getTime() - vm.getProxyAssignTime().getTime() < proxySessionTimeoutValue;
    } else {
        s_logger.warn(String.format("Unable to retrieve load info from proxy [%s] on an overloaded proxy.", proxy.toString()));
        return false;
    }
}
Also used : ConsoleProxyConnectionInfo(com.cloud.info.ConsoleProxyConnectionInfo) ConsoleProxyStatus(com.cloud.info.ConsoleProxyStatus) JsonParseException(com.google.gson.JsonParseException)

Example 84 with JsonParseException

use of com.google.gson.JsonParseException in project zeppelin by apache.

the class HttpBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    ActionResponse response = null;
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            gson.fromJson(query, Map.class);
        } catch (final JsonParseException e) {
            // This is not a JSON (or maybe not well formatted...)
            query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
        }
    }
    try {
        final HttpRequestWithBody request = Unirest.post(getUrl(indices, types) + "/_search?size=" + size).header("Content-Type", "application/json");
        if (StringUtils.isNoneEmpty(query)) {
            request.header("Accept", "application/json").body(query);
        }
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<JsonNode> result = request.asJson();
        final JSONObject body = result.getBody() != null ? result.getBody().getObject() : null;
        if (isSucceeded(result)) {
            final long total = getTotal(result);
            response = new ActionResponse().succeeded(true).totalHits(total);
            if (containsAggs(result)) {
                JSONObject aggregationsMap = body.getJSONObject("aggregations");
                if (aggregationsMap == null) {
                    aggregationsMap = body.getJSONObject("aggs");
                }
                for (final String key : aggregationsMap.keySet()) {
                    final JSONObject aggResult = aggregationsMap.getJSONObject(key);
                    if (aggResult.has("buckets")) {
                        // Multi-bucket aggregations
                        final Iterator<Object> buckets = aggResult.getJSONArray("buckets").iterator();
                        while (buckets.hasNext()) {
                            response.addAggregation(new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
                        }
                    } else {
                        response.addAggregation(new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
                    }
                    // Keep only one aggregation
                    break;
                }
            } else if (size > 0 && total > 0) {
                final JSONArray hits = getFieldAsArray(body, "hits/hits");
                final Iterator<Object> iter = hits.iterator();
                while (iter.hasNext()) {
                    final JSONObject hit = (JSONObject) iter.next();
                    final Object data = hit.opt("_source") != null ? hit.opt("_source") : hit.opt("fields");
                    response.addHit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), data.toString()));
                }
            }
        } else {
            throw new ActionException(body.get("error").toString());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : JSONArray(org.json.JSONArray) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) JsonParseException(com.google.gson.JsonParseException) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject)

Example 85 with JsonParseException

use of com.google.gson.JsonParseException in project zeppelin by apache.

the class GCSNotebookRepo method get.

@Override
public Note get(String noteId, String notePath, AuthenticationInfo subject) throws IOException {
    BlobId blobId = makeBlobId(noteId, notePath);
    byte[] contents;
    try {
        contents = storage.readAllBytes(blobId);
    } catch (StorageException se) {
        throw new IOException("Could not read " + blobId.toString() + ": " + se.getMessage(), se);
    }
    try {
        return Note.fromJson(noteId, new String(contents, encoding));
    } catch (JsonParseException jpe) {
        throw new IOException("Could note parse as json " + blobId.toString() + jpe.getMessage(), jpe);
    }
}
Also used : IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)267 JsonObject (com.google.gson.JsonObject)105 IOException (java.io.IOException)78 JsonElement (com.google.gson.JsonElement)71 Gson (com.google.gson.Gson)39 JsonArray (com.google.gson.JsonArray)30 JsonReader (com.google.gson.stream.JsonReader)28 InputStreamReader (java.io.InputStreamReader)28 JsonParser (com.google.gson.JsonParser)25 JsonPrimitive (com.google.gson.JsonPrimitive)25 Map (java.util.Map)25 InputStream (java.io.InputStream)23 GsonBuilder (com.google.gson.GsonBuilder)20 ArrayList (java.util.ArrayList)20 Type (java.lang.reflect.Type)17 JsonWriter (com.google.gson.stream.JsonWriter)15 StringReader (java.io.StringReader)13 HashMap (java.util.HashMap)13 LinkedHashMap (java.util.LinkedHashMap)13 HttpUrl (okhttp3.HttpUrl)13