@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 CouchDb createDatabase(String dbName) throws Exception { // Compute url URL effectiveUrl = new URL(url, dbName+"/"); JSONObject response = ConnectionUtils.putJsonResource(context, effectiveUrl, null); ConnectionUtils.captureReponseErrors(response, "Error while creating database "+dbName+": "); CouchDbImpl db = new CouchDbImpl(this, effectiveUrl); return db; }
@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 void updateDocument(JSONObject doc) throws Exception { // Fetch document id String docId = null; try { docId = doc.getString("_id"); } catch(Exception e) { throw new Exception("Unable to find document id during update", e); } // Compute URL URL effectiveUrl = new URL(url, URLEncoder.encode(docId,"UTF-8")); // Update JSONObject response = ConnectionUtils.putJsonResource(getContext(), effectiveUrl, doc); ConnectionUtils.captureReponseErrors(response, "Error while updating "+docId+": "); // Parse response String revision = null; try { revision = response.getString("rev"); } catch(Exception e) { throw new Exception("Error parsing revision during update: "+docId, e); } doc.put("_rev", revision); }
@Override public void setSecurityDocument(CouchDbSecurityDocument security) throws Exception { URL securityUrl = new URL(url, "_security"); JSONObject jsonSecurity = security.getJSON(); if( null == jsonSecurity ){ jsonSecurity = new JSONObject(); } // Put document JSONObject response = ConnectionUtils.putJsonResource(getContext(), securityUrl, jsonSecurity); ConnectionUtils.captureReponseErrors(response, "Error while updating security document: "); } }
@Override public JSONObject deleteAttachment(JSONObject doc, String name) throws Exception { String docId = doc.getString("_id"); String path = URLEncoder.encode(docId,"UTF-8") + "/" + URLEncoder.encode(name,"UTF-8"); URL attachmentUrl = new URL(url, path); URL effectiveUrl = ConnectionUtils.computeUrlWithParameter(attachmentUrl, new UrlParameter("rev",doc.optString("_rev"))); // DELETE JSONObject response = ConnectionUtils.deleteJsonResource(getContext(), effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while deleting attachment "+name+" from "+docId+": "); return response; }
@Override public CouchContext createSession(String userName, String password) throws Exception { Map<String,String> form = new HashMap<String,String>(); form.put("name", userName); form.put("password", password); // Create a context to receive the cookie for authentication CouchContextCookie context = new CouchContextCookie(); // POST _session JSONObject jsonUserCtx = ConnectionUtils.postForm(context, url, form); ConnectionUtils.captureReponseErrors(jsonUserCtx, "Error while creating session context: "); // Parse response CouchUtils.authenticationContextFromDocument(jsonUserCtx); return context; }
@Override public void deleteDatabase(CouchDb couchDb) throws Exception { URL effectiveUrl = couchDb.getUrl(); JSONObject response = ConnectionUtils.deleteJsonResource(context, effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while deleting database "+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; }
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 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; }
ConnectionUtils.captureReponseErrors(response, "Error while creating document: ");
@Override public void deleteDocument(JSONObject doc) throws Exception { // Fetch document id String docId = null; String revision = null; try { docId = doc.getString("_id"); revision = doc.getString("_rev"); } catch(Exception e) { throw new Exception("Unable to find document id or revision during delete", e); } // Compute URL UrlParameter parameter = new UrlParameter("rev",revision); URL effectiveUrl = ConnectionUtils.computeUrlWithParameter( new URL(url, URLEncoder.encode(docId,"UTF-8")) ,parameter ); // DELETE JSONObject response = ConnectionUtils.deleteJsonResource(getContext(), effectiveUrl); ConnectionUtils.captureReponseErrors(response, "Error while deleting "+docId+": "); }
@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; }
); ConnectionUtils.captureReponseErrors(response, "Error while uploading attachment to "+docId+": ");
ConnectionUtils.captureReponseErrors(response, "Error while obtaining session context: ");
); ConnectionUtils.captureReponseErrors(response, "Error while updating "+docId+": ");
ConnectionUtils.captureReponseErrors(doc, "Error while fetching document "+docId+": ");