public CouchDb getDb(CouchClient client, URL dbUrl) throws Exception { CouchDbImpl couchDb = new CouchDbImpl(client, dbUrl); return couchDb; }
@Override public boolean documentExists(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 to verify existence", e); } return documentExists(docId); }
@Override public String downloadAttachment( JSONObject doc ,String name ,OutputStream os ) throws Exception { String docId = doc.getString("_id"); return downloadAttachment(docId, name, os); }
@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 Collection<JSONObject> getDocuments(List<String> docIds) throws Exception { return getDocuments(docIds, null); }
synchronized private URL getChangeUrl() throws Exception { if( null == changeUrl ){ changeUrl = new URL(couchDb.getUrl(),"_changes"); } return changeUrl; } }
@Override public String getDocumentRevision(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 to fetch revision", e); } return getDocumentRevision(docId); }
@Override public void uploadAttachment( JSONObject doc ,String name ,File file ,String contentType ) throws Exception { FileInputStream fis = null; try { long size = file.length(); fis = new FileInputStream(file); uploadAttachment(doc, name, fis, contentType, size); } catch (Exception e) { throw e; } finally { if( null != fis ) { try { fis.close(); } catch (Exception e) { // Ignore } } } }
@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 CouchDb getDatabase(String dbName) throws Exception { // Compute url URL effectiveUrl = new URL(url, dbName+"/"); CouchDbImpl couchDb = new CouchDbImpl(this, effectiveUrl); return couchDb; }
@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 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 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); }
public CouchDb getDb(CouchContext context, CouchDb clonedDb) throws Exception { CouchClient client = getClient(context, clonedDb.getClient()); CouchDbImpl couchDb = new CouchDbImpl(client, clonedDb.getUrl()); return couchDb; } }
@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; }