List of usage examples for org.json JSONObject getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testAddConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); JSONObject configResponse = listConfigEntries(gitConfigUri); JSONArray configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN); // initial number of config entries int initialConfigEntriesCount = configEntries.length(); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); assertConfigUri(entryLocation);/*from w w w .j a v a 2 s .co m*/ // get list of config entries again configResponse = listConfigEntries(gitConfigUri); configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(initialConfigEntriesCount + 1, configEntries.length()); entryLocation = null; for (int i = 0; i < configEntries.length(); i++) { JSONObject configEntry = configEntries.getJSONObject(i); if (ENTRY_KEY.equals(configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_KEY))) { assertConfigOption(configEntry, ENTRY_KEY, ENTRY_VALUE); break; } } // double check org.eclipse.jgit.lib.Config config = getRepositoryForContentLocation(contentLocation).getConfig(); assertEquals(ENTRY_VALUE, config.getString("a", "b", "c")); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testGetSingleConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); JSONObject configEntry = listConfigEntries(entryLocation); assertConfigOption(configEntry, ENTRY_KEY, ENTRY_VALUE); }//from w w w.j av a2 s . c om }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testUpdateConfigEntryUsingPOST() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); // update config entry using POST final String NEW_ENTRY_VALUE = "valueABC"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, NEW_ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CONFLICT, response.getResponseCode()); // get value of config entry JSONObject configEntry = listConfigEntries(entryLocation); // assert unchanged assertConfigOption(configEntry, ENTRY_KEY, ENTRY_VALUE); }/* ww w .j ava2 s.c o m*/ }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testUpdateConfigEntryUsingPUT() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); // update config entry using PUT final String NEW_ENTRY_VALUE = "v2"; request = getPutGitConfigRequest(entryLocation, NEW_ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject configEntry = listConfigEntries(entryLocation); assertConfigOption(configEntry, ENTRY_KEY, NEW_ENTRY_VALUE); }/*w w w . java 2 s. co m*/ }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testDeleteConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); // check if it exists request = getGetGitConfigRequest(entryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // delete config entry request = getDeleteGitConfigRequest(entryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // it shouldn't exist request = getGetGitConfigRequest(entryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); // so next delete operation should fail request = getDeleteGitConfigRequest(entryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); }// www . j a va 2 s .c o m }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testCreateInvalidConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); final String INVALID_ENTRY_KEY = "a"; // no name specified, dot missing final String ENTRY_VALUE = "v"; // try to set entry with invalid key request = getPostGitConfigRequest(gitConfigUri, INVALID_ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); }/*from ww w .j a v a2 s .c o m*/ }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testUpdateNonExistingConfigEntryUsingPUT() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; String invalidEntryLocation = gitConfigUri.replace(ConfigOption.RESOURCE, ConfigOption.RESOURCE + "/" + ENTRY_KEY); // check if it doesn't exist request = getGetGitConfigRequest(invalidEntryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); // try to update non-existing config entry using PUT (not allowed) request = getPutGitConfigRequest(invalidEntryLocation, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); }//from ww w . j a va 2 s.c o m }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testRequestWithMissingArguments() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; // missing key request = getPostGitConfigRequest(gitConfigUri, null, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); // missing value request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, null); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); // missing key and value request = getPostGitConfigRequest(gitConfigUri, null, null); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); // add some config request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); // put without value request = getPutGitConfigRequest(entryLocation, null); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); }/*from ww w . ja v a2s. c o m*/ }
From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java
/** * This method is called when the JavaScript sends a message to the native side. * This method should be overridden in subclasses. * @param message : the JSON-message sent by JavaScript. * @return true if the message was handled by the native, false otherwise * @details some basic operations are already implemented : navigation, logs, toasts, native alerts, web alerts * @details this method may be called from a secondary thread. *//* w w w . j a va 2 s. c om*/ // This method must be public !!! @JavascriptInterface public boolean onCobaltMessage(String message) { try { final JSONObject jsonObj = new JSONObject(message); // TYPE if (jsonObj.has(Cobalt.kJSType)) { String type = jsonObj.getString(Cobalt.kJSType); //CALLBACK if (type.equals(Cobalt.JSTypeCallBack)) { String callbackID = jsonObj.getString(Cobalt.kJSCallback); JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData); return handleCallback(callbackID, data); } // COBALT IS READY else if (type.equals(Cobalt.JSTypeCobaltIsReady)) { String versionWeb = jsonObj.optString(Cobalt.kJSVersion, null); String versionNative = getResources().getString(R.string.version_name); if (versionWeb != null && !versionWeb.equals(versionNative)) Log.e(TAG, "Warning : Cobalt version mismatch : Android Cobalt version is " + versionNative + " but Web Cobalt version is " + versionWeb + ". You should fix this. "); onCobaltIsReady(); return true; } // EVENT else if (type.equals(Cobalt.JSTypeEvent)) { String event = jsonObj.getString(Cobalt.kJSEvent); JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData); String callback = jsonObj.optString(Cobalt.kJSCallback, null); return handleEvent(event, data, callback); } // INTENT else if (type.equals(Cobalt.JSTypeIntent)) { String action = jsonObj.getString(Cobalt.kJSAction); // OPEN EXTERNAL URL if (action.equals(Cobalt.JSActionIntentOpenExternalUrl)) { JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String url = data.getString(Cobalt.kJSUrl); openExternalUrl(url); return true; } // UNHANDLED INTENT else { onUnhandledMessage(jsonObj); } } // LOG else if (type.equals(Cobalt.JSTypeLog)) { String text = jsonObj.getString(Cobalt.kJSValue); Log.d(Cobalt.TAG, "JS LOG: " + text); return true; } // NAVIGATION else if (type.equals(Cobalt.JSTypeNavigation)) { String action = jsonObj.getString(Cobalt.kJSAction); // PUSH if (action.equals(Cobalt.JSActionNavigationPush)) { JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String page = data.getString(Cobalt.kJSPage); String controller = data.optString(Cobalt.kJSController, null); push(controller, page); return true; } // POP else if (action.equals(Cobalt.JSActionNavigationPop)) { JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData); if (data != null) { String page = data.getString(Cobalt.kJSPage); String controller = data.optString(Cobalt.kJSController, null); pop(controller, page); } else pop(); return true; } // MODAL else if (action.equals(Cobalt.JSActionNavigationModal)) { JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String page = data.getString(Cobalt.kJSPage); String controller = data.optString(Cobalt.kJSController, null); String callbackId = jsonObj.optString(Cobalt.kJSCallback, null); presentModal(controller, page, callbackId); return true; } // DISMISS else if (action.equals(Cobalt.JSActionNavigationDismiss)) { // TODO: not present in iOS JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String controller = data.getString(Cobalt.kJSController); String page = data.getString(Cobalt.kJSPage); dismissModal(controller, page); return true; } //REPLACE else if (action.equals(Cobalt.JSActionNavigationReplace)) { JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String controller = data.getString(Cobalt.kJSController); String page = data.getString(Cobalt.kJSPage); boolean animated = data.optBoolean(Cobalt.kJSAnimated); replace(controller, page, animated); return true; } // UNHANDLED NAVIGATION else { onUnhandledMessage(jsonObj); } } // PLUGIN else if (type.equals(Cobalt.JSTypePlugin)) { mPluginManager.onMessage(mContext, this, jsonObj); } // UI else if (type.equals(Cobalt.JSTypeUI)) { String control = jsonObj.getString(Cobalt.kJSUIControl); JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); String callback = jsonObj.optString(Cobalt.kJSCallback, null); return handleUi(control, data, callback); } // WEB LAYER else if (type.equals(Cobalt.JSTypeWebLayer)) { String action = jsonObj.getString(Cobalt.kJSAction); // SHOW if (action.equals(Cobalt.JSActionWebLayerShow)) { final JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData); mHandler.post(new Runnable() { @Override public void run() { showWebLayer(data); } }); return true; } // UNHANDLED WEB LAYER else { onUnhandledMessage(jsonObj); } } // UNHANDLED TYPE else { onUnhandledMessage(jsonObj); } } // UNHANDLED MESSAGE else { onUnhandledMessage(jsonObj); } } catch (JSONException exception) { if (Cobalt.DEBUG) Log.e(Cobalt.TAG, TAG + " - onCobaltMessage: JSONException"); exception.printStackTrace(); } catch (NullPointerException exception) { if (Cobalt.DEBUG) Log.e(Cobalt.TAG, TAG + " - onCobaltMessage: NullPointerException"); exception.printStackTrace(); } return false; }
From source file:org.openhab.habdroid.model.OpenHABWidget.java
public OpenHABWidget(OpenHABWidget parent, JSONObject widgetJson) { this.parent = parent; this.children = new ArrayList<OpenHABWidget>(); this.mappings = new ArrayList<OpenHABWidgetMapping>(); try {//www.ja v a 2 s. c om if (widgetJson.has("item")) { this.setItem(new OpenHABItem(widgetJson.getJSONObject("item"))); } if (widgetJson.has("linkedPage")) { this.setLinkedPage(new OpenHABLinkedPage(widgetJson.getJSONObject("linkedPage"))); } if (widgetJson.has("mappings")) { JSONArray mappingsJsonArray = widgetJson.getJSONArray("mappings"); for (int i = 0; i < mappingsJsonArray.length(); i++) { JSONObject mappingObject = mappingsJsonArray.getJSONObject(i); OpenHABWidgetMapping mapping = new OpenHABWidgetMapping(mappingObject.getString("command"), mappingObject.getString("label")); mappings.add(mapping); } } if (widgetJson.has("type")) this.setType(widgetJson.getString("type")); if (widgetJson.has("widgetId")) this.setId(widgetJson.getString("widgetId")); if (widgetJson.has("label")) this.setLabel(widgetJson.getString("label")); if (widgetJson.has("icon")) this.setIcon(widgetJson.getString("icon")); if (widgetJson.has("url")) this.setUrl(widgetJson.getString("url")); if (widgetJson.has("minValue")) this.setMinValue((float) widgetJson.getDouble("minValue")); if (widgetJson.has("maxValue")) this.setMaxValue((float) widgetJson.getDouble("maxValue")); if (widgetJson.has("step")) this.setStep((float) widgetJson.getDouble("step")); if (widgetJson.has("refresh")) this.setRefresh(widgetJson.getInt("refresh")); if (widgetJson.has("period")) this.setPeriod(widgetJson.getString("period")); if (widgetJson.has("service")) this.setService(widgetJson.getString("service")); if (widgetJson.has("height")) this.setHeight(widgetJson.getInt("height")); if (widgetJson.has("iconcolor")) this.setIconColor(widgetJson.getString("iconcolor")); if (widgetJson.has("labelcolor")) this.setLabelColor(widgetJson.getString("labelcolor")); if (widgetJson.has("valuecolor")) this.setValueColor(widgetJson.getString("valuecolor")); if (widgetJson.has("encoding")) this.setEncoding(widgetJson.getString("encoding")); } catch (JSONException e) { e.printStackTrace(); } if (widgetJson.has("widgets")) { try { JSONArray childWidgetJsonArray = widgetJson.getJSONArray("widgets"); for (int i = 0; i < childWidgetJsonArray.length(); i++) { new OpenHABWidget(this, childWidgetJsonArray.getJSONObject(i)); } } catch (JSONException e) { e.printStackTrace(); } } this.parent.addChildWidget(this); }