Example usage for org.json JSONObject get

List of usage examples for org.json JSONObject get

Introduction

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

Prototype

public Object get(String key) throws JSONException 

Source Link

Document

Get the value object associated with a key.

Usage

From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java

@SuppressWarnings("unchecked")
@Override/*from  www.j av  a 2  s . c  o m*/
public List<Contacter> getContacterList(String userID, String dealerOrgID, String projectID, String customerID)
        throws ResponseException {
    ReleasableList<Contacter> contacterList = null;
    try {

        // ??.
        if (userID == null || dealerOrgID == null) {
            throw new ResponseException("userID or dealerOrgID is null.");
        }

        JSONObject params = new JSONObject();
        params.put("userID", userID);
        params.put("dealerOrgID", dealerOrgID);
        params.put("projectID", projectID);
        params.put("customerID", customerID);

        // ?Key
        String key = getKey(URLContact.METHOD_GET_CONTACTER_LIST, params);

        // ????
        contacterList = lruCache.get(key);
        if (contacterList != null && !contacterList.isExpired()) {
            return contacterList;
        }

        RLHttpResponse response = getHttpClient()
                .executePostJSON(getURLAddress(URLContact.METHOD_GET_CONTACTER_LIST), params, null);
        if (response.isSuccess()) {
            contacterList = new ArrayReleasableList<Contacter>();
            String data = getSimpleString(response);
            Log.i("getContacterList", data);
            JSONObject jsonBean = new JSONObject(data);
            JSONArray contacter = jsonBean.getJSONArray("result");
            int n = contacter.length();
            for (int i = 0; i < n; i++) {
                JSONObject json = contacter.getJSONObject(i);
                Contacter resultContacter = new Contacter();
                resultContacter.setContacterID(json.getString("contacterID"));
                resultContacter.setProjectID(String.valueOf(json.getString("projectID")));
                resultContacter.setCustomerID(String.valueOf(json.getString("customerID")));
                resultContacter.setContName(parsingString(json.get("contName")));
                resultContacter.setContMobile(parsingString(json.get("contMobile")));
                resultContacter.setContOtherPhone(parsingString(json.get("contOtherPhone")));
                resultContacter.setIsPrimContanter(String.valueOf(json.getBoolean("isPrimContanter")));
                resultContacter.setContGenderCode(parsingString(json.get("contGenderCode")));
                resultContacter.setContGender(parsingString(json.get("contGender")));
                resultContacter
                        .setContBirthday(String.valueOf(DataVerify.isZero(json.getString("contBirthday"))));
                resultContacter.setIdNumber(parsingString(json.get("idNumber")));
                resultContacter.setAgeScopeCode(parsingString(json.get("ageScopeCode")));
                resultContacter.setAgeScope(parsingString(json.get("ageScope")));
                resultContacter.setContType(parsingString(json.get("contType")));
                resultContacter.setContTypeCode(String.valueOf(json.getString("contTypeCode")));
                resultContacter.setContRelationCode(String.valueOf(json.getString("contRelationCode")));
                resultContacter.setContRelation(parsingString(json.get("contRelation")));
                resultContacter
                        .setLicenseValid(StringUtils.toLong(DataVerify.isZero(json.getString("licenseValid"))));
                contacterList.add(resultContacter);

            }
            // ?
            if (contacterList != null) {
                lruCache.put(key, contacterList);
            }
            return contacterList;
        }
        throw new ResponseException();
    } catch (IOException e) {
        Log.e(tag, "Connection network error.", e);
        throw new ResponseException(e);
    } catch (JSONException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e);
    }

}

From source file:com.microsoft.office365.starter.models.O365CalendarModel.java

private String getErrorMessage(String result) {
    String errorMessage = "";
    try {//from w w  w.  ja v a 2s  .  co  m

        // Gets the JSON object out of the result string
        String responsejSON = result.substring(result.indexOf("{"), result.length());

        JSONObject jObject = new JSONObject(responsejSON);
        JSONObject error = (JSONObject) jObject.get("error");
        errorMessage = error.getString("message");

    } catch (JSONException e) {
        e.printStackTrace();
        errorMessage = e.getMessage();
    }
    return errorMessage;
}

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

