Example usage for org.json JSONObject optString

List of usage examples for org.json JSONObject optString

Introduction

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

Prototype

public String optString(String key) 

Source Link

Document

Get an optional string associated with a key.

Usage

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testCopyProjectNonDefaultLocation() throws IOException, SAXException, JSONException {
    //create workspace
    String workspaceName = WorkspaceServiceTest.class.getName() + "#testCopyProjectNonDefaultLocation";
    URI workspaceLocation = createWorkspace(workspaceName);

    String tmp = System.getProperty("java.io.tmpdir");
    File projectLocation = new File(new File(tmp), "Orion-testCopyProjectNonDefaultLocation");
    projectLocation.mkdir();/* w w w .j a v a2  s. c om*/
    toDelete.add(EFS.getLocalFileSystem().getStore(projectLocation.toURI()));
    ServletTestingSupport.allowedPrefixes = projectLocation.toString();

    //create a project
    String sourceName = "My Project";
    WebRequest request = getCreateProjectRequest(workspaceLocation, sourceName, projectLocation.toString());
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    String sourceLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    JSONObject responseObject = new JSONObject(response.getText());
    String sourceContentLocation = responseObject.optString(ProtocolConstants.KEY_CONTENT_LOCATION);
    assertNotNull(sourceContentLocation);

    //add a file in the project
    String fileName = "file.txt";
    request = getPostFilesRequest(toAbsoluteURI(sourceContentLocation), "{}", fileName);
    response = webConversation.getResource(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    assertEquals("Response should contain file metadata in JSON, but was " + response.getText(),
            "application/json", response.getContentType());
    JSONObject fileResponseObject = new JSONObject(response.getText());
    assertNotNull("No file information in response", fileResponseObject);
    checkFileMetadata(fileResponseObject, fileName, null, null, null, null, null, null, null, sourceName);

    // copy the project
    String destinationName = "Destination Project";
    request = getCopyMoveProjectRequest(workspaceLocation, destinationName, sourceLocation, false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    String destinationLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    String destinationContentLocation = responseObject.optString(ProtocolConstants.KEY_CONTENT_LOCATION);

    //assert the copy took effect
    assertFalse(sourceLocation.equals(destinationLocation));
    JSONObject resultObject = new JSONObject(response.getText());
    assertEquals(destinationName, resultObject.getString(ProtocolConstants.KEY_NAME));

    //ensure the source is still intact
    response = webConversation.getResponse(getGetRequest(sourceContentLocation + "?depth=1"));
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    resultObject = new JSONObject(response.getText());
    assertEquals(sourceName, resultObject.getString(ProtocolConstants.KEY_NAME));
    JSONArray children = resultObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, children.length());
    JSONObject child = children.getJSONObject(0);
    assertEquals(fileName, child.getString(ProtocolConstants.KEY_NAME));

    //ensure the destination is intact
    response = webConversation.getResponse(getGetRequest(destinationContentLocation + "?depth=1"));
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    resultObject = new JSONObject(response.getText());
    assertEquals(sourceName, resultObject.getString(ProtocolConstants.KEY_NAME));
    children = resultObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, children.length());
    child = children.getJSONObject(0);
    assertEquals(fileName, child.getString(ProtocolConstants.KEY_NAME));
}

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testCopyProject() throws IOException, SAXException, JSONException {
    //create workspace
    String workspaceName = WorkspaceServiceTest.class.getName() + "#testCopyProject";
    URI workspaceLocation = createWorkspace(workspaceName);

    //create a project
    String sourceName = "Source Project";
    WebRequest request = getCreateProjectRequest(workspaceLocation, sourceName, null);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    String sourceLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    JSONObject responseObject = new JSONObject(response.getText());
    String sourceContentLocation = responseObject.optString(ProtocolConstants.KEY_CONTENT_LOCATION);
    assertNotNull(sourceContentLocation);

    //add a file in the project
    String fileName = "file.txt";
    request = getPostFilesRequest(toAbsoluteURI(sourceContentLocation), "{}", fileName);
    response = webConversation.getResource(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

    // copy the project
    String destinationName = "Destination Project";
    request = getCopyMoveProjectRequest(workspaceLocation, destinationName, sourceLocation, false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    String destinationLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    String destinationContentLocation = responseObject.optString(ProtocolConstants.KEY_CONTENT_LOCATION);

    //assert the copy took effect
    assertFalse(sourceLocation.equals(destinationLocation));
    JSONObject resultObject = new JSONObject(response.getText());
    assertEquals(destinationName, resultObject.getString(ProtocolConstants.KEY_NAME));

    //ensure the source is still intact
    response = webConversation.getResponse(getGetRequest(sourceContentLocation + "?depth=1"));
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    resultObject = new JSONObject(response.getText());
    assertEquals(sourceName, resultObject.getString(ProtocolConstants.KEY_NAME));
    JSONArray children = resultObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, children.length());/*ww  w .  j  a v a  2 s.  co  m*/
    JSONObject child = children.getJSONObject(0);
    assertEquals(fileName, child.getString(ProtocolConstants.KEY_NAME));

    //ensure the destination is intact
    response = webConversation.getResponse(getGetRequest(destinationContentLocation + "?depth=1"));
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    resultObject = new JSONObject(response.getText());
    assertEquals(sourceName, resultObject.getString(ProtocolConstants.KEY_NAME));
    children = resultObject.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, children.length());
    child = children.getJSONObject(0);
    assertEquals(fileName, child.getString(ProtocolConstants.KEY_NAME));
}

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testCreateWorkspace() throws IOException, SAXException, JSONException {
    String workspaceName = WorkspaceServiceTest.class.getName() + "#testCreateWorkspace";
    WebResponse response = basicCreateWorkspace(workspaceName);

    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    String location = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    assertNotNull(location);/*from w  ww.  j  a v a  2s .c  om*/
    assertEquals("application/json", response.getContentType());
    JSONObject responseObject = new JSONObject(response.getText());
    assertNotNull("No workspace information in response", responseObject);
    assertNotNull(responseObject.optString(ProtocolConstants.KEY_ID));
    assertEquals(workspaceName, responseObject.optString(ProtocolConstants.KEY_NAME));
}

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testGetWorkspaceMetadata() throws IOException, SAXException, JSONException, URISyntaxException {
    //create workspace
    String workspaceName = WorkspaceServiceTest.class.getName() + "#testGetWorkspaceMetadata";
    WebResponse response = basicCreateWorkspace(workspaceName);
    String locationString = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    URI workspaceLocation = new URI(locationString);
    JSONObject workspace = new JSONObject(response.getText());
    String workspaceId = workspace.getString(ProtocolConstants.KEY_ID);

    //get workspace metadata and ensure it is correct
    WebRequest request = getGetRequest(workspaceLocation.toString());
    setAuthentication(request);/*from www  .j av  a2s .  c om*/
    response = webConversation.getResponse(request);
    workspace = new JSONObject(response.getText());
    assertNotNull(workspace);
    assertEquals(locationString, workspace.optString(ProtocolConstants.KEY_LOCATION));
    assertEquals(workspaceName, workspace.optString(ProtocolConstants.KEY_NAME));
    assertEquals(workspaceId, workspace.optString(ProtocolConstants.KEY_ID));
}

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testGetProjectMetadata() throws IOException, SAXException, JSONException {
    //create workspace
    String workspaceName = WorkspaceServiceTest.class.getName() + "#testGetProjectMetadata";
    URI workspaceLocation = createWorkspace(workspaceName);

    //create a project
    String sourceName = "testGetProjectMetadata Project";
    WebRequest request = getCreateProjectRequest(workspaceLocation, sourceName, null);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    String sourceLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);

    //now get the project metadata
    request = getGetRequest(sourceLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject responseObject = new JSONObject(response.getText());
    String sourceContentLocation = responseObject.optString(ProtocolConstants.KEY_CONTENT_LOCATION);
    assertEquals(sourceName, responseObject.optString(ProtocolConstants.KEY_NAME));
    assertNotNull(sourceContentLocation);
}

