Search in sources :

Example 71 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class LookupQuestCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    String query = String.join(" ", args);
    try {
        HttpEntity entity = new NStringEntity("{ \"query\": { \"match\" : { \"title\" : \"" + query + "\" } } }", ContentType.APPLICATION_JSON);
        Response response = plugin.getBot().getElasticSearch().performRequest("POST", "/wow/quest/_search", Collections.emptyMap(), entity);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            event.getChannel().sendMessage("http://www.wowhead.com/quest=" + firstItem.get("_id")).queue();
        } catch (ParseException e) {
            e.printStackTrace();
            event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "command.lookupquest.notfound")).queue();
        }
    } catch (IOException e) {
        e.printStackTrace();
        plugin.getBot().getStacktraceHandler().sendStacktrace(e, "query:" + query);
        event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "error.occurred.try.again.later")).queue();
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 72 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class ServerCommand method getServerStatus.

/**
 * Retrieve the server status of a World of Warcraft realm
 * The Map returned will have the following values:
 * name -> Realm Name
 * status -> Online/Offline
 * queue -> Yes/No
 * population -> The population of the Realm (Low/Medium/High/Full)
 * @param region The Region the server is hosted in.
 * @param serverName The server name
 * @return A {@link Map} containing the values above if it is found. Else an empty map.
 */
public Map<String, String> getServerStatus(String region, String serverName) throws IOException {
    Map<String, String> map = new HashMap<>();
    HttpUrl url = new HttpUrl.Builder().scheme("https").host(region + ".api.battle.net").addPathSegments("/wow/realm/status").addQueryParameter("realms", serverName).build();
    Request request = new Request.Builder().url(url).build();
    String result = client.newCall(request).execute().body().string();
    try {
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(result);
        JSONArray realms = (JSONArray) object.get("realms");
        for (Object realmObject : realms) {
            JSONObject realm = (JSONObject) realmObject;
            map.put("population", (String) realm.get("population"));
            map.put("queue", (Boolean) realm.get("queue") ? "Yes" : "No");
            map.put("status", (Boolean) realm.get("status") ? "Online" : "Offline");
            map.put("name", realm.get("name").toString());
            map.put("region", region);
        }
    } catch (ParseException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e, "region:" + region, "serverName:" + serverName);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) HttpUrl(okhttp3.HttpUrl) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException)

Example 73 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class GifCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    StringBuilder builder = new StringBuilder();
    for (String s : args) {
        if (builder.length() == 0) {
            builder.append(s);
        } else {
            builder.append(" ").append(s);
        }
    }
    HttpUrl url = new HttpUrl.Builder().scheme("https").host("rightgif.com").addPathSegments("search/web").build();
    FormBody body = new FormBody.Builder().add("text", builder.toString()).build();
    Request request = new Request.Builder().url(url).post(body).build();
    try {
        String result = client.newCall(request).execute().body().string();
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(result);
        event.getChannel().sendMessage(object.get("url").toString()).queue();
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException)

Example 74 with ParseException

use of org.json.simple.parser.ParseException in project storm by apache.

the class RuncLibContainerManager method getContainerIdFromOciJson.

private String getContainerIdFromOciJson(String workerId) throws IOException {
    String ociJson = ConfigUtils.workerRoot(conf, workerId) + FILE_SEPARATOR + OCI_CONFIG_JSON;
    LOG.info("port unknown for workerId {}, looking up from {}", workerId, ociJson);
    JSONParser parser = new JSONParser();
    try (Reader reader = new FileReader(ociJson)) {
        JSONObject jsonObject = (JSONObject) parser.parse(reader);
        return (String) jsonObject.get("containerId");
    } catch (ParseException e) {
        throw new IOException("Unable to parse {}", e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) Reader(java.io.Reader) ObjectReader(org.apache.storm.utils.ObjectReader) FileReader(java.io.FileReader) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 75 with ParseException

use of org.json.simple.parser.ParseException in project storm by apache.

the class ResourceUtils method getJsonWithUpdatedResources.

public static String getJsonWithUpdatedResources(String jsonConf, Map<String, Double> resourceUpdates) {
    try {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(jsonConf);
        JSONObject jsonObject = (JSONObject) obj;
        Map<String, Double> componentResourceMap = (Map<String, Double>) jsonObject.getOrDefault(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, new HashMap<String, Double>());
        for (Map.Entry<String, Double> resourceUpdateEntry : resourceUpdates.entrySet()) {
            if (NormalizedResources.RESOURCE_NAME_NORMALIZER.getResourceNameMapping().containsValue(resourceUpdateEntry.getKey())) {
                // if there will be legacy values they will be in the outer conf
                jsonObject.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
                componentResourceMap.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
            }
            componentResourceMap.put(resourceUpdateEntry.getKey(), resourceUpdateEntry.getValue());
        }
        jsonObject.put(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, componentResourceMap);
        return jsonObject.toJSONString();
    } catch (ParseException ex) {
        throw new RuntimeException("Failed to parse component resources with json: " + jsonConf);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ParseException (org.json.simple.parser.ParseException)259 JSONObject (org.json.simple.JSONObject)193 JSONParser (org.json.simple.parser.JSONParser)186 JSONArray (org.json.simple.JSONArray)84 IOException (java.io.IOException)72 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)34 Map (java.util.Map)23 HashSet (java.util.HashSet)18 API (org.wso2.carbon.apimgt.api.model.API)18 BufferedReader (java.io.BufferedReader)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)13 List (java.util.List)12 File (java.io.File)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 InputStreamReader (java.io.InputStreamReader)10 URL (java.net.URL)10