private void handleVoidResponse(HttpResponse hr) { if (hr == null) return; try { if (!hr.isSuccessful()) { new StdResponseHandler<Void>().error(hr); } } finally { hr.releaseConnection(); } }
public T success(HttpResponse hr) throws Exception { return checkResponseBodyOkAndReturnDefaultValue(hr, null, MAPPER); }
public T error(HttpResponse hr) { throw StdResponseHandler.createDbAccessException(hr); }
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); } }
InputStream content = hr.getContent(); if (content != null) { responseBody = responseBodyAsNode(IOUtils.toString(content)); } else { responseBody = NullNode.getInstance(); String body; try { body = toPrettyString(responseBody); } catch (IOException e) { body = "unavailable";
@Override public List<Revision> error(HttpResponse hr) { if (hr.getCode() == HttpStatus.NOT_FOUND) { return Collections.emptyList(); } return super.error(hr); } });
protected static <T> T checkResponseBodyOkAndReturnDefaultValue(HttpResponse hr, T defaultValue, ObjectMapper mapper) throws IOException { InputStream content = hr.getContent(); try { content = hr.getContent(); JsonNode body = responseBodyAsNode(content, MAPPER); JsonNode okNode = body.get("ok"); if (okNode != null) { if (okNode.isBoolean()) { if (okNode.booleanValue()) { return defaultValue; } } } throw new DbAccessException("Unexpected response body content, expected {\"ok\":true}, got " + body.toString()); } finally { IOUtils.closeQuietly(content); } }
@Override public void ensureFullCommit() { restTemplate.post(dbURI.append("_ensure_full_commit").toString(), "", new StdResponseHandler<Void>()); }
@Override public T error(HttpResponse hr) { return hr.getCode() == HttpStatus.NOT_FOUND ? null : super.error(hr); } });
@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
@Override public Void error(HttpResponse hr) { if (hr.getCode() == HttpStatus.CONFLICT) { throw new UpdateConflictException(id, Documents .getRevision(o)); } return super.error(hr); } });
@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
@Override public Void error(HttpResponse hr) { if (hr.getCode() == HttpStatus.CONFLICT) { throw new UpdateConflictException(id, Documents .getRevision(o)); } return super.error(hr); } });
private HttpResponse handleRawResponse(HttpResponse hr) { try { if (!hr.isSuccessful()) { throw StdResponseHandler.createDbAccessException(hr); } return hr; } catch (Exception e) { hr.releaseConnection(); throw Exceptions.propagate(e); } } }