Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

In this page you can find the example usage for org.json JSONObject getJSONObject.

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

@Test
public void testPushToDelete() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[][] clonePaths = createTestClonePairs(workspaceLocation);

    for (IPath[] clonePath : clonePaths) {
        // clone 1
        JSONObject clone1 = clone(clonePath[0]);
        String cloneLocation1 = clone1.getString(ProtocolConstants.KEY_LOCATION);
        String contentLocation1 = clone1.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
        String branchesLocation1 = clone1.getString(GitConstants.KEY_BRANCH);

        // clone 1 - get project1 metadata
        WebRequest request = getGetRequest(contentLocation1);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project1 = new JSONObject(response.getText());
        JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);
        String gitIndexUri1 = gitSection1.getString(GitConstants.KEY_INDEX);
        String gitHeadUri1 = gitSection1.getString(GitConstants.KEY_HEAD);

        // clone 1 - create branch "a"
        response = branch(branchesLocation1, "a");
        JSONObject newBranch = new JSONObject(response.getText());
        JSONArray remoteBranchLocations1 = newBranch.getJSONArray(GitConstants.KEY_REMOTE);
        assertEquals(1, remoteBranchLocations1.length());

        // clone 1 - checkout "a"
        final String newBranchName = "a";
        response = checkoutBranch(cloneLocation1, newBranchName);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - change
        JSONObject testTxt1 = getChild(project1, "test.txt");
        modifyFile(testTxt1, "clone1 change");

        // clone 1 - add
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - commit
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "clone1 change commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - push "a"
        int i = 0;
        String remoteBranchName = remoteBranchLocations1.getJSONObject(0)
                .getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(i)
                .getString(ProtocolConstants.KEY_NAME);
        if (!remoteBranchName.equals("origin/a")) {
            i = 1;/*  w  w  w . ja va 2s.  c  o  m*/
            remoteBranchName = remoteBranchLocations1.getJSONObject(0)
                    .getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(i)
                    .getString(ProtocolConstants.KEY_NAME);
        }
        assertEquals("origin/a", remoteBranchName);
        String remoteBranchLocation1 = remoteBranchLocations1.getJSONObject(0)
                .getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(i)
                .getString(ProtocolConstants.KEY_LOCATION);
        ServerStatus pushStatus = push(remoteBranchLocation1, Constants.HEAD, false);
        assertEquals(true, pushStatus.isOK());

        // clone 1 - list remote branches - expect 2
        JSONObject remote1 = getRemote(gitRemoteUri1, 1, 0, Constants.DEFAULT_REMOTE_NAME);
        String remoteLocation1 = remote1.getString(ProtocolConstants.KEY_LOCATION);

        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation1);
        response = webConversation.getResponse(request);
        ServerStatus status = waitForTask(response);
        assertTrue(status.toString(), status.isOK());
        remote1 = status.getJsonData();
        JSONArray refsArray = remote1.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(2, refsArray.length());
        JSONObject ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + newBranchName,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        ref = refsArray.getJSONObject(1);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));

        // clone 2 
        JSONObject clone2 = clone(clonePath[1]);
        String cloneLocation2 = clone2.getString(ProtocolConstants.KEY_LOCATION);
        String contentLocation2 = clone2.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // clone 2 - get project2 metadata
        request = getGetRequest(contentLocation2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project2 = new JSONObject(response.getText());
        JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);

        // clone 2 - check if the branch "a" is available
        JSONObject remote2 = getRemote(gitRemoteUri2, 1, 0, Constants.DEFAULT_REMOTE_NAME);
        String remoteLocation2 = remote2.getString(ProtocolConstants.KEY_LOCATION);

        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        remote2 = new JSONObject(response.getText());
        refsArray = remote2.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(2, refsArray.length());
        ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        ref = refsArray.getJSONObject(1);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + newBranchName,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        String remoteBranchLocation2 = ref.getString(ProtocolConstants.KEY_LOCATION);

        // clone 2 - checkout branch "a"
        response = checkoutBranch(cloneLocation2, newBranchName);

        // clone 1 - delete remote branch "a"
        push(remoteBranchLocation1, "", false, false);

        // clone 1 - list remote branches - expect 1
        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        remote1 = new JSONObject(response.getText());
        refsArray = remote1.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(1, refsArray.length());
        ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));

        // clone 2 - fetch
        request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation2, true, false);
        response = webConversation.getResponse(request);
        status = waitForTask(response);
        assertFalse(status.toString(), status.isOK());

        // clone 2 - fetch task should fail
        JSONObject statusJson = status.toJSON();
        JSONObject result = statusJson.has("Result") ? statusJson.getJSONObject("Result") : statusJson;
        assertEquals("Error", result.getString("Severity"));
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

