public class HttpFactoryBean implements FactoryBean<HttpClient>{ private String host; private int port; public HttpClient getObject() throws Exception { return new StdHttpClient.Builder() .host(host) .port(port) .build(); } public Class<? extends HttpClient> getObjectType() { return StdHttpClient.class; } public boolean isSingleton() { return true; } public void setHost(String host) { this.host = host; } public void setPort(int port) { this.port = port; }}
private void handleVoidResponse(HttpResponse hr) { if (hr == null) return; try { if (!hr.isSuccessful()) { new StdResponseHandler<Void>().error(hr); } } finally { hr.releaseConnection(); } }
public HttpClient build() { org.apache.http.client.HttpClient client = configureClient(); org.apache.http.client.HttpClient cachingHttpClient = client; if (caching) { cachingHttpClient = WithCachingBuilder.withCaching(client, maxCacheEntries, maxObjectSizeBytes); } return new StdHttpClient(cachingHttpClient, client); }
@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 AttachmentInputStream getAttachment(String attachmentId, URI uri) { HttpResponse r = restTemplate.get(uri.toString()); return new AttachmentInputStream(attachmentId, r.getContent(), r.getContentType(), r.getContentLength()); }
@Override public void ensureFullCommit() { restTemplate.post(dbURI.append("_ensure_full_commit").toString(), "", new StdResponseHandler<Void>()); }
public <T> T put(String path, InputStream data, String contentType, long contentLength, ResponseCallback<T> callback) { return handleResponse(callback, client.put(path, data, contentType, contentLength)); }
@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 HttpResponse copy(String sourceUri, String destination) { return executeRequest(new HttpCopyRequest(sourceUri, destination), true); }
public <T> T get(String path, ResponseCallback<T> callback) { HttpResponse hr = client.get(path); return handleResponse(callback, hr); }
public <T> T getUncached(String path, ResponseCallback<T> callback) { HttpResponse hr = client.getUncached(path); return handleResponse(callback, hr); }
public void put(String path, InputStream data, String contentType, long contentLength) { handleVoidResponse(client.put(path, data, contentType, contentLength)); }
private InputStream fetchChangesAsStream(ChangesCommand cmd) { HttpResponse r = restTemplate.get(dbURI.append(cmd.toString()) .toString()); return r.getContent(); }
public <T> T put(String path, String content, ResponseCallback<T> callback) { return handleResponse(callback, client.put(path, content)); }