List of usage examples for org.json JSONObject isNull
public boolean isNull(String key)
From source file:org.wso2.iot.agent.utils.CommonUtils.java
public static AppRestriction getAppRestrictionTypeAndList(Operation operation, ResultPayload resultBuilder, Resources resources) throws AndroidAgentException { AppRestriction appRestriction = new AppRestriction(); JSONArray permittedApps = null;/*from w w w .ja v a 2 s . co m*/ try { JSONObject payload = new JSONObject(operation.getPayLoad().toString()); appRestriction.setRestrictionType((String) payload.get(Constants.AppRestriction.RESTRICTION_TYPE)); if (!payload.isNull(Constants.AppRestriction.RESTRICTED_APPLICATIONS)) { permittedApps = payload.getJSONArray(Constants.AppRestriction.RESTRICTED_APPLICATIONS); } } catch (JSONException e) { if (resources != null && resultBuilder != null) { operation.setStatus(resources.getString(R.string.operation_value_error)); resultBuilder.build(operation); } throw new AndroidAgentException("Invalid JSON format.", e); } List<String> restrictedPackages = new ArrayList<>(); if (permittedApps != null) { for (int i = 0; i < permittedApps.length(); i++) { try { restrictedPackages.add((String) ((JSONObject) permittedApps.get(i)) .get(Constants.AppRuntimePermission.PACKAGE_NAME)); } catch (JSONException e) { if (resources != null && resultBuilder != null) { operation.setStatus(resources.getString(R.string.operation_value_error)); resultBuilder.build(operation); } throw new AndroidAgentException("Invalid JSON format", e); } } } appRestriction.setRestrictedList(restrictedPackages); return appRestriction; }
From source file:org.wso2.iot.agent.utils.CommonUtils.java
public static AppRestriction getAppRuntimePermissionTypeAndList(Operation operation, ResultPayload resultBuilder, Resources resources) throws AndroidAgentException { AppRestriction appRestriction = new AppRestriction(); JSONArray restrictedApps = null;/*from ww w . j ava2s. c o m*/ try { JSONObject payload = new JSONObject(operation.getPayLoad().toString()); appRestriction.setRestrictionType((String) payload.get(Constants.AppRuntimePermission.PERMISSION_TYPE)); if (!payload.isNull(Constants.AppRuntimePermission.PERMITTED_APPS)) { restrictedApps = payload.getJSONArray(Constants.AppRuntimePermission.PERMITTED_APPS); } } catch (JSONException e) { if (resources != null && resultBuilder != null) { operation.setStatus(resources.getString(R.string.operation_value_error)); resultBuilder.build(operation); } throw new AndroidAgentException("Invalid JSON format.", e); } List<String> restrictedPackages = new ArrayList<>(); if (restrictedApps != null) { for (int i = 0; i < restrictedApps.length(); i++) { try { restrictedPackages.add((String) ((JSONObject) restrictedApps.get(i)) .get(Constants.AppRestriction.PACKAGE_NAME)); } catch (JSONException e) { if (resources != null && resultBuilder != null) { operation.setStatus(resources.getString(R.string.operation_value_error)); resultBuilder.build(operation); } throw new AndroidAgentException("Invalid JSON format", e); } } } appRestriction.setRestrictedList(restrictedPackages); return appRestriction; }
From source file:com.scigames.slidegame.SciGamesHttpPoster.java
protected void onPostExecute(JSONObject response) { // this.serverResponse = response; Log.d(TAG, "Called by Activity: "); Log.d(TAG, MyActivity.toString());/*from ww w .j av a 2 s.co m*/ String[] student = { "null" }; String[] slide_session = { "null" }; String[] slide_level = { "null" }; String[] fabric = { "null" }; String[] objective_images = null; String attempts = "null"; String[] result_images = null; String[] score_images = null; boolean no_session = false; if (MyActivity.toString().startsWith("com.scigames.slidegame.LoginActivity") || MyActivity.toString().startsWith("com.scigames.slidegame.ReviewActivity") || MyActivity.toString().startsWith("com.scigames.slidegame.ObjectiveActivity")) { if (checkLoginFailed(response)) { listener.failedQuery(failureReason); } else { if (response.has("student")) { //parse student object try { student = parseStudent(response); } catch (JSONException e) { e.printStackTrace(); } } if (response.has("slide_session")) { if (response.isNull("slide_session")) { Log.d(TAG, "SLIDE_SESSION == NULL"); no_session = true; //this is if a kid swipes in before going on the slide } else { try { slide_session = parseSlideSession(response); } catch (JSONException e) { e.printStackTrace(); } } } if (response.has("slide_level") && !no_session) { //parse slide_level try { slide_level = parseSlideLevel(response); } catch (JSONException e) { e.printStackTrace(); } } if (response.has("fabric") && !no_session) { //parse fabric try { fabric = parseFabric(response); } catch (JSONException e) { e.printStackTrace(); } } if (response.has("objectives")) { try { objective_images = new String[response.getJSONArray("objectives").length()]; Log.d(TAG, "number of objective_images:"); Log.d(TAG, String.valueOf(response.getJSONArray("objectives").length())); Log.d(TAG, response.getJSONArray("objectives").toString()); Log.d(TAG, response.getJSONArray("objectives").getString(0)); for (int i = 0; i < response.getJSONArray("objectives").length(); i++) { objective_images[i] = response.getJSONArray("objectives").getString(i); } } catch (JSONException e) { e.printStackTrace(); } } if (response.has("result_images")) { try { result_images = new String[response.getJSONArray("result_images").length()]; Log.d(TAG, "number of result_images:"); Log.d(TAG, String.valueOf(response.getJSONArray("result_images").length())); Log.d(TAG, response.getJSONArray("result_images").toString()); Log.d(TAG, response.getJSONArray("result_images").getString(0)); for (int i = 0; i < response.getJSONArray("result_images").length(); i++) { result_images[i] = response.getJSONArray("result_images").getString(i); } } catch (JSONException e) { e.printStackTrace(); } } if (response.has("score_images")) { try { score_images = new String[response.getJSONArray("score_images").length()]; for (int i = 0; i < response.getJSONArray("score_images").length(); i++) { score_images[i] = response.getJSONArray("score_images").getString(i); } } catch (JSONException e) { e.printStackTrace(); } } if (response.has("attempts")) { try { attempts = response.getString("attempts"); } catch (JSONException e) { e.printStackTrace(); } } try { listener.onResultsSucceeded(student, slide_session, slide_level, objective_images, fabric, result_images, score_images, attempts, no_session, response); } catch (JSONException e) { e.printStackTrace(); } } } }
From source file:com.imaginary.home.cloud.CloudTest.java
private void setupUser() throws Exception { HashMap<String, Object> user = new HashMap<String, Object>(); long key = (System.currentTimeMillis() % 100000); user.put("firstName", "Test " + key); user.put("lastName", "User"); user.put("email", "test" + key + "@example.com"); user.put("password", "ABC" + random.nextInt(1000000)); HttpClient client = getClient();//from w ww . j a v a 2 s. com HttpPost method = new HttpPost(cloudAPI + "/user"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(user)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject u = new JSONObject(json); JSONObject keys = (u.has("apiKeys") && !u.isNull("apiKeys")) ? u.getJSONObject("apiKeys") : null; if (keys != null) { CloudTest.apiKeyId = (keys.has("apiKeyId") && !keys.isNull("apiKeyId")) ? keys.getString("apiKeyId") : null; CloudTest.apiKeySecret = (keys.has("apiKeySecret") && !keys.isNull("apiKeySecret")) ? keys.getString("apiKeySecret") : null; } } else { Assert.fail("Failed to create user (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
private void setupLocation() throws Exception { if (apiKeyId == null) { setupUser();/* w ww . jav a 2 s.c om*/ } HashMap<String, Object> lstate = new HashMap<String, Object>(); long key = (System.currentTimeMillis() % 100000); lstate.put("name", "My Home " + key); lstate.put("description", "Integration test location"); lstate.put("timeZone", TimeZone.getDefault().getID()); HttpClient client = getClient(); HttpPost method = new HttpPost(cloudAPI + "/location"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); method.addHeader("x-imaginary-api-key", apiKeyId); method.addHeader("x-imaginary-signature", CloudService.sign(apiKeySecret.getBytes("utf-8"), "post:/location:" + apiKeyId + ":" + timestamp + ":" + VERSION)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(lstate)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject u = new JSONObject(json); locationId = (u.has("locationId") && !u.isNull("locationId")) ? u.getString("locationId") : null; } else { Assert.fail("Failed to create location (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
private void setupPairing() throws Exception { if (locationId == null) { setupLocation();//w w w . j av a 2 s. com } HashMap<String, Object> action = new HashMap<String, Object>(); action.put("action", "initializePairing"); HttpClient client = getClient(); HttpPut method = new HttpPut(cloudAPI + "/location/" + locationId); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); method.addHeader("x-imaginary-api-key", apiKeyId); method.addHeader("x-imaginary-signature", CloudService.sign(apiKeySecret.getBytes("utf-8"), "put:/location/" + locationId + ":" + apiKeyId + ":" + timestamp + ":" + VERSION)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(action)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_OK) { String json = EntityUtils.toString(response.getEntity()); JSONObject u = new JSONObject(json); pairingCode = (u.has("pairingCode") && !u.isNull("pairingCode")) ? u.getString("pairingCode") : null; } else { Assert.fail("Failed to initialize pairing (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
private void setupRelay() throws Exception { if (pairingCode == null) { setupPairing();// ww w .j a v a 2 s .co m } HashMap<String, Object> map = new HashMap<String, Object>(); long key = (System.currentTimeMillis() % 100000); map.put("pairingCode", pairingCode); map.put("name", "Test Controller " + key); HttpClient client = getClient(); HttpPost method = new HttpPost(cloudAPI + "/relay"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(map)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject keys = new JSONObject(json); relayKeyId = (keys.has("apiKeyId") && !keys.isNull("apiKeyId")) ? keys.getString("apiKeyId") : null; relayKeySecret = (keys.has("apiKeySecret") && !keys.isNull("apiKeySecret")) ? keys.getString("apiKeySecret") : null; pairingCode = null; } else { Assert.fail("Failed to setup pairing for relay keys (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
private void setupToken() throws Exception { if (relayKeyId == null) { setupRelay();/*from ww w.j a v a 2 s .c om*/ } HttpClient client = getClient(); HttpPost method = new HttpPost(cloudAPI + "/token"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); method.addHeader("x-imaginary-api-key", relayKeyId); method.addHeader("x-imaginary-signature", CloudService.sign(relayKeyId.getBytes("utf-8"), "post:/token:" + relayKeySecret + ":" + timestamp + ":" + VERSION)); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject t = new JSONObject(json); token = (t.has("token") && !t.isNull("token")) ? t.getString("token") : null; } else { Assert.fail("Failed to generate token (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
@Test public void createUser() throws Exception { HashMap<String, Object> user = new HashMap<String, Object>(); long key = (System.currentTimeMillis() % 100000); user.put("firstName", "Test " + key); user.put("lastName", "User"); user.put("email", "test" + key + "@example.com"); user.put("password", "ABC" + random.nextInt(1000000)); HttpClient client = getClient();//from w w w .j av a2 s . co m HttpPost method = new HttpPost(cloudAPI + "/user"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(user)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject u = new JSONObject(json); String userId = (u.has("userId") && !u.isNull("userId")) ? u.getString("userId") : null; String email = (u.has("email") && !u.isNull("email")) ? u.getString("email") : null; String firstName = (u.has("firstName") && !u.isNull("firstName")) ? u.getString("firstName") : null; String lastName = (u.has("lastName") && !u.isNull("lastName")) ? u.getString("lastName") : null; JSONObject keys = (u.has("apiKeys") && !u.isNull("apiKeys")) ? u.getJSONObject("apiKeys") : null; String apiKeyId = null, apiKeySecret = null; if (keys != null) { apiKeyId = (keys.has("apiKeyId") && !keys.isNull("apiKeyId")) ? keys.getString("apiKeyId") : null; apiKeySecret = (keys.has("apiKeySecret") && !keys.isNull("apiKeySecret")) ? keys.getString("apiKeySecret") : null; } out("ID: " + userId); out("Email: " + email); out("First Name: " + firstName); out("Last Name: " + lastName); out("API Key ID: " + apiKeyId); out("API Key Secret: " + apiKeySecret); Assert.assertNotNull("User ID may not be null", userId); Assert.assertNotNull("Email may not be null", email); Assert.assertNotNull("First name may not be null", firstName); Assert.assertNotNull("Last name may not be null", lastName); Assert.assertNotNull("API key ID may not be null", apiKeyId); Assert.assertNotNull("API key secret may not be null", apiKeySecret); if (CloudTest.apiKeyId == null) { CloudTest.apiKeyId = apiKeyId; CloudTest.apiKeySecret = apiKeySecret; } } else { Assert.fail("Failed to create user (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }
From source file:com.imaginary.home.cloud.CloudTest.java
@Test public void createLocation() throws Exception { HashMap<String, Object> user = new HashMap<String, Object>(); long key = (System.currentTimeMillis() % 100000); user.put("name", "My Home " + key); user.put("description", "Integration test location"); user.put("timeZone", TimeZone.getDefault().getID()); HttpClient client = getClient();/* ww w .j a v a 2 s. co m*/ HttpPost method = new HttpPost(cloudAPI + "/location"); long timestamp = System.currentTimeMillis(); method.addHeader("Content-Type", "application/json"); method.addHeader("x-imaginary-version", VERSION); method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp)); method.addHeader("x-imaginary-api-key", apiKeyId); method.addHeader("x-imaginary-signature", CloudService.sign(apiKeySecret.getBytes("utf-8"), "post:/location:" + apiKeyId + ":" + timestamp + ":" + VERSION)); //noinspection deprecation method.setEntity(new StringEntity((new JSONObject(user)).toString(), "application/json", "UTF-8")); HttpResponse response; StatusLine status; try { response = client.execute(method); status = response.getStatusLine(); } catch (IOException e) { e.printStackTrace(); throw new CommunicationException(e); } if (status.getStatusCode() == HttpServletResponse.SC_CREATED) { String json = EntityUtils.toString(response.getEntity()); JSONObject u = new JSONObject(json); String locationId = (u.has("locationId") && !u.isNull("locationId")) ? u.getString("locationId") : null; out("ID: " + locationId); Assert.assertNotNull("Location ID may not be null", locationId); if (CloudTest.locationId == null) { CloudTest.locationId = locationId; } } else { Assert.fail("Failed to create location (" + status.getStatusCode() + ": " + EntityUtils.toString(response.getEntity())); } }