public static URI of(String path) { return new URI(path); }
public URI param(String name, String value) { if (prototype) { return copy().param(name, value); } if (params != null) { params().append("&"); } else { params().append("?"); } try { params().append(name).append("=").append(URLEncoder.encode(value, "UTF-8")); } catch (UnsupportedEncodingException e) { throw Exceptions.propagate(e); } uri = null; return this; }
@Override public String toString() { if (queryString == null) { URI uri = URI.of("_changes"); if (continuous) { uri.param("feed", "continuous"); uri.param("since", since); uri.param("filter", filter); uri.param("include_docs", "true"); uri.param("heartbeat", heartbeat); uri.param("limit", limit); uri.params(extraQueryParams); queryString = uri.toString();
@Override public String path() { return dbURI.toString(); }
private URI buildViewPath() { assertHasText(dbPath, "dbPath"); assertHasText(viewName, "viewName"); URI uri = URI.of(dbPath); if (isNotEmpty(listName)) { uri.append(designDocId).append("_list").append(listName).append(viewName); } else if (ALL_DOCS_VIEW_NAME.equals(viewName)) { uri.append(viewName); } else { assertHasText(designDocId, "designDocId"); uri.append(designDocId).append("_view").append(viewName); } return uri; }
public URI append(String pathElement) { if (prototype) { return copy().append(pathElement); } if (path.charAt(path.length()-1) != '/') { path.append("/"); } try { if (!pathElement.startsWith("_")) { pathElement = URLEncoder.encode(pathElement, "UTF-8"); } path.append(pathElement); } catch (UnsupportedEncodingException e) { throw Exceptions.propagate(e); } uri = null; return this; }
@Override public AttachmentInputStream getAttachment(String id, String attachmentId, String revision) { assertDocIdHasValue(id); Assert.hasText(attachmentId, "attachmentId may not be null or empty"); Assert.hasText(revision, "revision may not be null or empty"); LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); return getAttachment(attachmentId, dbURI.append(id).append(attachmentId).param("rev", revision)); }
public URI param(String name, long value) { return param(name, Long.toString(value)); }
@Override public AttachmentInputStream getAttachment(final String id, final String attachmentId) { assertDocIdHasValue(id); Assert.hasText(attachmentId, "attachmentId may not be null or empty"); LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); return getAttachment(attachmentId, dbURI.append(id).append(attachmentId)); }
private void applyOptions(Options options, URI uri) { if (options != null && !options.isEmpty()) { uri.params(options.getOptions()); } }
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 String buildQuery() { if (cachedQuery != null) { return cachedQuery; } URI query = buildQueryURI(); cachedQuery = query.toString(); return cachedQuery; }
private URI buildUpdateHandlerRequestPath() { Assert.hasText(dbPath, "dbPath"); Assert.hasText(functionName, "functionName"); Assert.hasText(designDocId, "designDocId"); Assert.hasText(docId, "docId"); URI uri = URI.of(dbPath); uri.append(designDocId) .append("_update") .append(functionName) .append(docId); return uri; }
public URI param(String name, int value) { return param(name, Integer.toString(value)); }
@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); } }); }
@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); } } ); }