List of usage examples for org.json JSONObject optString
public String optString(String key, String defaultValue)
From source file:org.cfr.restlet.ext.shindig.resource.MakeRequestHandlerTest.java
@Test public void testFetchContentTypeFeed() throws Exception { String entryTitle = "Feed title"; String entryLink = "http://example.org/entry/0/1"; String entrySummary = "This is the summary"; String rss = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<rss version=\"2.0\"><channel>" + "<title>dummy</title>" + "<link>http://example.org/</link>" + "<item>" + "<title>" + entryTitle + "</title>" + "<link>" + entryLink + "</link>" + "<description>" + entrySummary + "</description>" + "</item>" + "</channel></rss>"; expectGetAndReturnBody(rss);/*www . j a va 2 s . co m*/ contextResource.getParameters().set(MakeRequestHandler.CONTENT_TYPE_PARAM, "FEED"); replay(); handler.fetch(contextResource); JSONObject results = extractJsonFromResponse(); JSONObject feed = new JSONObject(results.getString("body")); JSONObject entry = feed.getJSONArray("Entry").getJSONObject(0); assertEquals(entryTitle, entry.getString("Title")); assertEquals(entryLink, entry.getString("Link")); assertNull("getSummaries has the wrong default value (should be false).", entry.optString("Summary", null)); assertTrue(rewriter.responseWasRewritten()); }
From source file:org.marietjedroid.connect.MarietjeClient.java
/** * Gets the queue Blocks until we have it * /*from ww w . j a v a 2 s. c om*/ * @return * @throws MarietjeException */ public MarietjeTrack[] getQueue() throws MarietjeException { if (channel.getException() != null) throw channel.getException(); ArrayList<MarietjeRequest> queue = new ArrayList<MarietjeRequest>(); try { if (!this.followingQueue || this.channel.getRequests() == null) { this.followQueue(); this.requestsRetrieved.acquire(); } JSONArray requests = this.channel.getRequests(); for (int i = 0; requests.optJSONObject(i) != null; i++) { JSONObject req = requests.getJSONObject(i); JSONObject media = req.getJSONObject("media"); String requester = req.optString("byKey", DEFAULT_REQUESTER); queue.add(new MarietjeRequest(requester, req.getInt("key"), media)); } } catch (JSONException e) { throw new MarietjeException("JSON Error"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return queue.toArray(new MarietjeTrack[0]); }
From source file:com.github.rnewson.couchdb.lucene.couchdb.ViewSettings.java
public ViewSettings(final JSONObject json, final ViewSettings defaults) { this(json.optString("field", null), json.optString("index", null), json.optString("store", null), json.optString("type", null), json.optString("boost", null), json.optString("termvector", null), defaults);//from w w w . jav a2 s . co m }
From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java
protected JSONObject createProjectOrLink(URI workspaceLocation, String projectName, String contentLocation) throws JSONException, IOException, SAXException { JSONObject body = new JSONObject(); if (contentLocation != null) { body.put(ProtocolConstants.KEY_CONTENT_LOCATION, contentLocation); ServletTestingSupport.allowedPrefixes = contentLocation; }/*from www.jav a 2 s .c o m*/ WebRequest request = new PostMethodWebRequest(workspaceLocation.toString(), IOUtilities.toInputStream(body.toString()), "UTF-8"); if (projectName != null) request.setHeaderField(ProtocolConstants.HEADER_SLUG, projectName); request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1"); setAuthentication(request); WebResponse response = webConversation.getResponse(request); if (response.getResponseCode() != HttpURLConnection.HTTP_CREATED) { String msg = response.getText(); LogHelper .log(new org.eclipse.core.runtime.Status(IStatus.ERROR, "org.eclipse.orion.server.tests", msg)); assertTrue("Unexpected failure cloning: " + msg, false); } JSONObject project = new JSONObject(response.getText()); assertEquals(projectName, project.getString(ProtocolConstants.KEY_NAME)); String projectId = project.optString(ProtocolConstants.KEY_ID, null); assertNotNull(projectId); IPath workspacePath = new Path(workspaceLocation.getPath()); String workspaceId = workspacePath.segment(workspacePath.segmentCount() - 1); testProjectBaseLocation = "/" + workspaceId + '/' + projectName; testProjectLocalFileLocation = "/" + project.optString(ProtocolConstants.KEY_ID, null); return project; }
From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java
/** * Returns the HTTP resource location for the given workspace and project. * @param workspaceId/*w w w. jav a 2 s . c o m*/ * @param project */ protected IPath getClonePath(String workspaceId, JSONObject project) { String projectName = project.optString(ProtocolConstants.KEY_NAME, null); assertNotNull(projectName); IPath serverPath = new Path(AbstractServerTest.SERVER_URI.getPath()); return serverPath.append("file").append(workspaceId).append(projectName).makeAbsolute(); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java
protected static void assertGitSectionExists(JSONObject json) { JSONObject gitSection = json.optJSONObject(GitConstants.KEY_GIT); assertNotNull(gitSection);//from w w w. j a va2 s. co m assertNotNull(gitSection.optString(GitConstants.KEY_STATUS, null)); assertNotNull(gitSection.optString(GitConstants.KEY_DIFF, null)); assertNotNull(gitSection.optString(GitConstants.KEY_INDEX, null)); assertNotNull(gitSection.optString(GitConstants.KEY_HEAD, null)); assertNotNull(gitSection.optString(GitConstants.KEY_REMOTE, null)); assertNotNull(gitSection.optString(GitConstants.KEY_TAG, null)); assertNotNull(gitSection.optString(GitConstants.KEY_CLONE, null)); }