@Test
public void testBlameNoCommits() throws IOException, SAXException, JSONException, CoreException {
    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[] clonePaths = createTestProjects(workspaceLocation);

    for (IPath clonePath : clonePaths) {
        //clone a repo
        JSONObject clone = clone(clonePath);
        String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

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

        // get blameUri
        JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
        String gitBlameUri = gitSection.getString(GitConstants.KEY_BLAME);

        // blame request
        request = getGetGitBlameRequest(gitBlameUri);
        response = webConversation.getResource(request);
        assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());

        // test/*from   w  w  w  . j a v  a2 s. com*/
        JSONObject blameObject = new JSONObject(response.getText());

        assertEquals(blameObject.length(), 4);
        assertEquals(blameObject.get("Severity"), "Error");
        assertEquals(blameObject.get("HttpCode"), 400);
        assertEquals(blameObject.get("Code"), 0);
    }
}

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

@Test
public void testBlameOneCommit() throws IOException, SAXException, JSONException, CoreException {

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

    for (IPath clonePath : clonePaths) {
        //clone a repo
        JSONObject clone = clone(clonePath);
        String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        //get project/folder metadata
        WebRequest request = getGetRequest(cloneContentLocation);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject folder = new JSONObject(response.getText());
        JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
        String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

        //create and modify file
        JSONObject testTxt = getChild(folder, "test.txt");
        modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");

        //commit/*from w  w  w  .  j a  va  2s.  com*/
        addFile(testTxt);
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // get blame Uri
        JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
        String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);

        // blame request
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        // get BlameInfo
        JSONObject blameObject = new JSONObject(response.getText());

        //Test
        assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");

        JSONArray blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        JSONArray children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        JSONObject child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 4);

    }
}

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

@Test
public void testBlameMultiCommit() throws IOException, SAXException, JSONException, CoreException {

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

    for (IPath clonePath : clonePaths) {
        //clone a repo
        JSONObject clone = clone(clonePath);
        String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        //get project/folder metadata
        WebRequest request = getGetRequest(cloneContentLocation);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject folder = new JSONObject(response.getText());
        JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
        String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

        //create file test.txtx
        JSONObject testTxt = getChild(folder, "test.txt");
        modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");

        //commit the file
        addFile(testTxt);//from ww  w.  j av a  2 s. com
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // get the blame uri for this file
        JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
        String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);

        // blame the file
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        // testing
        JSONObject blameObject = new JSONObject(response.getText());

        // non blame info tests
        JSONArray blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        JSONArray children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        JSONObject child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 4);

        //save commit info to test
        String commitLocation1 = blameObject.getString(GitConstants.KEY_COMMIT);
        int commitTime1 = blameObject.getInt(GitConstants.KEY_COMMIT_TIME);
        String commitId1 = blameObject.getString(ProtocolConstants.KEY_NAME);

        // modify the file
        modifyFile(testTxt, "LINE ONE \n LINE TWO \n LINE THREE \n LINE FOUR \n LINE FIVE");
        addFile(testTxt);

        //commit the new changes
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // get blame uri
        testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
        blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);

        // blame file
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        // test
        blameObject = new JSONObject(response.getText());

        // non blame info tests
        assertEquals(blameObject.length(), 4);
        assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
        assertEquals(blameObject.getString(ProtocolConstants.KEY_LOCATION), blameUri);

        blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));

        // test first commit
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);

        //test second commit
        blameObject = blame.getJSONObject(0);

        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 5);

        String commitLocation2 = blameObject.getString(GitConstants.KEY_COMMIT);
        int commitTime2 = blameObject.getInt(GitConstants.KEY_COMMIT_TIME);
        String commitId2 = blameObject.getString(ProtocolConstants.KEY_NAME);

        // test that there are not duplicates of the same commit
        assertNotSame(commitId1, commitId2);
        assertNotSame(commitLocation1, commitLocation2);
        assertNotSame(commitTime1, commitTime2);

    }
}

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

