All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.bnd.http.HttpRequest Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.bnd.http;

import java.io.File;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

import org.osgi.util.promise.Deferred;
import org.osgi.util.promise.Promise;

import aQute.bnd.osgi.Processor;
import aQute.bnd.service.url.TaggedData;
import aQute.lib.converter.TypeReference;
import aQute.service.reporter.Reporter;

/**
 * Builds up a request
 * 
 * @param 
 */
@SuppressWarnings("unchecked")
public class HttpRequest {
	String				verb		= "GET";
	Object				upload;
	Type				download;
	Map	headers		= new HashMap<>();
	long				timeout		= -1;
	HttpClient			client;
	String				ifNoneMatch;
	long				ifModifiedSince;
	long				ifUnmodifiedSince;
	URL					url;
	int					redirects	= 10;
	String				ifMatch;
	boolean				cached;
	long				maxStale;
	Reporter			reporter;
	File				useCacheFile;
	boolean				updateTag;

	HttpRequest(HttpClient client) {
		this.client = client;
	}

	/**
	 * Convert the result to a specific type
	 */
	public  HttpRequest get(Class type) {
		this.download = type;
		return (HttpRequest) this;
	}

	/**
	 * Convert the result to a specific type
	 */
	public  HttpRequest get(TypeReference type) {
		this.download = type.getType();
		return (HttpRequest) this;
	}

	/**
	 * Convert the result to a specific type
	 */
	public HttpRequest get(Type type) {
		this.download = type;
		return (HttpRequest) this;
	}

	/**
	 * Set the HTTP verb
	 */

	public HttpRequest verb(String verb) {
		this.verb = verb;
		return this;
	}

	/**
	 * Set the verb/method to put
	 */
	public HttpRequest put() {
		this.verb = "PUT";
		return this;
	}

	/**
	 * Set the verb/method to head
	 */
	public HttpRequest head() {
		this.verb = "HEAD";
		return this;
	}

	/**
	 * Set the verb/method to get
	 */
	public HttpRequest get() {
		this.verb = "GET";
		return this;
	}

	/**
	 * Set the verb/method to post
	 */
	public HttpRequest post() {
		this.verb = "POST";
		return this;
	}

	/**
	 * Set the verb/method to option
	 */
	public HttpRequest option() {
		this.verb = "OPTION";
		return this;
	}

	/**
	 * Set the verb/method to delete
	 */
	public HttpRequest delete() {
		this.verb = "DELETE";
		return this;
	}

	/**
	 * Set the object to upload. Can be of several types:
	 * 
    *
  • InputStream – copied verbatim *
  • String – content sent *
  • byte[] – content sent *
  • File – content sent *
  • Otherwise assumes DTO and encodes in JSON *
*/ public HttpRequest upload(Object upload) { this.upload = upload; return this; } /** * Add headers to request */ public HttpRequest headers(Map map) { headers.putAll(map); return this; } /** * Add header to request */ public HttpRequest headers(String key, String value) { headers.put(key, value); return this; } /** * Set timeout in ms */ public HttpRequest timeout(long timeoutInMs) { this.timeout = timeoutInMs; return this; } public HttpRequest ifNoneMatch(String etag) { this.ifNoneMatch = etag; return this; } public HttpRequest ifModifiedSince(long epochTime) { this.ifModifiedSince = epochTime; return this; } public HttpRequest maxRedirects(int n) { this.redirects = n; return this; } public T go(URL url) throws Exception { this.url = url; return (T) client.send(this); } public T go(URI url) throws Exception { this.url = url.toURL(); return (T) client.send(this); } public HttpRequest age(int n, TimeUnit tu) { this.headers.put("Age", "" + tu.toSeconds(n)); return this; } public Promise async(final URL url) throws InterruptedException { this.url = url; final Deferred deferred = new Deferred<>(); Executor e = Processor.getExecutor(); e.execute(new Runnable() { @Override public void run() { try { T result = (T) client.send(HttpRequest.this); deferred.resolve(result); } catch (Throwable t) { deferred.fail(t); } } }); return deferred.getPromise(); } public Promise async(URI uri) throws MalformedURLException, InterruptedException { return async(uri.toURL()); } @Override public String toString() { return "HttpRequest [verb=" + verb + ", upload=" + upload + ", download=" + download + ", headers=" + headers + ", timeout=" + timeout + ", client=" + client + ", url=" + url + "]"; } public HttpRequest ifUnmodifiedSince(long ifNotModifiedSince) { this.ifUnmodifiedSince = ifNotModifiedSince; return this; } public HttpRequest ifMatch(String etag) { this.ifMatch = etag; return this; } public HttpRequest asTag() { return get(TaggedData.class); } public HttpRequest asString() { return get(String.class); } public boolean isCache() { return ("GET".equalsIgnoreCase(verb) && cached) || download == File.class; } public HttpRequest useCache(long maxStale) { this.maxStale = maxStale; this.cached = true; download = File.class; return (HttpRequest) this; } public HttpRequest useCache() { return useCache(-1); } public HttpRequest useCache(File file) { this.useCacheFile = file; return useCache(-1); } public HttpRequest useCache(File file, long maxStale) { this.useCacheFile = file; return useCache(maxStale); } public HttpRequest report(Reporter reporter) { this.reporter = reporter; return this; } // TODO public HttpRequest timeout(long timeout, TimeUnit unit) { this.timeout = timeout; return this; } public boolean isTagResult() { return download == null || download == TaggedData.class; } public HttpRequest updateTag() { updateTag = true; return this; } }