private void removeDatabase() { HttpClient connection = couchDbConnector.getConnection(); RestTemplate restTemplate = new RestTemplate(connection); restTemplate.delete(couchDbConnector.path()); couchDbConnector.createDatabaseIfNotExists(); }
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); } }); }
protected void testConnect(HttpClient client) { try { RestTemplate rt = new RestTemplate(client); rt.head("/", new StdResponseHandler<Void>()); } catch (Exception e) { throw new BeanCreationException(String.format("Cannot connect to CouchDb@%s:%s", host, port), e); } }
@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; } }
public StdCouchDbInstance(HttpClient client, ObjectMapperFactory of) { Assert.notNull(client, "HttpClient may not be null"); Assert.notNull(of, "ObjectMapperFactory may not be null"); this.client = client; this.restTemplate = new RestTemplate(client); this.objectMapper = of.createObjectMapper(); this.objectMapperFactory = of; }
@Override public String deleteConfiguration(String section, String key) { Assert.notNull(section, "Section may not be null"); Assert.notNull(key, "Key may not be null"); String url = "/_config/" + section + "/" + key; return restTemplate.delete(url, new StdResponseHandler<String>() { @Override public String success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), String.class); } }); }
public <T> T put(String path, String content, ResponseCallback<T> callback) { return handleResponse(callback, client.put(path, content)); }
@Override public boolean contains(String id) { return restTemplate.head(URIWithDocId(id), new ResponseCallback<Boolean>() { @Override public Boolean error(HttpResponse hr) { if (hr.getCode() == HttpStatus.NOT_FOUND) { return Boolean.FALSE; } throw new DbAccessException(hr.toString()); } @Override public Boolean success(HttpResponse hr) throws Exception { return Boolean.TRUE; } }); }
private HttpResponse queryForHttpResponse(ViewQuery query) { Assert.notNull(query, "query cannot be null"); query.dbPath(dbURI.toString()); return query.hasMultipleKeys() ? restTemplate.postUncached(query.buildQuery(), query.getKeysAsJson()) : restTemplate.getUncached( query.buildQuery()); }
public void put(String path, InputStream data, String contentType, long contentLength) { handleVoidResponse(client.put(path, data, contentType, contentLength)); }
public HttpResponse getUncached(String path) { return handleRawResponse(client.getUncached(path)); }
@Override public String copy(String sourceDocId, String targetDocId, String targetRevision) { String destinationUri = targetRevision != null ? targetDocId + "?rev=" + targetRevision : targetDocId; return restTemplate.copy(dbURI.append(sourceDocId).toString(), destinationUri, revisionHandler).getRevision(); }
@Override public ChangesFeed changesFeed(ChangesCommand cmd) { int heartbeat = cmd.heartbeat > 0 ? cmd.heartbeat : DEFAULT_HEARTBEAT_INTERVAL; String since = cmd.since != null ? cmd.since : getDbInfo().getUpdateSeqAsString(); ChangesCommand actualCmd = new ChangesCommand.Builder().merge(cmd) .continuous(true).heartbeat(heartbeat).since(since).build(); return new ContinuousChangesFeed(dbName, restTemplate.getUncached(dbURI.append(actualCmd.toString()).toString())); }
public StdCouchDbConnector(String databaseName, CouchDbInstance dbi, ObjectMapperFactory om) { Assert.hasText(databaseName, "DatabaseName may not be null or empty"); Assert.notNull(dbi, "CouchDbInstance may not be null"); Assert.notNull(om, "ObjectMapperFactory may not be null"); DbPath dbp = DbPath.fromString(databaseName); dbName = dbp.getDbName(); this.dbURI = URI.prototype(dbp.getPath()); this.dbInstance = dbi; this.objectMapper = om.createObjectMapper(this); this.jsonSerializer = new StreamingJsonSerializer(objectMapper); this.restTemplate = new RestTemplate(dbi.getConnection()); this.revisionHandler = new RevisionResponseHandler(objectMapper); this.docIdResponseHandler = new DocIdResponseHandler(objectMapper); this.queryExecutor = new DefaultQueryExecutor(this.restTemplate); collectionBulkExecutor = new BulkOperationCollectionBulkExecutor(dbURI, restTemplate, objectMapper) { @Override protected JsonSerializer getJsonSerializer() { return jsonSerializer; } }; localBulkBuffer = new DefaultLocalBulkBuffer() { @Override protected BulkExecutor getBulkExecutor() { return collectionBulkExecutor; } }; inputStreamBulkExecutor = new InputStreamWrapperBulkExecutor(dbURI, restTemplate, objectMapper); }
public void deleteDatabase(String path) { Assert.notNull(path); restTemplate.delete(DbPath.fromString(path).getPath()); }
public <T> T get(String path, ResponseCallback<T> callback) { HttpResponse hr = client.get(path); return handleResponse(callback, hr); }
@Override public boolean checkIfDbExists(DbPath db) { return restTemplate.head(db.getPath(), new StdResponseHandler<Boolean>() { @Override public Boolean error(HttpResponse hr) { if(hr.getCode() == HttpStatus.NOT_FOUND) { // only 404 is a valid response, anything else is an error // see http://docs.couchdb.org/en/latest/api/database/common.html#head--db return false; } throw StdResponseHandler.createDbAccessException(hr); } @Override public Boolean success(HttpResponse hr) throws Exception { return true; } }); }
public void put(String path) { handleVoidResponse(client.put(path)); }
public HttpResponse postUncached(String path, String content ) { return handleRawResponse(client.postUncached(path,content)); }
@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; } }