@Test
public void testBlameMultiFile() throws IOException, SAXException, JSONException, CoreException {

    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[] clonePaths = createTestProjects(workspaceLocation);
    for (IPath clonePath : clonePaths) {
        //clone a repo
        JSONObject clone = clone(clonePath);
        String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

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

        JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
        String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

        /*//from  w  w w.  j a v  a2 s .  c om
         * Commit 1
         */
        // create the original file and modify it
        JSONObject testTxt = getChild(folder, "test.txt");
        modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");

        //commit file
        addFile(testTxt);
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // get blameURI
        JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
        String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);

        // make blame request
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        // test
        JSONObject blameObject = new JSONObject(response.getText());
        assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
        assertEquals(blameObject.getString(ProtocolConstants.KEY_LOCATION), blameUri);

        //test blameInfo
        JSONArray blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        JSONArray children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        JSONObject child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 4);

        /*
         * commit 2 - different file
         */

        // create a second file in a different folder
        JSONObject newfolder = getChild(folder, "folder");
        JSONObject folderTxt = getChild(newfolder, "folder.txt");
        modifyFile(folderTxt, "commit me");

        // commit the new file
        addFile(folderTxt);
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit on testFile2.txt", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // make blame request
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        /*
         * These tests should produce the same results as the above tests
         * They should be not affected by the recent commit
         */
        blameObject = new JSONObject(response.getText());
        assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
        assertEquals(blameObject.getString(ProtocolConstants.KEY_LOCATION), blameUri);

        //test blameInfo
        blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 4);

        /*
         * commit 3 - original file
         */

        // modify original file
        modifyFile(testTxt, "LINE ONE \n LINE TWO \n LINE THREE \n LINE FOUR \n LINE FIVE");

        //commit the changes
        addFile(testTxt);
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "modified testFile.txt", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);

        // do blame computation
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        blameObject = new JSONObject(response.getText());
        assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
        assertEquals(blameObject.getString(ProtocolConstants.KEY_LOCATION), blameUri);

        // test
        blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN));
        assertEquals(blame.length(), 1);
        blameObject = blame.getJSONObject(0);
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME));
        assertNotNull(blameObject.get(GitConstants.KEY_COMMIT));
        assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME));
        children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        child = children.getJSONObject(0);
        assertEquals(children.length(), 1);
        assertEquals(child.get(GitConstants.KEY_START_RANGE), 1);
        assertEquals(child.get(GitConstants.KEY_END_RANGE), 5);

    }
}

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

@Test
public void testFolderBlame() throws IOException, SAXException, JSONException, CoreException {

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

    for (IPath clonePath : clonePaths) {
        //clone a repo
        JSONObject clone = clone(clonePath);
        String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        //get project/folder metadata
        WebRequest request = getGetRequest(cloneContentLocation);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject folder = new JSONObject(response.getText());
        JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
        String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

        JSONObject newfolder = getChild(folder, "folder");

        //modifyFile(newfolder, "commit me");

        // commit the new file
        addFile(newfolder);//from ww  w  .j  a va2s  . c o  m
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit on testFile2.txt", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject newfolderGitSection = newfolder.getJSONObject(GitConstants.KEY_GIT);
        String blameUri = newfolderGitSection.getString(GitConstants.KEY_BLAME);

        // blame request
        request = getGetGitBlameRequest(blameUri);
        response = webConversation.getResource(request);

        // get BlameInfo
        JSONObject blameObject = new JSONObject(response.getText());

        //Test

        assertEquals(blameObject.length(), 4);

        assertEquals(blameObject.get("Severity"), "Error");
        assertEquals(blameObject.get("HttpCode"), 400);
        assertEquals(blameObject.get("Code"), 0);

    }
}

