Search in sources :

Example 21 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class DigitalSTROMJSONImpl method getDeviceSensorValue.

@Override
public short getDeviceSensorValue(String token, DSID dsid, String name, SensorIndexEnum sensorIndex) {
    if (((dsid != null && dsid.getValue() != null) || name != null) && sensorIndex != null) {
        String response = null;
        if (dsid != null && dsid.getValue() != null) {
            if (name != null) {
                response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_DSID + dsid.getValue() + JSONRequestConstants.INFIX_PARAMETER_NAME + name + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
            } else {
                response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_DSID + dsid.getValue() + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
            }
        } else if (name != null) {
            response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_NAME + name + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
        }
        JSONObject responseObj = handler.toJSONObject(response);
        if (handler.checkResponse(responseObj)) {
            JSONObject valueObject = handler.getResultJSONObject(responseObj);
            if (valueObject != null && valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()) != null) {
                short value = -1;
                try {
                    value = Short.parseShort(valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()).toString());
                } catch (java.lang.NumberFormatException e) {
                    logger.error("NumberFormatException by getDeviceSensorValue: " + valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()).toString());
                }
                return value;
            }
        }
    }
    return -1;
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 22 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class JointSpaceBinding method getTVVolume.

/**
     * Function to query the TV Volume
     *
     * @param host
     * @return struct containing all given information about current volume
     *         settings (volume, mute, min, max) @see volumeConfig
     */
private volumeConfig getTVVolume(String host) {
    volumeConfig conf = new volumeConfig();
    String url = "http://" + host + "/1/audio/volume";
    String volume_json = HttpUtil.executeUrl("GET", url, IOUtils.toInputStream(""), CONTENT_TYPE_JSON, 1000);
    if (volume_json != null) {
        try {
            Object obj = JSONValue.parse(volume_json);
            JSONObject array = (JSONObject) obj;
            conf.mute = Boolean.parseBoolean(array.get("muted").toString());
            conf.volume = Integer.parseInt(array.get("current").toString());
            conf.min = Integer.parseInt(array.get("min").toString());
            conf.max = Integer.parseInt(array.get("max").toString());
        } catch (NumberFormatException ex) {
            logger.warn("Exception while interpreting volume json return");
        } catch (Throwable t) {
            logger.warn("Could not parse JSON String for volume value. Error: {}", t.toString());
        }
    }
    return conf;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 23 with JsonObject

use of org.json.simple.JsonObject in project opennms by OpenNMS.

the class MattermostNotificationStrategyTestServlet method squawk.

@SuppressWarnings("unchecked")
private void squawk(final HttpServletResponse resp, String reason) throws IOException {
    JSONObject errorJson = new JSONObject();
    errorJson.put("message", reason);
    errorJson.put("detailed_error", "");
    errorJson.put("request_id", "deadbeefcafebabe");
    errorJson.put("status_code", 500);
    errorJson.put("isoauth", false);
    final String responseText = errorJson.toJSONString();
    final ServletOutputStream os = resp.getOutputStream();
    os.print(responseText);
    os.close();
    resp.setContentType("application/json");
    resp.setContentLength(responseText.length());
    resp.setStatus(500);
}
Also used : JSONObject(org.json.simple.JSONObject) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 24 with JsonObject

use of org.json.simple.JsonObject in project tdi-studio-se by Talend.

the class OAuthClient method refreshToken.

public Token refreshToken(String refreshToken) throws Exception {
    Token newToken = new Token();
    newToken.setRefresh_token(refreshToken);
    // get token using refresh token
    String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
    URLConnection conn = new URL(token_url).openConnection();
    // post
    conn.setDoOutput(true);
    String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&refresh_token=%s&format=%s", "refresh_token", clientID, clientSecret, refreshToken, "json");
    OutputStream output = null;
    try {
        output = conn.getOutputStream();
        output.write(query.getBytes(UTF8));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ignore) {
            }
        }
    }
    InputStream input = conn.getInputStream();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(input));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        if (json.get("error") == null) {
            if (json.get("access_token") != null) {
                newToken.setAccess_token(json.get("access_token").toString());
            }
            if (json.get("instance_url") != null) {
                newToken.setInstance_url(json.get("instance_url").toString());
            }
            if (json.get("id") != null) {
                newToken.setId(json.get("id").toString());
            }
            if (json.get("issued_at") != null) {
                newToken.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
            }
            if (json.get("signature") != null) {
                newToken.setSignature(json.get("signature").toString());
            }
        } else {
            throw new Exception(json.toString());
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
    }
    return newToken;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 25 with JsonObject

use of org.json.simple.JsonObject in project tdi-studio-se by Talend.

the class OAuthClient method getToken.

public Token getToken() throws Exception {
    Token token = new Token();
    URL callback_url = new URL("https", callbackHost, callbackPort, "");
    String oauth2_authorize_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_AUTHORIZE : baseOAuthURL + "/" + OAUTH2_AUTHORIZE;
    String authorize_url = // &scope=%s
    String.format(// &scope=%s
    "%s?response_type=%s&client_id=%s&redirect_uri=%s", // &scope=%s
    oauth2_authorize_url, "code", clientID, // , "full%20refresh_token"
    URLEncoder.encode(callback_url.toString(), UTF8));
    System.out.println("Paste this URL into a web browser to authorize Salesforce Access:");
    System.out.println(authorize_url);
    // start a service to get Authorization code
    HttpsService service = new HttpsService(callbackPort);
    String code = null;
    while (service.getCode() == null) {
        Thread.sleep(2 * 1000);
    }
    code = service.getCode();
    // stop service
    service.stop();
    // get token using Authorization code
    String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
    URLConnection conn = new URL(token_url).openConnection();
    // post
    conn.setDoOutput(true);
    String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s", "authorization_code", clientID, clientSecret, URLEncoder.encode(callback_url.toString(), UTF8), code);
    OutputStream output = null;
    try {
        output = conn.getOutputStream();
        output.write(query.getBytes(UTF8));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ignore) {
            }
        }
    }
    InputStream input = conn.getInputStream();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(input));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        if (json.get("access_token") != null) {
            token.setAccess_token(json.get("access_token").toString());
        }
        if (json.get("refresh_token") != null) {
            token.setRefresh_token(json.get("refresh_token").toString());
        }
        if (json.get("instance_url") != null) {
            token.setInstance_url(json.get("instance_url").toString());
        }
        if (json.get("id") != null) {
            token.setId(json.get("id").toString());
        }
        if (json.get("issued_at") != null) {
            token.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
        }
        if (json.get("signature") != null) {
            token.setSignature(json.get("signature").toString());
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
    }
    return token;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

JSONObject (org.json.simple.JSONObject)2110 JSONArray (org.json.simple.JSONArray)601 JSONParser (org.json.simple.parser.JSONParser)389 Test (org.junit.Test)341 Test (org.junit.jupiter.api.Test)247 HashMap (java.util.HashMap)214 IOException (java.io.IOException)199 ArrayList (java.util.ArrayList)190 ParseException (org.json.simple.parser.ParseException)171 Map (java.util.Map)140 Date (java.util.Date)74 InputStreamReader (java.io.InputStreamReader)62 List (java.util.List)61 File (java.io.File)60 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)60 URL (java.net.URL)45 HashSet (java.util.HashSet)42 HttpURLConnection (java.net.HttpURLConnection)41 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)41 HttpClient (org.apache.commons.httpclient.HttpClient)41