mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-03-08 09:49:44 +00:00
Rewrite of the fluent API
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1189320 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
732095943f
commit
66e7b84915
@ -26,6 +26,46 @@
|
||||
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
public enum FluentHttpMethod {
|
||||
GET_METHOD, POST_METHOD, DELETE_METHOD, OPTIONS_METHOD, PUT_METHOD, HEAD_METHOD, TRACE_METHOD;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
public class Content {
|
||||
|
||||
private final byte[] raw;
|
||||
private final ContentType type;
|
||||
|
||||
Content(final byte[] raw, final ContentType type) {
|
||||
super();
|
||||
this.raw = raw;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ContentType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public byte[] asBytes() {
|
||||
return this.raw.clone();
|
||||
}
|
||||
|
||||
public String asString() {
|
||||
String charset = this.type.getCharset();
|
||||
if (charset == null) {
|
||||
charset = HTTP.DEFAULT_CONTENT_TYPE;
|
||||
}
|
||||
try {
|
||||
return new String(this.raw, charset);
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
return new String(this.raw);
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream asStream() {
|
||||
return new ByteArrayInputStream(this.raw);
|
||||
}
|
||||
|
||||
}
|
@ -27,142 +27,150 @@
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.Credentials;
|
||||
import org.apache.http.auth.NTCredentials;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.scheme.SchemeSocketFactory;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
||||
import org.apache.http.impl.conn.SchemeRegistryFactory;
|
||||
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
|
||||
public class FluentExecutor {
|
||||
|
||||
final static PoolingClientConnectionManager CONNMGR = new PoolingClientConnectionManager(
|
||||
SchemeRegistryFactory.createSystemDefault());
|
||||
final static DefaultHttpClient CLIENT = new DefaultHttpClient(CONNMGR);
|
||||
|
||||
public static FluentExecutor newInstance() {
|
||||
FluentExecutor fexe = new FluentExecutor();
|
||||
return fexe;
|
||||
return new FluentExecutor(CLIENT);
|
||||
}
|
||||
|
||||
private ThreadSafeClientConnManager localConnManager;
|
||||
private SchemeRegistry localSchemeRegistry;
|
||||
private SchemeSocketFactory localSocketFactory;
|
||||
private final HttpClient httpclient;
|
||||
private final BasicHttpContext localContext;
|
||||
private final AuthCache authCache;
|
||||
|
||||
private FluentExecutor() {
|
||||
localSchemeRegistry = SchemeRegistryFactory.createDefault();
|
||||
localConnManager = new ThreadSafeClientConnManager(localSchemeRegistry);
|
||||
localSocketFactory = PlainSocketFactory.getSocketFactory();
|
||||
private CredentialsProvider credentialsProvider;
|
||||
private CookieStore cookieStore;
|
||||
|
||||
FluentExecutor(final HttpClient httpclient) {
|
||||
super();
|
||||
this.httpclient = httpclient;
|
||||
this.localContext = new BasicHttpContext();
|
||||
this.authCache = new BasicAuthCache();
|
||||
}
|
||||
|
||||
public FluentResponse exec(FluentRequest req)
|
||||
throws ClientProtocolException, IOException {
|
||||
DefaultHttpClient client = getClient();
|
||||
client.setCredentialsProvider(req.getCredentialsProvider());
|
||||
client.setParams(req.getLocalParams());
|
||||
HttpResponse resp = client.execute(req, req.getLocalContext());
|
||||
FluentResponse fresp = new FluentResponse(resp);
|
||||
return fresp;
|
||||
}
|
||||
|
||||
public FluentResponse[] exec(final FluentRequest[] reqs)
|
||||
throws InterruptedException {
|
||||
if (reqs == null)
|
||||
throw new NullPointerException("The request array may not be null.");
|
||||
int length = reqs.length;
|
||||
if (length == 0)
|
||||
return new FluentResponse[0];
|
||||
FluentResponse[] resps = new FluentResponse[length];
|
||||
MultiRequestThread[] threads = new MultiRequestThread[length];
|
||||
for (int id = 0; id < length; id++) {
|
||||
threads[id] = new MultiRequestThread(this, reqs, resps, id);
|
||||
public FluentExecutor auth(final AuthScope authScope, final Credentials creds) {
|
||||
if (this.credentialsProvider == null) {
|
||||
this.credentialsProvider = new BasicCredentialsProvider();
|
||||
}
|
||||
for (int id = 0; id < length; id++) {
|
||||
threads[id].start();
|
||||
this.credentialsProvider.setCredentials(authScope, creds);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final HttpHost host, final Credentials creds) {
|
||||
AuthScope authScope = host != null ? new AuthScope(host) : AuthScope.ANY;
|
||||
return auth(authScope, creds);
|
||||
}
|
||||
|
||||
public FluentExecutor authPreemptive(final HttpHost host, final Credentials creds) {
|
||||
auth(host, creds);
|
||||
this.authCache.put(host, new BasicScheme());
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final Credentials cred) {
|
||||
return auth(AuthScope.ANY, cred);
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final String username, final String password) {
|
||||
return auth(new UsernamePasswordCredentials(username, password));
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final String username, final String password,
|
||||
final String workstation, final String domain) {
|
||||
return auth(new NTCredentials(username, password, workstation, domain));
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final HttpHost host,
|
||||
final String username, final String password) {
|
||||
return auth(host, new UsernamePasswordCredentials(username, password));
|
||||
}
|
||||
|
||||
public FluentExecutor auth(final HttpHost host,
|
||||
final String username, final String password,
|
||||
final String workstation, final String domain) {
|
||||
return auth(host, new NTCredentials(username, password, workstation, domain));
|
||||
}
|
||||
|
||||
public FluentExecutor authPreemptive(final HttpHost host,
|
||||
final String username, final String password) {
|
||||
auth(host, username, password);
|
||||
this.authCache.put(host, new BasicScheme());
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor clearAuth() {
|
||||
if (this.credentialsProvider != null) {
|
||||
this.credentialsProvider.clear();
|
||||
}
|
||||
for (int id = 0; id < length; id++) {
|
||||
threads[id].join();
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor cookieStore(final CookieStore cookieStore) {
|
||||
this.cookieStore = cookieStore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor clearCookies() {
|
||||
if (this.cookieStore != null) {
|
||||
this.cookieStore.clear();
|
||||
}
|
||||
return resps;
|
||||
}
|
||||
|
||||
public DefaultHttpClient getClient() {
|
||||
DefaultHttpClient client;
|
||||
client = new DefaultHttpClient(localConnManager);
|
||||
return client;
|
||||
}
|
||||
|
||||
public int getMaxConnectionsPerRoute() {
|
||||
return localConnManager.getDefaultMaxPerRoute();
|
||||
}
|
||||
|
||||
public int getMaxTotalConnections() {
|
||||
return localConnManager.getMaxTotal();
|
||||
}
|
||||
|
||||
public Scheme getScheme(final String name) {
|
||||
return localSchemeRegistry.getScheme(name);
|
||||
}
|
||||
|
||||
public List<String> getSchemeNames() {
|
||||
List<String> schemeNames = localSchemeRegistry.getSchemeNames();
|
||||
return schemeNames;
|
||||
}
|
||||
|
||||
public FluentExecutor registerScheme(final String name, final int port) {
|
||||
Scheme sch = new Scheme(name, port, localSocketFactory);
|
||||
localSchemeRegistry.register(sch);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentExecutor setMaxConnectionsPerRoute(final int maxPerRoute) {
|
||||
localConnManager.setDefaultMaxPerRoute(maxPerRoute);
|
||||
return this;
|
||||
public FluentResponse exec(
|
||||
final FluentRequest req) throws ClientProtocolException, IOException {
|
||||
this.localContext.setAttribute(ClientContext.CREDS_PROVIDER, this.credentialsProvider);
|
||||
this.localContext.setAttribute(ClientContext.AUTH_CACHE, this.authCache);
|
||||
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
|
||||
HttpRequestBase httprequest = req.getHttpRequest();
|
||||
httprequest.reset();
|
||||
return new FluentResponse(this.httpclient.execute(httprequest, this.localContext));
|
||||
}
|
||||
|
||||
public FluentExecutor setMaxTotalConnections(final int maxTotal) {
|
||||
localConnManager.setMaxTotal(maxTotal);
|
||||
return this;
|
||||
public static void setMaxTotal(int max) {
|
||||
CONNMGR.setMaxTotal(max);
|
||||
}
|
||||
|
||||
public FluentExecutor unregisterAllSchemes() {
|
||||
for (String name : getSchemeNames())
|
||||
localSchemeRegistry.unregister(name);
|
||||
return this;
|
||||
public static void setDefaultMaxPerRoute(int max) {
|
||||
CONNMGR.setDefaultMaxPerRoute(max);
|
||||
}
|
||||
|
||||
public FluentExecutor unregisterScheme(final String name) {
|
||||
localSchemeRegistry.unregister(name);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
class MultiRequestThread extends Thread {
|
||||
private FluentExecutor executor;
|
||||
private FluentRequest[] reqs;
|
||||
private FluentResponse[] resps;
|
||||
private int id;
|
||||
|
||||
MultiRequestThread(final FluentExecutor executor,
|
||||
final FluentRequest[] reqs, final FluentResponse[] resps,
|
||||
final int id) {
|
||||
this.executor = executor;
|
||||
this.reqs = reqs;
|
||||
this.resps = resps;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
FluentRequest req = reqs[id];
|
||||
try {
|
||||
FluentResponse resp = executor.exec(req);
|
||||
resp.loadContent();
|
||||
resps[id] = resp;
|
||||
} catch (Exception e) {
|
||||
req.abort();
|
||||
}
|
||||
public static void setMaxPerRoute(final HttpRoute route, int max) {
|
||||
CONNMGR.setMaxPerRoute(route, max);
|
||||
}
|
||||
|
||||
public static void registerScheme(final Scheme scheme) {
|
||||
CONNMGR.getSchemeRegistry().register(scheme);
|
||||
}
|
||||
|
||||
public static void unregisterScheme(final String name) {
|
||||
CONNMGR.getSchemeRegistry().unregister(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,524 +27,277 @@
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HeaderIterator;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.RequestLine;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.Credentials;
|
||||
import org.apache.http.auth.NTCredentials;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.fluent.header.DateUtils;
|
||||
import org.apache.http.client.fluent.header.HttpHeader;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpOptions;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.conn.params.ConnRoutePNames;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.params.CoreConnectionPNames;
|
||||
import org.apache.http.params.CoreProtocolPNames;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
public class FluentRequest implements HttpUriRequest {
|
||||
private HttpParams localParams;
|
||||
private HttpContext localContext;
|
||||
private CredentialsProvider credentialsProvider;
|
||||
private HttpUriRequest request;
|
||||
private FluentHttpMethod method;
|
||||
private HttpHost localProxy;
|
||||
public class FluentRequest {
|
||||
|
||||
protected static final Log log = LogFactory.getLog(FluentRequest.class);
|
||||
public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||
public static final Locale DATE_LOCALE = Locale.US;
|
||||
public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT");
|
||||
|
||||
public FluentRequest(final HttpUriRequest req) {
|
||||
URI uri = req.getURI();
|
||||
String methodName = req.getMethod().toUpperCase();
|
||||
FluentHttpMethod method = FluentHttpMethod.GET_METHOD;
|
||||
if (methodName.equals("GET"))
|
||||
method = FluentHttpMethod.GET_METHOD;
|
||||
else if (methodName.equals("POST"))
|
||||
method = FluentHttpMethod.POST_METHOD;
|
||||
else if (methodName.equals("OPTIONS"))
|
||||
method = FluentHttpMethod.OPTIONS_METHOD;
|
||||
else if (methodName.equals("DELETE"))
|
||||
method = FluentHttpMethod.DELETE_METHOD;
|
||||
else if (methodName.equals("HEAD"))
|
||||
method = FluentHttpMethod.HEAD_METHOD;
|
||||
else if (methodName.equals("PUT"))
|
||||
method = FluentHttpMethod.PUT_METHOD;
|
||||
else if (methodName.equals("TRACE"))
|
||||
method = FluentHttpMethod.TRACE_METHOD;
|
||||
init(uri, method);
|
||||
private final HttpRequestBase request;
|
||||
private final HttpParams localParams;
|
||||
|
||||
private SimpleDateFormat dateFormatter;
|
||||
|
||||
public static FluentRequest Get(final URI uri) {
|
||||
return new FluentRequest(new HttpGet(uri));
|
||||
}
|
||||
|
||||
public FluentRequest(final String uri) {
|
||||
init(uri, FluentHttpMethod.GET_METHOD);
|
||||
public static FluentRequest Get(final String uri) {
|
||||
return new FluentRequest(new HttpGet(uri));
|
||||
}
|
||||
|
||||
public FluentRequest(final String uri, final FluentHttpMethod method) {
|
||||
init(uri, method);
|
||||
public static FluentRequest Head(final URI uri) {
|
||||
return new FluentRequest(new HttpHead(uri));
|
||||
}
|
||||
|
||||
public FluentRequest(final URI uri) {
|
||||
init(uri, FluentHttpMethod.GET_METHOD);
|
||||
public static FluentRequest Head(final String uri) {
|
||||
return new FluentRequest(new HttpHead(uri));
|
||||
}
|
||||
|
||||
public FluentRequest(final URI uri, final FluentHttpMethod method) {
|
||||
init(uri, method);
|
||||
public static FluentRequest Post(final URI uri) {
|
||||
return new FluentRequest(new HttpPost(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Post(final String uri) {
|
||||
return new FluentRequest(new HttpPost(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Put(final URI uri) {
|
||||
return new FluentRequest(new HttpPut(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Put(final String uri) {
|
||||
return new FluentRequest(new HttpPut(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Trace(final URI uri) {
|
||||
return new FluentRequest(new HttpTrace(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Trace(final String uri) {
|
||||
return new FluentRequest(new HttpTrace(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Delete(final URI uri) {
|
||||
return new FluentRequest(new HttpDelete(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Delete(final String uri) {
|
||||
return new FluentRequest(new HttpDelete(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Options(final URI uri) {
|
||||
return new FluentRequest(new HttpOptions(uri));
|
||||
}
|
||||
|
||||
public static FluentRequest Options(final String uri) {
|
||||
return new FluentRequest(new HttpOptions(uri));
|
||||
}
|
||||
|
||||
FluentRequest(final HttpRequestBase request) {
|
||||
super();
|
||||
this.request = request;
|
||||
this.localParams = request.getParams();
|
||||
}
|
||||
|
||||
HttpRequestBase getHttpRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
public FluentResponse exec() throws ClientProtocolException, IOException {
|
||||
return new FluentResponse(FluentExecutor.CLIENT.execute(this.request));
|
||||
}
|
||||
|
||||
public void abort() throws UnsupportedOperationException {
|
||||
this.request.abort();
|
||||
}
|
||||
|
||||
public void addHeader(final Header header) {
|
||||
//// HTTP header operations
|
||||
|
||||
public FluentRequest addHeader(final Header header) {
|
||||
this.request.addHeader(header);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addHeader(final String name, final String value) {
|
||||
public FluentRequest addHeader(final String name, final String value) {
|
||||
this.request.addHeader(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean containsHeader(final String name) {
|
||||
return this.request.containsHeader(name);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return a <code>FluentResponse</code> instance referring to the response
|
||||
* of this request
|
||||
* @throws ClientProtocolException
|
||||
* @throws IOException
|
||||
*/
|
||||
public FluentResponse exec() throws ClientProtocolException, IOException {
|
||||
DefaultHttpClient client = new DefaultHttpClient();
|
||||
return new FluentResponse(client.execute(request));
|
||||
}
|
||||
|
||||
public Header[] getAllHeaders() {
|
||||
return this.request.getAllHeaders();
|
||||
}
|
||||
|
||||
public String getCacheControl() {
|
||||
return getValueOfHeader(HttpHeader.CACHE_CONTROL);
|
||||
}
|
||||
|
||||
public int getConnectionTimeout() {
|
||||
return HttpConnectionParams.getConnectionTimeout(localParams);
|
||||
}
|
||||
|
||||
public String getContentCharset() {
|
||||
return (String) localParams
|
||||
.getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET);
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
String value = getValueOfHeader(HttpHeader.CONTENT_LENGTH);
|
||||
if (value == null)
|
||||
return -1;
|
||||
else {
|
||||
long contentLength = Long.parseLong(value);
|
||||
return contentLength;
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return getValueOfHeader(HttpHeader.CONTENT_TYPE);
|
||||
}
|
||||
|
||||
public CredentialsProvider getCredentialsProvider() {
|
||||
return credentialsProvider;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return getValueOfHeader(HttpHeader.DATE);
|
||||
}
|
||||
|
||||
public String getElementCharset() {
|
||||
return (String) localParams
|
||||
.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
|
||||
}
|
||||
|
||||
public Header getFirstHeader(final String name) {
|
||||
return this.request.getFirstHeader(name);
|
||||
}
|
||||
|
||||
public Header[] getHeaders(final String name) {
|
||||
return this.request.getHeaders(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTTP method as a field of <code><a
|
||||
* href="FunHttpMethod.html">FunHttpMethod</a></code> enumeration, such as
|
||||
* <code>GET</code>, <code>PUT</code>, <code>POST</code>, or other.
|
||||
*
|
||||
* @return a field of <a href="FunHttpMethod.html">FunHttpMethod</a>
|
||||
* enumeration indicates the name of HTTP method
|
||||
*/
|
||||
public FluentHttpMethod getHttpMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public HttpVersion getHttpVersion() {
|
||||
return (HttpVersion) this.localParams
|
||||
.getParameter(CoreProtocolPNames.PROTOCOL_VERSION);
|
||||
}
|
||||
|
||||
public String getIfModifiedSince() {
|
||||
return getValueOfHeader(HttpHeader.IF_MODIFIED_SINCE);
|
||||
}
|
||||
|
||||
public String getIfUnmodifiedSince() {
|
||||
return getValueOfHeader(HttpHeader.IF_UNMODIFIED_SINCE);
|
||||
}
|
||||
|
||||
public Header getLastHeader(final String name) {
|
||||
return this.request.getLastHeader(name);
|
||||
}
|
||||
|
||||
public HttpContext getLocalContext() {
|
||||
return localContext;
|
||||
}
|
||||
|
||||
public HttpParams getLocalParams() {
|
||||
return localParams;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return this.request.getMethod();
|
||||
}
|
||||
|
||||
public HttpParams getParams() {
|
||||
return this.request.getParams();
|
||||
}
|
||||
|
||||
public ProtocolVersion getProtocolVersion() {
|
||||
return this.request.getProtocolVersion();
|
||||
}
|
||||
|
||||
public RequestLine getRequestLine() {
|
||||
return this.request.getRequestLine();
|
||||
}
|
||||
|
||||
public int getSocketTimeout() {
|
||||
return HttpConnectionParams.getSoTimeout(localParams);
|
||||
}
|
||||
|
||||
public URI getURI() {
|
||||
return this.request.getURI();
|
||||
}
|
||||
|
||||
public String getUserAgent() {
|
||||
return (String) localParams.getParameter(CoreProtocolPNames.USER_AGENT);
|
||||
}
|
||||
|
||||
private String getValueOfHeader(final String headerName) {
|
||||
Header header = request.getFirstHeader(headerName);
|
||||
if (header != null)
|
||||
return header.getValue();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getWaitForContinue() {
|
||||
return (Integer) localParams
|
||||
.getParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE);
|
||||
}
|
||||
|
||||
public HeaderIterator headerIterator() {
|
||||
return this.request.headerIterator();
|
||||
}
|
||||
|
||||
public HeaderIterator headerIterator(final String name) {
|
||||
return this.request.headerIterator(name);
|
||||
}
|
||||
|
||||
private void init(final String uriString, final FluentHttpMethod method) {
|
||||
try {
|
||||
URI uri = new URI(uriString);
|
||||
init(uri, method);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void init(final URI uri, final FluentHttpMethod method) {
|
||||
switch (method) {
|
||||
case GET_METHOD:
|
||||
this.request = new HttpGet(uri);
|
||||
break;
|
||||
case POST_METHOD:
|
||||
this.request = new HttpPost(uri);
|
||||
break;
|
||||
case OPTIONS_METHOD:
|
||||
this.request = new HttpOptions(uri);
|
||||
break;
|
||||
case DELETE_METHOD:
|
||||
this.request = new HttpDelete(uri);
|
||||
break;
|
||||
case HEAD_METHOD:
|
||||
this.request = new HttpHead(uri);
|
||||
break;
|
||||
case PUT_METHOD:
|
||||
this.request = new HttpPut(uri);
|
||||
break;
|
||||
case TRACE_METHOD:
|
||||
this.request = new HttpTrace(uri);
|
||||
break;
|
||||
}
|
||||
this.method = method;
|
||||
localParams = request.getParams();
|
||||
localContext = new BasicHttpContext();
|
||||
credentialsProvider = new BasicCredentialsProvider();
|
||||
localProxy = null;
|
||||
}
|
||||
|
||||
public boolean isAborted() {
|
||||
return this.request.isAborted();
|
||||
}
|
||||
|
||||
public boolean isStrictTransferEncoding() {
|
||||
return (Boolean) localParams
|
||||
.getParameter(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
|
||||
}
|
||||
|
||||
public boolean isUseExpectContinue() {
|
||||
return (Boolean) localParams
|
||||
.getParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE);
|
||||
}
|
||||
|
||||
public FluentRequest removeAuth() {
|
||||
return setAuth(null);
|
||||
}
|
||||
|
||||
public void removeHeader(final Header header) {
|
||||
public FluentRequest removeHeader(final Header header) {
|
||||
this.request.removeHeader(header);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeHeaders(final String name) {
|
||||
public FluentRequest removeHeaders(final String name) {
|
||||
this.request.removeHeaders(name);
|
||||
}
|
||||
|
||||
public FluentRequest removeProxy() {
|
||||
setProxyAuth(null);
|
||||
localParams.removeParameter(ConnRoutePNames.DEFAULT_PROXY);
|
||||
localProxy = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setAuth(final Credentials cred) {
|
||||
String hostAddr = request.getURI().getHost();
|
||||
credentialsProvider.setCredentials(new AuthScope(hostAddr,
|
||||
AuthScope.ANY_PORT), cred);
|
||||
AuthCache authCache = new BasicAuthCache();
|
||||
HttpHost authHost = new HttpHost(hostAddr);
|
||||
authCache.put(authHost, new BasicScheme());
|
||||
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||
public FluentRequest setHeaders(final Header[] headers) {
|
||||
this.request.setHeaders(headers);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setAuth(final String username, final String password) {
|
||||
return setAuth(new UsernamePasswordCredentials(username, password));
|
||||
}
|
||||
|
||||
public FluentRequest setAuth(final String username, final String password,
|
||||
final String workstation, final String domain) {
|
||||
return setAuth(new NTCredentials(username, password, workstation,
|
||||
domain));
|
||||
}
|
||||
|
||||
public FluentRequest setCacheControl(String cacheControl) {
|
||||
request.setHeader(HttpHeader.CACHE_CONTROL, cacheControl);
|
||||
this.request.setHeader(HttpHeader.CACHE_CONTROL, cacheControl);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setConnectionTimeout(final int connectionTimeoutMillis) {
|
||||
HttpConnectionParams.setConnectionTimeout(localParams,
|
||||
connectionTimeoutMillis);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setContentCharset(final String charset) {
|
||||
localParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,
|
||||
charset);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setContentLength(final long contentLength) {
|
||||
request.setHeader(HttpHeader.CONTENT_LENGTH,
|
||||
String.valueOf(contentLength));
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setContentType(final String contentType) {
|
||||
request.setHeader(HttpHeader.CONTENT_TYPE, contentType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setCredentialProvider(
|
||||
final CredentialsProvider credProvider) {
|
||||
credentialsProvider = credProvider;
|
||||
return this;
|
||||
private SimpleDateFormat getDateFormat() {
|
||||
if (this.dateFormatter == null) {
|
||||
this.dateFormatter = new SimpleDateFormat(DATE_FORMAT, DATE_LOCALE);
|
||||
this.dateFormatter.setTimeZone(TIME_ZONE);
|
||||
}
|
||||
return this.dateFormatter;
|
||||
}
|
||||
|
||||
public FluentRequest setDate(final Date date) {
|
||||
String formattedDate = DateUtils.format(date);
|
||||
request.setHeader(HttpHeader.DATE, formattedDate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setElementCharset(final String charset) {
|
||||
localParams.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,
|
||||
charset);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setEntity(final HttpEntity entity) {
|
||||
if (method == FluentHttpMethod.POST_METHOD) {
|
||||
HttpPost post = (HttpPost) this.request;
|
||||
post.setEntity(entity);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Only POST method can have an entity.");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setHeader(final Header header) {
|
||||
this.request.setHeader(header);
|
||||
}
|
||||
|
||||
public void setHeader(final String name, final String value) {
|
||||
this.request.setHeader(name, value);
|
||||
}
|
||||
|
||||
public void setHeaders(final Header[] headers) {
|
||||
this.request.setHeaders(headers);
|
||||
}
|
||||
|
||||
public FluentRequest setHTMLFormEntity(final Map<String, String> form,
|
||||
final String encoding) throws UnsupportedEncodingException {
|
||||
List<NameValuePair> formparams = new ArrayList<NameValuePair>(
|
||||
form.size());
|
||||
for (String name : form.keySet()) {
|
||||
formparams.add(new BasicNameValuePair(name, form.get("name")));
|
||||
}
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
|
||||
encoding);
|
||||
return setEntity(entity);
|
||||
}
|
||||
|
||||
public FluentRequest setHttpVersion(HttpVersion version) {
|
||||
localParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
|
||||
this.request.setHeader(HttpHeader.DATE, getDateFormat().format(date));
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setIfModifiedSince(final Date date) {
|
||||
String formattedDate = DateUtils.format(date);
|
||||
request.setHeader(HttpHeader.IF_MODIFIED_SINCE, formattedDate);
|
||||
this.request.setHeader(HttpHeader.IF_MODIFIED_SINCE, getDateFormat().format(date));
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setIfUnmodifiedSince(final Date date) {
|
||||
String formattedDate = DateUtils.format(date);
|
||||
request.setHeader(HttpHeader.IF_UNMODIFIED_SINCE, formattedDate);
|
||||
this.request.setHeader(HttpHeader.IF_UNMODIFIED_SINCE, getDateFormat().format(date));
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setParams(final HttpParams params) {
|
||||
this.request.setParams(params);
|
||||
//// HTTP config parameter operations
|
||||
|
||||
public FluentRequest config(final String param, final Object object) {
|
||||
this.localParams.setParameter(param, object);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setProxy(final String proxyAddr, final int proxyPort) {
|
||||
return setProxy(proxyAddr, proxyPort, null, null);
|
||||
public FluentRequest removeConfig(final String param) {
|
||||
this.localParams.removeParameter(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setProxy(final String proxyAddr, final int proxyPort,
|
||||
final String username, final String password) {
|
||||
localProxy = new HttpHost(proxyAddr, proxyPort);
|
||||
localParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, localProxy);
|
||||
if (username != null) {
|
||||
setProxyAuth(username, password);
|
||||
//// HTTP protocol parameter operations
|
||||
|
||||
public FluentRequest version(final HttpVersion version) {
|
||||
return config(CoreProtocolPNames.PROTOCOL_VERSION, version);
|
||||
}
|
||||
|
||||
public FluentRequest elementCharset(final String charset) {
|
||||
return config(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
|
||||
}
|
||||
|
||||
public FluentRequest useExpectContinue() {
|
||||
return config(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
|
||||
}
|
||||
|
||||
public FluentRequest userAgent(final String agent) {
|
||||
return config(CoreProtocolPNames.USER_AGENT, agent);
|
||||
}
|
||||
|
||||
//// HTTP connection parameter operations
|
||||
|
||||
public FluentRequest socketTimeout(int timeout) {
|
||||
return config(CoreConnectionPNames.SO_TIMEOUT, timeout);
|
||||
}
|
||||
|
||||
public FluentRequest connectTimeout(int timeout) {
|
||||
return config(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
|
||||
}
|
||||
|
||||
public FluentRequest staleConnectionCheck(boolean b) {
|
||||
return config(CoreConnectionPNames.STALE_CONNECTION_CHECK, b);
|
||||
}
|
||||
|
||||
//// HTTP connection route operations
|
||||
|
||||
public FluentRequest proxy(final HttpHost proxy) {
|
||||
return config(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
||||
}
|
||||
|
||||
public FluentRequest noProxy() {
|
||||
return removeConfig(ConnRoutePNames.DEFAULT_PROXY);
|
||||
}
|
||||
|
||||
//// HTTP entity operations
|
||||
|
||||
public FluentRequest body(final HttpEntity entity) {
|
||||
if (this.request instanceof HttpEntityEnclosingRequest) {
|
||||
((HttpEntityEnclosingRequest) this.request).setEntity(entity);
|
||||
} else {
|
||||
throw new IllegalStateException(this.request.getMethod()
|
||||
+ " request cannot enclose an entity");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setProxyAuth(final Credentials proxyAuth) {
|
||||
if (localProxy == null)
|
||||
throw new IllegalStateException("HTTP proxy is not used.");
|
||||
credentialsProvider.setCredentials(
|
||||
new AuthScope(localProxy.getHostName(), localProxy.getPort()),
|
||||
proxyAuth);
|
||||
return this;
|
||||
public FluentRequest htmlFormBody(final NameValuePair[] formParams, final String charset) {
|
||||
try {
|
||||
return body(new UrlEncodedFormEntity(Arrays.asList(formParams)));
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new IllegalArgumentException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public FluentRequest setProxyAuth(final String username,
|
||||
final String password) {
|
||||
return setProxyAuth(new UsernamePasswordCredentials(username, password));
|
||||
public FluentRequest htmlFormBody(final NameValuePair... formParams) {
|
||||
return htmlFormBody(formParams, HTTP.DEFAULT_CONTENT_CHARSET);
|
||||
}
|
||||
|
||||
public FluentRequest setProxyAuth(final String username,
|
||||
final String password, final String workstation, final String domain) {
|
||||
return setProxyAuth(new NTCredentials(username, password, workstation,
|
||||
domain));
|
||||
public FluentRequest stringBody(final String s, final ContentType contentType) {
|
||||
return body(StringEntity.create(s, contentType));
|
||||
}
|
||||
|
||||
public FluentRequest setSocketTimeout(int socketTimeoutMillis) {
|
||||
HttpConnectionParams.setSoTimeout(localParams, socketTimeoutMillis);
|
||||
return this;
|
||||
public FluentRequest byteArrayBody(final byte[] b) {
|
||||
return body(new ByteArrayEntity(b));
|
||||
}
|
||||
|
||||
public FluentRequest setStrictTransferEncoding(final boolean bool) {
|
||||
localParams.setBooleanParameter(
|
||||
CoreProtocolPNames.STRICT_TRANSFER_ENCODING, bool);
|
||||
return this;
|
||||
public FluentRequest byteArrayBody(final byte[] b, int off, int len) {
|
||||
return body(new ByteArrayEntity(b, off, len));
|
||||
}
|
||||
|
||||
public FluentRequest setUseExpectContinue(Boolean bool) {
|
||||
localParams.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
|
||||
bool);
|
||||
return this;
|
||||
public FluentRequest streamBody(final InputStream instream) {
|
||||
return body(new InputStreamEntity(instream, -1));
|
||||
}
|
||||
|
||||
public FluentRequest setUserAgent(final String agent) {
|
||||
localParams.setParameter(CoreProtocolPNames.USER_AGENT, agent);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FluentRequest setWaitForContinue(final int waitMillis) {
|
||||
localParams.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE,
|
||||
waitMillis);
|
||||
return this;
|
||||
public FluentRequest streamBody(final InputStream instream, final ContentType contentType) {
|
||||
return body(new InputStreamEntity(instream, -1, contentType));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,238 +26,105 @@
|
||||
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HeaderIterator;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.fluent.header.HttpHeader;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
public class FluentResponse implements HttpResponse {
|
||||
protected static final Log log = LogFactory.getLog(FluentResponse.class);
|
||||
private HttpResponse response;
|
||||
private byte[] content;
|
||||
private String contentString;
|
||||
public class FluentResponse {
|
||||
|
||||
private final HttpResponse response;
|
||||
private boolean consumed;
|
||||
|
||||
FluentResponse(HttpResponse response) {
|
||||
FluentResponse(final HttpResponse response) {
|
||||
super();
|
||||
this.response = response;
|
||||
consumed = false;
|
||||
}
|
||||
|
||||
public void addHeader(Header header) {
|
||||
this.response.addHeader(header);
|
||||
}
|
||||
|
||||
public void addHeader(String name, String value) {
|
||||
this.response.addHeader(name, value);
|
||||
}
|
||||
|
||||
public FluentResponse cacheControl(String cacheControl) {
|
||||
response.setHeader(HttpHeader.CACHE_CONTROL, cacheControl);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean containsHeader(String name) {
|
||||
return this.response.containsHeader(name);
|
||||
}
|
||||
|
||||
public Header[] getAllHeaders() {
|
||||
return this.response.getAllHeaders();
|
||||
}
|
||||
|
||||
public String getCacheControl() {
|
||||
return getValueOfHeader(HttpHeader.CACHE_CONTROL);
|
||||
}
|
||||
|
||||
public InputStream getContent() throws IllegalStateException, IOException {
|
||||
return this.response.getEntity().getContent();
|
||||
}
|
||||
|
||||
public byte[] getContentByteArray() throws IllegalStateException,
|
||||
IOException {
|
||||
if (!consumed)
|
||||
loadContent();
|
||||
return content;
|
||||
}
|
||||
|
||||
public String getContentCharset() {
|
||||
return EntityUtils.getContentCharSet(getEntity());
|
||||
}
|
||||
|
||||
public String getContentEncoding() {
|
||||
if (this.getEntity() == null)
|
||||
throw new IllegalStateException("Response does not contain data");
|
||||
Header contentEncoding = this.getEntity().getContentEncoding();
|
||||
if (contentEncoding == null) {
|
||||
log.warn("Response does not contain Content-Encoding header");
|
||||
return System.getProperty("file.encoding");
|
||||
} else
|
||||
return contentEncoding.getValue();
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
String value = getValueOfHeader(HttpHeader.CONTENT_LENGTH);
|
||||
if (value == null)
|
||||
return -1;
|
||||
else {
|
||||
long contentLength = Long.parseLong(value);
|
||||
return contentLength;
|
||||
private void assertNotConsumed() {
|
||||
if (this.consumed) {
|
||||
throw new IllegalStateException("Response content has been already consumed");
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentString() throws IOException {
|
||||
if (contentString != null)
|
||||
return contentString;
|
||||
if (this.getEntity() == null)
|
||||
return null;
|
||||
String contentCharset = this.getContentCharset();
|
||||
return getContentString(contentCharset);
|
||||
}
|
||||
|
||||
public String getContentString(String encoding) throws IOException {
|
||||
if (contentString != null)
|
||||
return contentString;
|
||||
if (getContentByteArray() == null)
|
||||
return null;
|
||||
if (encoding == null)
|
||||
contentString = new String(content);
|
||||
else
|
||||
contentString = new String(content, encoding);
|
||||
return contentString;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
if (this.getEntity() == null)
|
||||
throw new IllegalStateException("Response does not contain data");
|
||||
Header contentType = this.getEntity().getContentType();
|
||||
if (contentType == null)
|
||||
throw new IllegalStateException(
|
||||
"Reponse does not contain Content-Type header");
|
||||
return contentType.getElements()[0].getName();
|
||||
}
|
||||
|
||||
public HttpEntity getEntity() {
|
||||
return this.response.getEntity();
|
||||
}
|
||||
|
||||
public Header getFirstHeader(String name) {
|
||||
return this.response.getFirstHeader(name);
|
||||
}
|
||||
|
||||
public Header[] getHeaders(String name) {
|
||||
return this.response.getHeaders(name);
|
||||
}
|
||||
|
||||
public Header getLastHeader(String name) {
|
||||
return this.response.getLastHeader(name);
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return this.response.getLocale();
|
||||
}
|
||||
|
||||
public HttpParams getParams() {
|
||||
return this.response.getParams();
|
||||
}
|
||||
|
||||
public ProtocolVersion getProtocolVersion() {
|
||||
return this.response.getProtocolVersion();
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return this.getStatusLine().getStatusCode();
|
||||
}
|
||||
|
||||
public StatusLine getStatusLine() {
|
||||
return this.response.getStatusLine();
|
||||
}
|
||||
|
||||
private String getValueOfHeader(String headerName) {
|
||||
Header header = response.getFirstHeader(headerName);
|
||||
if (header != null)
|
||||
return header.getValue();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public HeaderIterator headerIterator() {
|
||||
return this.response.headerIterator();
|
||||
}
|
||||
|
||||
public HeaderIterator headerIterator(String name) {
|
||||
return this.response.headerIterator(name);
|
||||
}
|
||||
|
||||
public FluentResponse loadContent() throws IOException {
|
||||
if (getEntity() == null)
|
||||
content = null;
|
||||
else {
|
||||
content = EntityUtils.toByteArray(getEntity());
|
||||
EntityUtils.consume(getEntity());
|
||||
public void dispose() {
|
||||
if (this.consumed) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
EntityUtils.consume(this.response.getEntity());
|
||||
} catch (Exception ignore) {
|
||||
} finally {
|
||||
this.consumed = true;
|
||||
}
|
||||
consumed = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeHeader(Header header) {
|
||||
this.response.removeHeader(header);
|
||||
public <T> T handle(final ResponseHandler<T> handler) throws ClientProtocolException, IOException {
|
||||
assertNotConsumed();
|
||||
try {
|
||||
return handler.handleResponse(this.response);
|
||||
} finally {
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeHeaders(String name) {
|
||||
this.response.removeHeaders(name);
|
||||
public Content content() throws ClientProtocolException, IOException {
|
||||
return handle(new ResponseHandler<Content>() {
|
||||
|
||||
public Content handleResponse(
|
||||
final HttpResponse response) throws ClientProtocolException, IOException {
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (statusLine.getStatusCode() >= 300) {
|
||||
throw new HttpResponseException(statusLine.getStatusCode(),
|
||||
statusLine.getReasonPhrase());
|
||||
}
|
||||
if (entity != null) {
|
||||
return new Content(EntityUtils.toByteArray(entity),
|
||||
ContentType.getOrDefault(entity));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public void setEntity(HttpEntity entity) {
|
||||
this.response.setEntity(entity);
|
||||
public HttpResponse response() throws IOException {
|
||||
assertNotConsumed();
|
||||
try {
|
||||
HttpEntity entity = this.response.getEntity();
|
||||
if (entity != null) {
|
||||
this.response.setEntity(new ByteArrayEntity(EntityUtils.toByteArray(entity),
|
||||
ContentType.getOrDefault(entity)));
|
||||
}
|
||||
return this.response;
|
||||
} finally {
|
||||
this.consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setHeader(Header header) {
|
||||
this.response.setHeader(header);
|
||||
public void save(final File file) throws IOException {
|
||||
assertNotConsumed();
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
try {
|
||||
HttpEntity entity = this.response.getEntity();
|
||||
if (entity != null) {
|
||||
entity.writeTo(out);
|
||||
}
|
||||
} finally {
|
||||
this.consumed = true;
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setHeader(String name, String value) {
|
||||
this.response.setHeader(name, value);
|
||||
}
|
||||
|
||||
public void setHeaders(Header[] headers) {
|
||||
this.response.setHeaders(headers);
|
||||
}
|
||||
|
||||
public void setLocale(Locale loc) {
|
||||
this.response.setLocale(loc);
|
||||
}
|
||||
|
||||
public void setParams(HttpParams params) {
|
||||
this.response.setParams(params);
|
||||
}
|
||||
|
||||
public void setReasonPhrase(String reason) throws IllegalStateException {
|
||||
this.response.setReasonPhrase(reason);
|
||||
}
|
||||
|
||||
public void setStatusCode(int code) throws IllegalStateException {
|
||||
this.response.setStatusCode(code);
|
||||
}
|
||||
|
||||
public void setStatusLine(ProtocolVersion ver, int code) {
|
||||
this.response.setStatusLine(ver, code);
|
||||
}
|
||||
|
||||
public void setStatusLine(ProtocolVersion ver, int code, String reason) {
|
||||
this.response.setStatusLine(ver, code, reason);
|
||||
}
|
||||
|
||||
public void setStatusLine(StatusLine statusline) {
|
||||
this.response.setStatusLine(statusline);
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.client.fluent.header;
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
class HttpHeader {
|
||||
|
||||
public class HttpHeader {
|
||||
public static final String CONTENT_LENGTH = "Content-Length";
|
||||
public static final String DATE = "Date";
|
||||
public static final String CACHE_CONTROL = CacheControl.NAME;
|
||||
public static final String CONTENT_TYPE = ContentType.NAME;
|
||||
public static final String CACHE_CONTROL = "Cache-Control";
|
||||
public static final String CONTENT_TYPE = "Content-Type";
|
||||
public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
|
||||
public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
|
||||
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
/*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.client.fluent.header;
|
||||
|
||||
public class CacheControl {
|
||||
|
||||
public static final String NAME = "Cache-Control";
|
||||
|
||||
/*
|
||||
* cache-request-directive
|
||||
*/
|
||||
public static final String NO_CACHE = "no-cache";
|
||||
public static final String NO_STORE = "no-store";
|
||||
public static final String MAX_STALE = "max-stale";
|
||||
public static final String NO_TRANSFORM = "no-transform";
|
||||
public static final String ONLY_IF_CACHED = "only-if-cached";
|
||||
|
||||
public static String MAX_AGE(final long deltaSeconds) {
|
||||
return "max-age=" + deltaSeconds;
|
||||
}
|
||||
|
||||
public static String MAX_STALE(final long deltaSeconds) {
|
||||
return MAX_STALE + "=" + deltaSeconds;
|
||||
}
|
||||
|
||||
public static String MIN_FRESH(final long deltaSeconds) {
|
||||
return "min-fresh=" + deltaSeconds;
|
||||
}
|
||||
|
||||
/*
|
||||
* cache-response-directive
|
||||
*/
|
||||
public static final String PUBLIC = "public";
|
||||
public static final String PRIVATE = "private";
|
||||
public static final String MUST_REVALIDATE = "must-revalidate";
|
||||
public static final String PROXY_REVALIDATE = "proxy-revalidate";
|
||||
|
||||
public static String S_MAXAGE(final long deltaSeconds) {
|
||||
return "s-maxage=" + deltaSeconds;
|
||||
}
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.client.fluent.header;
|
||||
|
||||
public class ContentType {
|
||||
public static final String NAME = "Content-Type";
|
||||
public static final String HTML = "text/html";
|
||||
public static final String JPEG = "image/jpeg";
|
||||
public static final String PNG = "image/png";
|
||||
public static final String GIF = "image/gif";
|
||||
public static final String TEXT = "text/plain";
|
||||
public static final String XML = "application/xml";
|
||||
public static final String JSON = "application/json";
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.client.fluent.header;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateUtils {
|
||||
public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||
public static final Locale DATE_LOCALE = Locale.US;
|
||||
public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT");
|
||||
|
||||
public static String format(Date d) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT, DATE_LOCALE);
|
||||
format.setTimeZone(TIME_ZONE);
|
||||
return format.format(d);
|
||||
}
|
||||
|
||||
public static String currentTime() {
|
||||
Date d = new Date();
|
||||
return format(d);
|
||||
}
|
||||
}
|
@ -1,392 +0,0 @@
|
||||
/*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.client.fluent;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.fluent.header.ContentType;
|
||||
import org.apache.http.client.fluent.header.DateUtils;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.localserver.LocalTestServer;
|
||||
import org.apache.http.params.CoreProtocolPNames;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HttpRequestHandler;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestFluentRequest {
|
||||
private static class SimpleService implements HttpRequestHandler {
|
||||
|
||||
public SimpleService() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void handle(final HttpRequest request,
|
||||
final HttpResponse response, final HttpContext context)
|
||||
throws HttpException, IOException {
|
||||
response.setStatusCode(HttpStatus.SC_OK);
|
||||
StringEntity entity = new StringEntity("Whatever");
|
||||
response.setEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private LocalTestServer localServer;
|
||||
|
||||
private URI getLocalServerURI() {
|
||||
int hostPort = localServer.getServiceAddress().getPort();
|
||||
String hostAddr = localServer.getServiceAddress().getAddress()
|
||||
.getHostAddress();
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI("http", null, hostAddr, hostPort, null, null, null);
|
||||
return uri;
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
localServer = new LocalTestServer(null, null);
|
||||
localServer.registerDefaultHandlers();
|
||||
localServer.start();
|
||||
localServer.register("*", new SimpleService());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (localServer != null)
|
||||
localServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheControl() {
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
String cacheControl = "no-cache";
|
||||
req.setCacheControl(cacheControl);
|
||||
assertEquals(cacheControl, req.getCacheControl());
|
||||
assertEquals(req.getFirstHeader("Cache-Control").getValue(),
|
||||
req.getCacheControl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionTimeout() throws Exception {
|
||||
// TODO how to delay the response from the localServer?
|
||||
int timeout = 1;
|
||||
URI uri = getLocalServerURI();
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
req.setConnectionTimeout(timeout);
|
||||
assertEquals(timeout, req.getConnectionTimeout());
|
||||
try {
|
||||
req.exec();
|
||||
// TODO: Delay local server's response
|
||||
// fail("ConnectTimeoutException exception is expected.");
|
||||
} catch (Exception e) {
|
||||
if (!(e instanceof ConnectTimeoutException)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentCharset() {
|
||||
URI uri = getLocalServerURI();
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
String charset = "UTF-8";
|
||||
req.setContentCharset(charset);
|
||||
assertEquals(charset, req.getContentCharset());
|
||||
assertEquals(
|
||||
req.getLocalParams().getParameter(
|
||||
CoreProtocolPNames.HTTP_CONTENT_CHARSET),
|
||||
req.getContentCharset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentLength() {
|
||||
int contentLength = 1000;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setContentLength(contentLength);
|
||||
assertEquals(contentLength, req.getContentLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentType() {
|
||||
String contentType = ContentType.HTML;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setContentType(contentType);
|
||||
assertEquals(contentType, req.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDate() {
|
||||
Date date = new Date();
|
||||
String dateValue = DateUtils.format(date);
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setDate(date);
|
||||
assertEquals(dateValue, req.getDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testElementCharset() {
|
||||
String charset = "UTF-8";
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setElementCharset(charset);
|
||||
assertEquals(charset, req.getElementCharset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExec() throws ClientProtocolException, IOException,
|
||||
URISyntaxException {
|
||||
URI uri = getLocalServerURI();
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
FluentResponse resp = req.exec();
|
||||
assertEquals(HttpStatus.SC_OK, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFluentRequestHttpUriRequest() {
|
||||
String uriString = "http://www.apache.org/";
|
||||
HttpUriRequest httpRequest = new HttpGet(uriString);
|
||||
FluentRequest req = new FluentRequest(httpRequest);
|
||||
assertEquals(uriString, req.getURI().toASCIIString());
|
||||
assertEquals("GET", req.getMethod().toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFluentRequestString() {
|
||||
String uriString = "http://www.apache.org/";
|
||||
FluentRequest req = new FluentRequest(uriString);
|
||||
assertEquals(uriString, req.getURI().toASCIIString());
|
||||
assertEquals("GET", req.getMethod().toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFluentRequestStringFluentHttpMethod() {
|
||||
String uriString = "http://www.apache.org/";
|
||||
FluentRequest req = new FluentRequest(uriString,
|
||||
FluentHttpMethod.POST_METHOD);
|
||||
assertEquals(uriString, req.getURI().toASCIIString());
|
||||
assertEquals("POST", req.getMethod().toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFluentRequestURI() throws URISyntaxException {
|
||||
String uriString = "http://www.apache.org/";
|
||||
URI uri = new URI(uriString);
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
assertEquals(req.getURI(), uri);
|
||||
assertEquals("GET", req.getMethod().toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFluentRequestURIFluentHttpMethod()
|
||||
throws URISyntaxException {
|
||||
String uriString = "http://www.apache.org/";
|
||||
URI uri = new URI(uriString);
|
||||
FluentRequest req = new FluentRequest(uri, FluentHttpMethod.HEAD_METHOD);
|
||||
assertEquals(req.getURI(), uri);
|
||||
assertEquals("HEAD", req.getMethod().toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHttpMethod() {
|
||||
FluentHttpMethod method = FluentHttpMethod.POST_METHOD;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/", method);
|
||||
assertEquals(method, req.getHttpMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetURI() {
|
||||
URI uri = getLocalServerURI();
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
assertEquals(uri, req.getURI());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHttpVersion() {
|
||||
HttpVersion procVersion = HttpVersion.HTTP_1_1;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setHttpVersion(procVersion);
|
||||
assertEquals(procVersion, req.getHttpVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfModifiedSince() {
|
||||
Date date = new Date();
|
||||
String dateValue = DateUtils.format(date);
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setIfModifiedSince(date);
|
||||
assertEquals(dateValue, req.getIfModifiedSince());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfUnmodifiedSince() {
|
||||
Date date = new Date();
|
||||
String dateValue = DateUtils.format(date);
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setIfUnmodifiedSince(date);
|
||||
assertEquals(dateValue, req.getIfUnmodifiedSince());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsUseExpectContinue() {
|
||||
boolean ueCont = true;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setUseExpectContinue(ueCont);
|
||||
assertEquals(ueCont, req.isUseExpectContinue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAuth() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveProxy() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAuthCredentials() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAuthStringString() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAuthStringStringStringString() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCredentialProvider() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEntity() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHTMLFormEntity() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetParams() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProxyAuthCredentials() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProxyAuthStringString() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProxyAuthStringStringStringString() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProxyStringInt() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProxyStringIntStringString() {
|
||||
// fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSocketTimeout() throws Exception {
|
||||
// TODO how to delay the response from the localServer?
|
||||
int timeout = 1;
|
||||
URI uri = getLocalServerURI();
|
||||
FluentRequest req = new FluentRequest(uri);
|
||||
req.setSocketTimeout(timeout);
|
||||
assertEquals(timeout, req.getSocketTimeout());
|
||||
try {
|
||||
req.exec();
|
||||
// TODO: Delay local server's response
|
||||
// fail("SocketTimeoutException exception is expected.");
|
||||
} catch (Exception e) {
|
||||
if (!(e instanceof SocketTimeoutException)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictTransferEncoding() {
|
||||
boolean stEnc = true;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setStrictTransferEncoding(stEnc);
|
||||
assertEquals(stEnc, req.isStrictTransferEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserAgent() {
|
||||
String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setUserAgent(userAgent);
|
||||
assertEquals(userAgent, req.getUserAgent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWaitForContinue() {
|
||||
int wait = 1000;
|
||||
FluentRequest req = new FluentRequest("http://www.apache.org/");
|
||||
req.setWaitForContinue(wait);
|
||||
assertEquals(wait, req.getWaitForContinue());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user