From source file:org.loklak.api.iot.AbstractPushServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Query post = RemoteAccess.evaluate(request);
    String remoteHash = Integer.toHexString(Math.abs(post.getClientHost().hashCode()));

    // manage DoS
    if (post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;//  w  ww  .  j  a va  2  s.  co  m
    }

    String url = post.get("url", "");
    if (url == null || url.length() == 0) {
        response.sendError(400, "your request does not contain an url to your data object");
        return;
    }
    String screen_name = post.get("screen_name", "");
    if (screen_name == null || screen_name.length() == 0) {
        response.sendError(400, "your request does not contain required screen_name parameter");
        return;
    }

    JSONObject map;
    String jsonString = "";
    try {
        jsonString = new String(ClientConnection.download(url), StandardCharsets.UTF_8);
        map = new JSONObject(jsonString);
    } catch (Exception e) {
        response.sendError(400, "error reading json file from url");
        return;
    }

    // validation phase
    ProcessingReport report = this.validator.validate(jsonString);
    if (!report.isSuccess()) {
        response.sendError(400,
                "json does not conform to schema : " + this.getValidatorSchema().name() + "\n" + report);
        return;
    }

    // conversion phase
    Object extractResults = extractMessages(map);
    JSONArray messages;
    if (extractResults instanceof JSONArray) {
        messages = (JSONArray) extractResults;
    } else if (extractResults instanceof JSONObject) {
        messages = new JSONArray();
        messages.put((JSONObject) extractResults);
    } else {
        throw new IOException("extractMessages must return either a List or a Map. Get "
                + (extractResults == null ? "null" : extractResults.getClass().getCanonicalName())
                + " instead");
    }
    JSONArray convertedMessages = this.converter.convert(messages);

    PushReport nodePushReport = new PushReport();
    ObjectWriter ow = new ObjectMapper().writerWithDefaultPrettyPrinter();
    // custom treatment for each message
    for (int i = 0; i < messages.length(); i++) {
        JSONObject message = (JSONObject) convertedMessages.get(i);
        message.put("source_type", this.getSourceType().toString());
        message.put("location_source", LocationSource.USER.name());
        message.put("place_context", PlaceContext.ABOUT.name());
        if (message.get("text") == null) {
            message.put("text", "");
        }

        // append rich-text attachment
        String jsonToText = ow.writeValueAsString(messages.get(i));
        message.put("text", message.get("text") + MessageEntry.RICH_TEXT_SEPARATOR + jsonToText);
        customProcessing(message);

        if (message.get("mtime") == null) {
            String existed = PushServletHelper.checkMessageExistence(message);
            // message known
            if (existed != null) {
                messages.remove(i);
                nodePushReport.incrementKnownCount(existed);
                continue;
            }
            // updated message -> save with new mtime value
            message.put("mtime", Long.toString(System.currentTimeMillis()));
        }

        try {
            message.put("id_str", PushServletHelper.computeMessageId(message, getSourceType()));
        } catch (Exception e) {
            DAO.log("Problem computing id : " + e.getMessage());
        }
    }
    try {
        PushReport savingReport = PushServletHelper.saveMessagesAndImportProfile(convertedMessages,
                jsonString.hashCode(), post, getSourceType(), screen_name);
        nodePushReport.combine(savingReport);
    } catch (IOException e) {
        response.sendError(404, e.getMessage());
        return;
    }
    String res = PushServletHelper.buildJSONResponse(post.get("callback", ""), nodePushReport);

    post.setResponse(response, "application/javascript");
    response.getOutputStream().println(res);
    DAO.log(request.getServletPath() + " -> records = " + nodePushReport.getRecordCount() + ", new = "
            + nodePushReport.getNewCount() + ", known = " + nodePushReport.getKnownCount() + ", error = "
            + nodePushReport.getErrorCount() + ", from host hash " + remoteHash);
}

From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerDTO.java

private PropertyContainerDTO(JSONObject json) {
    if (json.has(JSONAttributes.CCLASS)) {
        containerClass = new PropertyContainerClassDTO.Builder(json.getJSONObject(JSONAttributes.CCLASS))
                .build();//from   ww w  .j  a  v  a 2 s  .c om
    }
    if (json.has(JSONAttributes.VALUES)) {
        values = new HashMap<>();
        JSONObject jp = json.getJSONObject(JSONAttributes.VALUES);
        for (Object o : jp.keySet()) {
            String key = o.toString();
            Object v = jp.get(key);
            if (JSONObject.NULL.equals(v)) {
                values.put(key, null);
            } else if (v instanceof Serializable || v instanceof JSONArray || v instanceof JSONObject) {
                values.put(key, v);
            } else {
                throw new HobsonRuntimeException("Invalid property value for " + key + ": " + v);
            }
        }
    }
}

From source file:org.jkan997.slingbeans.slingfs.FileObjectAttribute.java

public void setJsonValue(JSONObject jsonObj, String propName) {
    propertyName = propName;/*from w  ww .ja  v a 2s.  c o m*/
    Object value = jsonObj.get(propName);
    String name = jsonObj.getString(":" + propName);
    type = JcrTypeHelper.getType(name, value);
    this.readOnly = readOnlyProps.contains(propertyName);
    hidden = (propName.startsWith(":"));
    if (type == PropertyType.DATE) {
        convertedValue = ISO8601.parseToDate(value.toString());
        this.readOnly = true;
    } else if (type == PropertyType.LONG) {
        convertedValue = ((Number) value).longValue();
    } else if (type == PropertyType.DOUBLE) {
        convertedValue = (Double) value;
    } else if (type == PropertyType.BOOLEAN) {
        convertedValue = (Boolean) value;
    } else if (type == PropertyType.STRING) {
        convertedValue = (String) value;
    } else if (value != null) {
        convertedValue = value.toString();
        readOnly = true;
    } else {
        convertedValue = "null";
        readOnly = true;
    }
}