From source file:org.eclipse.orion.server.tests.servlets.workspace.WorkspaceServiceTest.java

@Test
public void testCreateWorkspaceNullName() throws IOException, SAXException, JSONException {
    //request with null workspace name is not allowed
    WebRequest request = getCreateWorkspaceRequest(null);
    WebResponse response = webConversation.getResponse(request);

    assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
    assertEquals("application/json", response.getContentType());
    //expecting a status response
    JSONObject responseObject = new JSONObject(response.getText());
    assertNotNull("No error response", responseObject);
    assertEquals("Error", responseObject.optString("Severity"));
    assertNotNull(responseObject.optString("message"));
}

From source file:org.smilec.smile.bu.json.StudentJSONParser.java

public static final Student process(JSONObject object) {

    String ip = object.optString(IP);
    String name = object.optString(NAME);

    Student student = new Student(ip, name);
    return student;

}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PhoneStateObj.java

private String asText(JSONObject obj) {
    StringBuilder status = new StringBuilder();
    String a = obj.optString(ACTION);
    String b = obj.optString(NUMBER);
    if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(a)) {
        status.append("Calling ");
    } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(a)) {
        status.append("Ending phone call with ");
    } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(a)) {
        status.append("Inbound call from ");
    }// w  w  w  . java2 s.c  o m
    status.append(b).append(".");
    return status.toString();
}