@Test
public void testPushFromLog() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    clone(clonePath);/* www. ja  va 2s.  c om*/

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // log
    JSONArray commitsArray = log(gitHeadUri);
    assertEquals(1, commitsArray.length());

    // change
    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "incoming change");

    // add
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // commit
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "incoming change commit", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // log again
    JSONObject logResponse = logObject(gitHeadUri);
    assertEquals(2, logResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject toRefBranch = logResponse.getJSONObject(GitConstants.KEY_LOG_TO_REF);
    JSONArray remoteLocations = toRefBranch.getJSONArray(GitConstants.KEY_REMOTE);
    assertEquals(1, remoteLocations.length());
    String remoteBranchLocation = remoteLocations.getJSONObject(0).getJSONArray(ProtocolConstants.KEY_CHILDREN)
            .getJSONObject(0).getString(ProtocolConstants.KEY_LOCATION);

    // push
    request = getPostGitRemoteRequest(remoteBranchLocation, Constants.HEAD, false, false);
    response = webConversation.getResponse(request);
    ServerStatus status = waitForTask(response);
    assertTrue(status.toString(), status.isOK());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

@Test
public void testPushRemoteRejected() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    clone(clonePath);//w w  w .ja v a 2s .  com

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitRemoteUri = gitSection.getString(GitConstants.KEY_REMOTE);

    // 1st commit
    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "1st change");
    addFile(testTxt);
    commitFile(testTxt, "1st change commit", false);

    // push
    ServerStatus pushStatus = push(gitRemoteUri, 1, 0, Constants.MASTER, Constants.HEAD, false);
    assertEquals(IStatus.OK, pushStatus.getSeverity());

    // 2nd commit
    modifyFile(testTxt, "2nd change");
    addFile(testTxt);
    commitFile(testTxt, "2nd change commit", false);

    FileUtils.delete(new File(gitDir, Constants.DOT_GIT + "/objects/pack/"), FileUtils.RECURSIVE);

    pushStatus = push(gitRemoteUri, 1, 0, Constants.MASTER, Constants.HEAD, false);
    assertEquals(IStatus.WARNING, pushStatus.getSeverity());
    Status pushResult = Status.valueOf(pushStatus.getMessage());
    assertEquals(Status.REJECTED_OTHER_REASON, pushResult);
    JSONObject jsonResult = pushStatus.toJSON();
    if (jsonResult.has("JsonData")) {
        jsonResult = jsonResult.getJSONObject("JsonData");
    }
    assertTrue(jsonResult.toString(), jsonResult.has("DetailedMessage"));
    assertTrue(jsonResult.getString("DetailedMessage"),
            jsonResult.getString("DetailedMessage").matches("^object [\\da-f]+ missing$"));
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

@Test
public void testForcedPush() throws Exception {
    // overwrite system settings, allow forced pushes, see bug 371881
    StoredConfig cfg = db.getConfig();/*  w  w  w . ja  v a  2  s .co  m*/
    cfg.setBoolean("receive", null, "denyNonFastforwards", false);
    cfg.save();

    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[][] clonePaths = createTestClonePairs(workspaceLocation);

    for (IPath[] clonePath : clonePaths) {
        // clone1
        String contentLocation1 = clone(clonePath[0]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // get project1 metadata
        WebRequest request = getGetRequest(contentLocation1);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project1 = new JSONObject(response.getText());
        JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);
        String gitIndexUri1 = gitSection1.getString(GitConstants.KEY_INDEX);
        String gitHeadUri1 = gitSection1.getString(GitConstants.KEY_HEAD);

        // clone2
        String contentLocation2 = clone(clonePath[1]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // get project2 metadata
        request = getGetRequest(contentLocation2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project2 = new JSONObject(response.getText());
        JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
        String gitIndexUri2 = gitSection2.getString(GitConstants.KEY_INDEX);
        String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);

        // clone1: change
        JSONObject testTxt1 = getChild(project1, "test.txt");
        modifyFile(testTxt1, "clone1 change");

        // clone1: add
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone1: commit
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "clone1 change commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone1: push
        ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, false);
        assertEquals(true, pushStatus.isOK());

        // clone2: change
        JSONObject testTxt2 = getChild(project2, "test.txt");
        modifyFile(testTxt2, "clone2 change");

        // clone2: add
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone2: commit
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri2, "clone2 change commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone2: push
        pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false);
        assertEquals(IStatus.WARNING, pushStatus.getSeverity());
        Status pushResult = Status.valueOf(pushStatus.getMessage());
        assertEquals(Status.REJECTED_NONFASTFORWARD, pushResult);

        // clone2: forced push
        pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false, true);
        assertTrue(pushStatus.toJSON().toString(), pushStatus.isOK());
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

