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 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; }
static public <T> T getJsonFromInputStream(InputStream contentStream, String encoding, Class<T> clazz) throws Exception { Object json = getJsonFromInputStream(contentStream, encoding); if( clazz.isAssignableFrom(json.getClass()) ) { T result = clazz.cast(json); return result; } throw new Exception("Unexpected returned object type: "+json.getClass().getSimpleName()); }
@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+": "); }
@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 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; } }
static public <T> T getJsonResource(CouchContext ctxt, URL url, Class<T> clazz) throws Exception { CouchContextBase context = CouchContextBase.getBase(ctxt); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); context.adjustConnection(conn); conn.setDoOutput(false); conn.setDoInput(true); conn.connect(); updateContextFromHeaderFields(ctxt,conn.getHeaderFields()); try { checkResponseForError(conn); } catch(Exception e) { try { conn.disconnect(); } catch(Exception e2) { // Ignore } throw e; } String contentEncoding = conn.getContentEncoding(); InputStream contentStream = conn.getInputStream(); T result = getJsonFromInputStream(contentStream, contentEncoding, clazz); contentStream.close(); conn.disconnect(); return result; }
+ URLEncoder.encode(name,"UTF-8"); URL attachmentUrl = new URL(url, path); URL effectiveUrl = ConnectionUtils.computeUrlWithParameter(attachmentUrl, new UrlParameter("rev",doc.optString("_rev"))); JSONObject response = ConnectionUtils.putStreamResource( getContext() ,effectiveUrl ); ConnectionUtils.captureReponseErrors(response, "Error while uploading attachment to "+docId+": ");
@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; }
conn.setDoOutput(false); conn.connect(); updateContextFromHeaderFields(ctxt,conn.getHeaderFields()); try { checkResponseForError(conn); } catch(Exception e) { try {
@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; }
JSONObject response = ConnectionUtils.putStreamResource( couchDb.getContext() ,effectiveUrl ); ConnectionUtils.captureReponseErrors(response, "Error while updating "+docId+": ");
JSONObject response = ConnectionUtils.postJsonResource(getContext(), effectiveUrl, request); ConnectionUtils.captureReponseErrors(response, "Error while replicating "+source+" -> "+target);
static public JSONObject getJsonResource(CouchContext ctxt, URL url) throws Exception { return getJsonResource(ctxt, url, JSONObject.class); }
@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; }
static public URL computeUrlWithParameter(URL url, UrlParameter parameter) throws Exception { List<UrlParameter> parameters = new ArrayList<UrlParameter>(1); parameters.add(parameter); return computeUrlWithParameters(url, parameters); }
static public JSONObject deleteJsonResource(CouchContext ctxt, URL url) throws Exception { CouchContextBase context = CouchContextBase.getBase(ctxt); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("DELETE"); context.adjustConnection(conn); conn.setDoOutput(false); conn.setDoInput(true); conn.connect(); updateContextFromHeaderFields(ctxt,conn.getHeaderFields()); try { checkResponseForError(conn); } catch(Exception e) { try { conn.disconnect(); } catch(Exception e2) { // Ignore } throw e; } String contentEncoding = conn.getContentEncoding(); InputStream contentStream = conn.getInputStream(); JSONObject result = getJsonFromInputStream(contentStream, contentEncoding, JSONObject.class); contentStream.close(); conn.disconnect(); 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 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: "); } }
URL effectiveUrl = ConnectionUtils.computeUrlWithParameter(uuidUrl, new UrlParameter("count", "10")); JSONObject obj = ConnectionUtils.getJsonResource(context, effectiveUrl);