From source file:org.catnut.service.UpgradeService.java

private void checkout() throws Exception {
    URL url = new URL(METADATA_URL);
    InputStream inputStream = url.openStream();
    Scanner in = new Scanner(inputStream).useDelimiter("\\A");
    if (in.hasNext()) {
        JSONObject metadata = new JSONObject(in.next());
        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
        if (info.versionCode < metadata.optInt(FIELD_VERSION_CODE)) {
            Notification.InboxStyle style = new Notification.InboxStyle(mBuilder);
            String size = metadata.optString("size");
            style.setBigContentTitle(getString(R.string.find_new_version, size));
            JSONArray messages = metadata.optJSONArray("messages");
            for (int i = 0; i < messages.length(); i++) {
                style.addLine(messages.optString(i));
            }/*from  w  ww. j a va  2 s  . c o  m*/
            // download&upgrade intent
            Intent download = new Intent(this, UpgradeService.class);
            download.setAction(ACTION_DOWNLOAD);
            download.putExtra(DOWNLOAD_LINK, metadata.optString(DOWNLOAD_LINK));
            PendingIntent piDownload = PendingIntent.getService(this, 0, download, 0);
            mBuilder.addAction(R.drawable.ic_stat_download_dark, getString(R.string.down_load_and_upgrade),
                    piDownload);
            // dismiss notification
            Intent dismiss = new Intent(this, UpgradeService.class);
            dismiss.setAction(ACTION_DISMISS);
            PendingIntent piDismiss = PendingIntent.getService(this, 0, dismiss, 0);
            mBuilder.addAction(R.drawable.ic_stat_content_remove_dark, getString(R.string.not_upgrade_now),
                    piDismiss);
            // show it.
            mBuilder.setTicker(getString(R.string.find_new_version));
            mNotificationManager.notify(ID, mBuilder.setDefaults(Notification.DEFAULT_ALL).build());
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(UpgradeService.this, getString(R.string.already_updated), Toast.LENGTH_SHORT)
                            .show();
                }
            });
        }
    }
    in.close();
}

From source file:com.vk.sdkweb.api.model.VKApiComment.java

/**
 * Fills a Comment instance from JSONObject.
 *//*from  w  ww  .j a va  2 s  .c o m*/
public VKApiComment parse(JSONObject from) throws JSONException {
    id = from.optInt("id");
    from_id = from.optInt("from_id");
    date = from.optLong("date");
    text = from.optString("text");
    reply_to_user = from.optInt("reply_to_user");
    reply_to_comment = from.optInt("reply_to_comment");
    attachments.fill(from.optJSONArray("attachments"));
    JSONObject likes = from.optJSONObject("likes");
    this.likes = ParseUtils.parseInt(likes, "count");
    this.user_likes = ParseUtils.parseBoolean(likes, "user_likes");
    this.can_like = ParseUtils.parseBoolean(likes, "can_like");
    return this;
}