@Test
public void testPushTags() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);

    // clone1/*from  w  w  w  . ja  va  2  s .c  om*/
    JSONObject project1 = createProjectOrLink(workspaceLocation, getMethodName() + "1", null);
    IPath clonePath1 = getClonePath(workspaceId, project1);
    clone(clonePath1);

    // get project1 metadata
    WebRequest request = getGetRequest(project1.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project1 = new JSONObject(response.getText());

    JSONObject gitSection1 = project1.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection1);
    String gitRemoteUri1 = gitSection1.optString(GitConstants.KEY_REMOTE);
    String gitTagUri1 = gitSection1.optString(GitConstants.KEY_TAG);

    // clone2
    JSONObject project2 = createProjectOrLink(workspaceLocation, getMethodName() + "2", null);
    IPath clonePath2 = getClonePath(workspaceId, project2);
    clone(clonePath2);

    // get project2 metadata
    request = getGetRequest(project2.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project2 = new JSONObject(response.getText());

    JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
    String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
    String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);
    String gitTagUri2 = gitSection2.getString(GitConstants.KEY_TAG);

    // clone1: tag HEAD with 'tag'
    tag(gitTagUri1, "tag", Constants.HEAD);

    ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, true);
    assertEquals(true, pushStatus.isOK());

    // clone2: list tags
    JSONArray tags = listTags(gitTagUri2);
    assertEquals(0, tags.length());

    // clone2: fetch + merge
    JSONObject remoteBranch = getRemoteBranch(gitRemoteUri2, 1, 0, Constants.MASTER);
    String remoteBranchLocation2 = remoteBranch.getString(ProtocolConstants.KEY_LOCATION);
    fetch(remoteBranchLocation2);
    String id = remoteBranch.getString(ProtocolConstants.KEY_ID);
    merge(gitHeadUri2, id);

    // clone2: list tags again
    tags = listTags(gitTagUri2);
    assertEquals(1, tags.length());
}

From source file:com.dvd.ui.UpdateCopy.java

private void updateBtn1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_updateBtn1ActionPerformed
    String dvdCodeTxt = dvdCodeTxt2.getText();
    String copyNumTxt = copyNumTxt2.getText();
    String isleNumTxt = isleNumTxt2.getText();
    String shelfNumTxt = shelfNumTxt2.getText();

    if (!dvdCodeTxt.isEmpty() && !copyNumTxt.isEmpty() && !isleNumTxt.isEmpty() && !shelfNumTxt.isEmpty()) {

        try {/*from ww  w  .j  a  v  a 2  s.  c  o  m*/
            if (isServiceOn) {
                String result = "";
                String url1 = "http://localhost:8080/updateCopyDVD?code=" + dvdCodeTxt + "&copyNumber="
                        + copyNumTxt + "&isleNumber=" + isleNumTxt + "&shelfNumber=" + shelfNumTxt;

                // String q = URLEncoder.encode(url1, "UTF-8").replace("+",
                // "%20");
                URL url = new URL(url1);

                URL obj = url;
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                // con.setRequestProperty(CONTENT_LENGTH,
                // Integer.toString(content.getBytes().length));
                con.setRequestProperty("Accept-Charset", "UTF-8");
                con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                con.setRequestProperty("Content-Language", "en-US");
                // optional default is GET
                con.setRequestMethod("POST");
                // add request header
                con.setRequestProperty("User-Agent", "Mozilla/5.0");

                int responseCode = con.getResponseCode();
                System.out.println("\nSending 'GET' request to URL : " + url);
                System.out.println("Response Code : " + responseCode);

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // print result
                result = response.toString();
                // Parse to get translated text

                JSONObject jsonObject = new JSONObject(result);
                int code = jsonObject.getJSONObject("responseCode").getInt("code");

                if (code == 201) {
                    errorcopyLabel3.setForeground(Color.BLACK);
                    errorcopyLabel3.setText("DVD Copy Successfully Updated.");
                } else {
                    errorcopyLabel3.setForeground(Color.RED);
                    errorcopyLabel3.setText("Updating DVD Copy failed");
                }

            } else {

                com.dvd.hibernate.repo.DVDRepository repository = new com.dvd.hibernate.repo.DVDRepository();

                DVDCopyDao copyDao = new DVDCopyDao();

                copyDao.setAvailable("1");
                copyDao.setCopyNumber(Integer.parseInt(copyNumTxt));
                copyDao.setDvdCode(Integer.parseInt(dvdCodeTxt));
                copyDao.setShelfNumber(shelfNumTxt);
                copyDao.setIsleNumber(isleNumTxt);

                repository.updateCopyDVD(copyDao);
                errorcopyLabel3.setForeground(Color.BLACK);
                errorcopyLabel3.setText("DVD Copy Successfully Updated.");

            }

        } catch (Exception e) {

            errorcopyLabel3.setForeground(Color.RED);
            errorcopyLabel3.setText("Updating DVD Copy failed");

        }

    } else {

        errorcopyLabel3.setForeground(Color.RED);
        errorcopyLabel3.setText("Updating DVD Copy failed");

    }
    // TODO add your handling code here:
}

