public List<String> getAllDatabases() { return restTemplate.get("/_all_dbs", new StdResponseHandler<List<String>>(){ @Override public List<String> success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), STRING_LIST_TYPE_DEF); } }); }
@Override public <T> T executeQuery(ViewQuery query, ResponseCallback<T> rh) { LOG.debug("Querying CouchDb view at {}.", query); T result; if (!query.isCacheOk()) { result = query.hasMultipleKeys() ? getRestTemplate().postUncached(query.buildQuery(), query.getKeysAsJson(), rh) : getRestTemplate().getUncached(query.buildQuery(), rh); } else { result = query.hasMultipleKeys() ? getRestTemplate().post(query.buildQuery(), query.getKeysAsJson(), rh) : getRestTemplate().get(query.buildQuery(), rh); } LOG.debug("Answer from view query: {}.", result); return result; } }
@Override public Collection<ActiveTask> getActiveTasks() { String url = "/_active_tasks"; List<StdActiveTask> tasks = restTemplate.get(url, new StdResponseHandler<List<StdActiveTask>>() { @Override public List<StdActiveTask> success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), new TypeReference<List<StdActiveTask>>() {}); } }); // We have to copy the list here because Java lacks covariance (i.e. we can't just return // the List<StdActiveTask> because it's not a Collection<ActiveTask>). Collection<ActiveTask> ret = new ArrayList<ActiveTask>(); for (StdActiveTask task : tasks) { ret.add(task); } return ret; } }
@Override public Security getSecurity() { return restTemplate.get(securityPath(), new StdResponseHandler<Security>() { @Override public Security success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), Security.class); } } ); }
@Override public DbInfo getDbInfo() { return restTemplate.get(dbURI.toString(), new StdResponseHandler<DbInfo>() { @Override public DbInfo success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), DbInfo.class); } } ); }
@Override public <T> T getConfiguration(final Class<T> c, String section, String key) { Assert.notNull(c, "Class may not be null"); String url = "/_config"; if(section != null) { url = url + "/" + section; if(key != null) { url = url + "/" + key; } } return restTemplate.get(url, new StdResponseHandler<T>() { @Override public T success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), c); } }); }
@Override public List<String> getAllDocIds() { return restTemplate.get(dbURI.append("_all_docs").toString(), docIdResponseHandler); }
@Override public int getRevisionLimit() { return restTemplate.get(dbURI.append("_revs_limit").toString(), new StdResponseHandler<Integer>() { @Override public Integer success(HttpResponse hr) throws Exception { JsonNode rlimit = objectMapper.readTree(hr.getContent()); return rlimit.asInt(); } }); }
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(MyApp.class) @WebIntegrationTest("server.port:9000") public class MyTest { private static RestTemplate restTemplate = new TestRestTemplate(); @BeforeClass public static void setup() { // configure proxy host and port here restTemplate = new TestRestTemplate(); } @Test public void test() { restTemplate.get(URI.create("http://localhost:9000/your-app", Object.class); } }
@Override public InputStream getAsStream(String id, Options options) { URI uri = dbURI.append(id); applyOptions(options, uri); HttpResponse r = restTemplate.get(uri.toString()); return r.getContent(); }
private InputStream fetchChangesAsStream(ChangesCommand cmd) { HttpResponse r = restTemplate.get(dbURI.append(cmd.toString()) .toString()); return r.getContent(); }
@Override public List<Revision> getRevisions(String id) { assertDocIdHasValue(id); return restTemplate.get(dbURI.append(id).param("revs_info", "true") .toString(), new StdResponseHandler<List<Revision>>() { @Override public List<Revision> success(HttpResponse hr) throws Exception { JsonNode root = objectMapper.readValue(hr.getContent(), JsonNode.class); List<Revision> revs = new ArrayList<Revision>(); for (Iterator<JsonNode> i = root.get("_revs_info") .elements(); i.hasNext();) { JsonNode rev = i.next(); revs.add(new Revision(rev.get("rev").textValue(), rev .get("status").textValue())); } return revs; } @Override public List<Revision> error(HttpResponse hr) { if (hr.getCode() == HttpStatus.NOT_FOUND) { return Collections.emptyList(); } return super.error(hr); } }); }
private AttachmentInputStream getAttachment(String attachmentId, URI uri) { HttpResponse r = restTemplate.get(uri.toString()); return new AttachmentInputStream(attachmentId, r.getContent(), r.getContentType(), r.getContentLength()); }
@Override public DesignDocInfo getDesignDocInfo(String designDocId) { Assert.hasText(designDocId, "designDocId may not be null or empty"); String uri = dbURI.append("_design").append(designDocId) .append("_info").toString(); return restTemplate.get(uri, new StdResponseHandler<DesignDocInfo>() { @Override public DesignDocInfo success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), DesignDocInfo.class); } }); }
@Override public <T> T get(final Class<T> c, String id, Options options) { Assert.notNull(c, "Class may not be null"); assertDocIdHasValue(id); URI uri = dbURI.append(id); applyOptions(options, uri); return restTemplate.get(uri.toString(), new StdResponseHandler<T>() { @Override public T success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), c); } }); }
@Override public StreamingChangesResult changesAsStream(ChangesCommand cmd) { if (cmd.continuous) { throw new IllegalArgumentException( "ChangesCommand may not declare continous = true while calling changes"); } ChangesCommand actualCmd = new ChangesCommand.Builder().merge(cmd) .continuous(false).build(); HttpResponse response = restTemplate.get(dbURI.append(actualCmd.toString()) .toString()); return new StreamingChangesResult(objectMapper, response); }
@Override public <T> T find(final Class<T> c, String id, Options options) { Assert.notNull(c, "Class may not be null"); assertDocIdHasValue(id); URI uri = dbURI.append(id); applyOptions(options, uri); return restTemplate.get(uri.toString(), new StdResponseHandler<T>() { @Override public T success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), c); } @Override public T error(HttpResponse hr) { return hr.getCode() == HttpStatus.NOT_FOUND ? null : super.error(hr); } }); }