@Override public Status updateSecurity(Security security) { try { return restTemplate.put(securityPath(), objectMapper.writeValueAsString(security), new StdResponseHandler<Status>() { @Override public Status success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), Status.class); } } ); } catch(JsonProcessingException e) { throw new IllegalStateException("Failed to update security: " + e.getMessage()); } }
@Override public void setRevisionLimit(int limit) { restTemplate.put(dbURI.append("_revs_limit").toString(), Integer.toString(limit), VOID_RESPONSE_HANDLER); }
@Override public String setConfiguration(String section, String key, String value) { Assert.notNull(section, "Section may not be null"); Assert.notNull(key, "Key may not be null"); String url = "/_config/" + section + "/" + key; String content; try { content = objectMapper.writeValueAsString(value); } catch (JsonProcessingException e) { throw Exceptions.propagate(e); } return restTemplate.put(url, content, new StdResponseHandler<String>() { @Override public String success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), String.class); } }); }
public boolean createDatabaseIfNotExists(final DbPath db) { boolean databaseAlreadyExists = checkIfDbExists(db); if (databaseAlreadyExists) { return false; } LOG.debug("creating db path: {}", db.getPath()); return restTemplate.put(db.getPath(), new StdResponseHandler<Boolean>() { @Override public Boolean error(HttpResponse hr) { if (hr.getCode() == HttpStatus.PRECONDITION_FAILED) { // 412 indicates existing database // see http://docs.couchdb.org/en/latest/api/database/common.html#put--db LOG.debug("database at db path {} already exists.", db.getPath()); return false; } throw StdResponseHandler.createDbAccessException(hr); } @Override public Boolean success(HttpResponse hr) throws Exception { return checkResponseBodyOkAndReturnDefaultValue(hr, true, objectMapper); } }); }
@Override public void create(String id, Object node) { assertDocIdHasValue(id); Assert.notNull(node, "node may not be null"); HttpEntity entity = createHttpEntity(node); restTemplate.put(URIWithDocId(id), entity); }
@Override public void updateMultipart(String id, InputStream stream, String boundary, long length, Options options) { assertDocIdHasValue(id); Assert.hasText(boundary); URI uri = dbURI.append(id); applyOptions(options, uri); String contentType = String.format("multipart/related; boundary=\"%s\"", boundary); restTemplate.put(uri.toString(), stream, contentType, length); }
@Override public void create(String id, Object node) { assertDocIdHasValue(id); Assert.notNull(node, "node may not be null"); restTemplate.put(URIWithDocId(id), serializeToJson(node)); }
@Override public void update(String id, InputStream document, long length, Options options) { assertDocIdHasValue(id); URI uri = dbURI.append(id); applyOptions(options, uri); restTemplate.put(uri.toString(), document, "application/json", length); }
@Override public void update(final Object o) { Assert.notNull(o, "Document cannot be null"); final String id = Documents.getId(o); assertDocIdHasValue(id); restTemplate.put(dbURI.append(id).toString(), serializeToJson(o), new StdResponseHandler<Void>() { @Override public Void success(HttpResponse hr) throws Exception { JsonNode n = objectMapper.readValue(hr.getContent(), JsonNode.class); Documents.setRevision(o, n.get("rev").textValue()); return null; } @Override public Void error(HttpResponse hr) { if (hr.getCode() == HttpStatus.CONFLICT) { throw new UpdateConflictException(id, Documents .getRevision(o)); } return super.error(hr); } }); }
@Override public void update(final Object o) { Assert.notNull(o, "Document cannot be null"); final String id = Documents.getId(o); assertDocIdHasValue(id); HttpEntity entity = createHttpEntity(o); restTemplate.put(dbURI.append(id).toString(), entity, new StdResponseHandler<Void>() { @Override public Void success(HttpResponse hr) throws Exception { JsonNode n = objectMapper.readValue(hr.getContent(), JsonNode.class); Documents.setRevision(o, n.get("rev").textValue()); return null; } @Override public Void error(HttpResponse hr) { if (hr.getCode() == HttpStatus.CONFLICT) { throw new UpdateConflictException(id, Documents .getRevision(o)); } return super.error(hr); } }); } }
@Override public String createAttachment(String docId, String revision, AttachmentInputStream data) { assertDocIdHasValue(docId); URI uri = dbURI.append(docId).append(data.getId()); if (revision != null) { uri.param("rev", revision); } return restTemplate.put(uri.toString(), data, data.getContentType(), data.getContentLength(), revisionHandler).getRevision(); }
@Override public void create(final Object o) { Assert.notNull(o, "Document may not be null"); Assert.isTrue(Documents.isNew(o), "Object must be new"); HttpEntity entity = createHttpEntity(o); String id = Documents.getId(o); DocumentOperationResult result; if (id != null && id.length() != 0) { result = restTemplate.put(URIWithDocId(id), entity, revisionHandler); } else { result = restTemplate.post(dbURI.toString(), entity, revisionHandler); Documents.setId(o, result.getId()); } Documents.setRevision(o, result.getRevision()); }
@Override public void create(final Object o) { Assert.notNull(o, "Document may not be null"); Assert.isTrue(Documents.isNew(o), "Object must be new"); String json = serializeToJson(o); String id = Documents.getId(o); DocumentOperationResult result; if (id != null && id.length() != 0) { result = restTemplate.put(URIWithDocId(id), json, revisionHandler); } else { result = restTemplate.post(dbURI.toString(), json, revisionHandler); Documents.setId(o, result.getId()); } Documents.setRevision(o, result.getRevision()); }
@Override public String callUpdateHandler(final UpdateHandlerRequest req) { Assert.hasText(req.getDesignDocId(), "designDocID may not be null or empty"); Assert.hasText(req.getFunctionName(), "functionName may not be null or empty"); Assert.hasText(req.getDocId(), "docId may not be null or empty"); req.dbPath(dbURI.toString()); return restTemplate.put(req.buildRequestUri(), serializeUpdateHandlerRequestBody(req.getBody()), new StdResponseHandler<String>() { @Override public String success(HttpResponse hr) throws JsonProcessingException, IOException { return IOUtils.toString(hr.getContent(), "UTF-8"); } }); }
@Override public <T> T callUpdateHandler(final UpdateHandlerRequest req, final Class<T> c) { Assert.hasText(req.getDesignDocId(), "designDocID may not be null or empty"); Assert.hasText(req.getFunctionName(), "functionName may not be null or empty"); Assert.hasText(req.getDocId(), "docId may not be null or empty"); req.dbPath(dbURI.toString()); return restTemplate.put(req.buildRequestUri(), serializeUpdateHandlerRequestBody(req.getBody()), new StdResponseHandler<T>() { @Override public T success(HttpResponse hr) throws Exception { return objectMapper.readValue(hr.getContent(), c); } @Override public T error(HttpResponse hr) { if (hr.getCode() == HttpStatus.CONFLICT) { throw new UpdateConflictException(req.getDocId(), "<Update Handler>"); } return null; } }); }