static public JSONObject getJsonResource(CouchContext ctxt, URL url) throws Exception { return getJsonResource(ctxt, url, JSONObject.class); }
@Override public void validateContext() throws Exception { // Compute url URL effectiveUrl = new URL(url, "_session"); JSONObject response = ConnectionUtils.getJsonResource(context, effectiveUrl, JSONObject.class); boolean ok = response.optBoolean("ok",false); if( false == ok ){ throw new Exception("Unable to log in with CouchDb server"); } }
@Override public List<String> listDatabases() throws Exception { // Compute url URL effectiveUrl = new URL(url, "_all_dbs"); JSONArray arr = ConnectionUtils.getJsonResource(context, effectiveUrl, JSONArray.class); // Parse response List<String> result = new Vector<String>(); try { for(int i=0,e=arr.length(); i<e; ++i) { result.add( arr.getString(i) ); } } catch(Exception e) { throw new Exception("Error parsing database list",e); } return result; }
@Override public JSONObject getDocument(String docId) throws Exception { URL docUrl = new URL(url, URLEncoder.encode(docId,"UTF-8")); JSONObject doc = ConnectionUtils.getJsonResource(getContext(), docUrl); ConnectionUtils.captureReponseErrors(doc, "Error while fetching document "+docId+": "); return doc; }
@Override public CouchServerVersion getVersion() throws Exception { if( null != version ) { return version; } else { JSONObject obj = ConnectionUtils.getJsonResource(context, url); // Parse version Object jsonVersion = obj.get("version"); if( null != jsonVersion && jsonVersion instanceof String ) { String[] versions = ((String)jsonVersion).split("\\."); if( versions.length >= 2 ) { int major = Integer.parseInt(versions[0]); int minor = Integer.parseInt(versions[1]); version = new CouchServerVersionImpl((String)jsonVersion,major,minor); return version; } } throw new Exception("Unable to parse server version: "+jsonVersion); } }
@Override public Collection<String> getAllDocIds() throws Exception { URL requestUrl = new URL(url, "_all_docs?include_docs=false"); JSONObject response = ConnectionUtils.getJsonResource(getContext(), requestUrl); ConnectionUtils.captureReponseErrors(response, "Error while fetching all doc ids: "); List<String> result = new Vector<String>(); try { JSONArray rows = response.getJSONArray("rows"); for(int loop=0,e=rows.length(); loop<e; ++loop){ JSONObject row = rows.getJSONObject(loop); String docId = row.getString("id"); result.add(docId); } } catch(Exception e) { throw new Exception("Error while interpreting the _all_docs response",e); } return result; }
@Override public JSONArray activeTasks() throws Exception { URL effectiveUrl = new URL(url, "_active_tasks"); JSONArray response = ConnectionUtils.getJsonResource(getContext(), effectiveUrl, JSONArray.class); ConnectionUtils.captureReponseErrors(response, "Error while obtaining active tasks"); return response; } }
@Override public String[] getUuids(int count) throws Exception { if( count < 1 ){ return new String[0]; } // Make request URL effectiveUrl = ConnectionUtils.computeUrlWithParameter(uuidUrl, new UrlParameter("count", ""+count)); JSONObject obj = ConnectionUtils.getJsonResource(context, effectiveUrl); // Parse Object uuidsObj = obj.get("uuids"); if( null == uuidsObj ) { throw new Exception("Unable to parse UUIDs response"); } if( false == (uuidsObj instanceof JSONArray) ) { throw new Exception("Unexpected class for array: "+uuidsObj.getClass().getSimpleName()); } JSONArray uuidsArray = (JSONArray)uuidsObj; String[] uuids = new String[uuidsArray.length()]; for(int loop=0, end=uuidsArray.length(); loop<end; ++loop) { String uuid = uuidsArray.getString(loop); uuids[loop] = uuid; } return uuids; }
JSONObject obj = ConnectionUtils.getJsonResource(context, effectiveUrl);
private JSONObject getChanges() throws Exception { List<UrlParameter> parameters = new ArrayList<UrlParameter>(3); parameters.add( new UrlParameter("since",""+lastSequence) ); parameters.add( new UrlParameter("feed","longpoll") ); parameters.add( new UrlParameter("timeout",""+LONG_POLL_TIMEOUT_MS) ); URL effectiveUrl = ConnectionUtils.computeUrlWithParameters(changeUrl, parameters); JSONObject response = ConnectionUtils.getJsonResource(context, effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while fetching changes: "); lastSequence = response.getLong("last_seq"); return response; }
@Override public CouchDbSecurityDocument getSecurityDocument() throws Exception { URL securityUrl = new URL(url, "_security"); JSONObject response = ConnectionUtils.getJsonResource(getContext(), securityUrl); ConnectionUtils.captureReponseErrors(response, "Error while fetching security document: "); CouchDbSecurityDocumentImpl security = new CouchDbSecurityDocumentImpl(response); return security; }
public CouchDbChangeMonitorThread(CouchContext context, URL changeUrl) throws Exception { this.context = context; this.changeUrl = changeUrl; // Compute last sequence List<UrlParameter> parameters = new ArrayList<UrlParameter>(3); parameters.add( new UrlParameter("limit","1") ); parameters.add( new UrlParameter("descending","true") ); parameters.add( new UrlParameter("feed","normal") ); URL effectiveUrl = ConnectionUtils.computeUrlWithParameters(changeUrl, parameters); JSONObject response = ConnectionUtils.getJsonResource(context, effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while fetching changes: "); lastSequence = response.getLong("last_seq"); }
@Override public String getDocumentRevision(String docId) throws Exception { // Compute URL List<UrlParameter> parameters = new ArrayList<UrlParameter>(3); parameters.add( new UrlParameter("include_docs","false") ); parameters.add( new UrlParameter("startkey","\""+docId+"\"") ); parameters.add( new UrlParameter("endkey","\""+docId+"\"") ); URL effectiveUrl = ConnectionUtils.computeUrlWithParameters(new URL(url, "_all_docs"), parameters); JSONObject response = ConnectionUtils.getJsonResource(getContext(), effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while fetching revision for "+docId+": "); // Parse response String revision = null; try { JSONArray rows = response.getJSONArray("rows"); JSONObject row = rows.getJSONObject(0); JSONObject doc = row.getJSONObject("value"); revision = doc.getString("rev"); } catch(Exception e) { throw new Exception("Error parsing document revision for: "+docId, e); } return revision; }
@Override public boolean documentExists(String docId) throws Exception { // Compute URL List<UrlParameter> parameters = new ArrayList<UrlParameter>(3); parameters.add( new UrlParameter("include_docs","false") ); parameters.add( new UrlParameter("startkey","\""+docId+"\"") ); parameters.add( new UrlParameter("endkey","\""+docId+"\"") ); URL effectiveUrl = ConnectionUtils.computeUrlWithParameters(new URL(url, "_all_docs"), parameters); JSONObject response = ConnectionUtils.getJsonResource(client.getContext(), effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while testing document existence for "+docId+": "); // Parse response boolean exists = false; try { JSONArray rows = response.getJSONArray("rows"); if( rows.length() > 0 ) { exists = true; } } catch(Exception e) { throw new Exception("Error parsing document existence for: "+docId, e); } return exists; }
JSONObject response = ConnectionUtils.getJsonResource(getContext(), url);
JSONObject response = ConnectionUtils.getJsonResource(getContext(), effectiveUrl);
JSONObject doc = ConnectionUtils.getJsonResource(getContext(), effectiveUrl);
T response = ConnectionUtils.getJsonResource(getContext(), effectiveUrl, expectedClass);