@Override public String downloadAttachment( String docId ,String name ,OutputStream os ) throws Exception { String path = URLEncoder.encode(docId,"UTF-8") + "/" + URLEncoder.encode(name,"UTF-8"); URL attachmentUrl = new URL(url, path); // GET String contentType = ConnectionUtils.getStreamResource( getContext() ,attachmentUrl ,os ); return contentType; }
@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 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 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 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 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 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; }
JSONObject response = ConnectionUtils.putJsonResource(getContext(), effectiveUrl, doc);
@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; }
getContext() ,effectiveUrl ,is
@Override synchronized public void addChangeListener(CouchDbChangeListener listener) throws Exception { if( !shuttingDown ) { if( null == thread ){ thread = new CouchDbChangeMonitorThread(couchDb.getContext(), getChangeUrl()); thread.start(); } thread.addChangeListener(listener); } }
JSONObject response = ConnectionUtils.getJsonResource(getContext(), effectiveUrl);
JSONObject doc = ConnectionUtils.getJsonResource(getContext(), effectiveUrl);