From source file:org.uiautomation.ios.mobileSafari.events.inserted.ChildIframeInserted.java

public ChildIframeInserted(JSONObject message) throws JSONException {
    super(message);

    JSONObject params = message.optJSONObject("params").getJSONObject("node");

    JSONObject json = params.getJSONObject("contentDocument");
    contentDocument = new NodeId(json.getInt("nodeId"));
}

From source file:de.jaetzold.philips.hue.HueBridge.java

private void completeSync(String username) {
    try {/*from   w ww  . ja v a  2s. c o m*/
        final List<JSONObject> response = comm.request(GET, "api/" + username.trim(), "");
        if (response.size() > 0) {
            final JSONObject datastore = response.get(0);
            if (datastore.has("error")) {
                throw new HueCommException(datastore.getJSONObject("error"));
            }
            if (datastore.has("config") && datastore.has("lights") && datastore.has("groups")) {
                parseConfig(datastore.getJSONObject("config"));
                parseLights(datastore.getJSONObject("lights"));
                parseGroups(datastore.getJSONObject("groups"));
                this.setUsername(username);
                initialSyncDone = true;
            } else {
                throw new HueCommException("Incomplete response. Missing at least one of config/lights/groups");
            }
        } else {
            throw new HueCommException("Empty response");
        }
    } catch (IOException e) {
        throw new HueCommException(e);
    }
}

From source file:de.jaetzold.philips.hue.HueBridge.java

private void parseLights(JSONObject lightsJson) {
    final Iterator<?> keys = lightsJson.keys();
    while (keys.hasNext()) {
        Object key = keys.next();
        try {//w  w w . ja  v  a  2 s.  com
            Integer id = Integer.parseInt((String) key);
            HueLightBulb light = lights.get(id);
            if (light == null) {
                light = new HueLightBulb(this, id);
                lights.put(id, light);
            }

            final JSONObject lightJson = lightsJson.getJSONObject((String) key);
            light.parseLight(lightJson);
        } catch (Exception e) {
            if (e instanceof HueCommException) {
                throw e;
            } else {
                throw new HueCommException("Lights result parsing failed. Probably some unexpected format?", e);
            }
        }
    }
}

From source file:de.jaetzold.philips.hue.HueBridge.java

private void parseGroups(JSONObject groupsJson) {
    final Iterator<?> keys = groupsJson.keys();
    while (keys.hasNext()) {
        Object key = keys.next();
        try {/* ww  w .j a  v  a2s. c  o  m*/
            Integer id = Integer.parseInt((String) key);
            HueLightGroup group = groups.get(id);
            if (group == null) {
                group = new HueLightGroup(this, id);
                groups.put(id, group);
            }

            final JSONObject lightJson = groupsJson.getJSONObject((String) key);
            group.name = lightJson.getString("name");

            final JSONArray lightsArray = lightJson.getJSONArray("lights");
            for (int i = 0; i < lightsArray.length(); i++) {
                Integer lightId = Integer.parseInt(lightsArray.getString(i));
                final HueLightBulb light = getLight(lightId);
                if (light == null) {
                    //noinspection ThrowCaughtLocally
                    throw new HueCommException("Can not find light with id " + lightId);
                } else {
                    group.lights.put(lightId, light);
                }
            }

        } catch (Exception e) {
            if (e instanceof HueCommException) {
                throw e;
            } else {
                throw new HueCommException("Groups result parsing failed. Probably some unexpected format?", e);
            }
        }
    }
}