Avoid using implementation classes shipped with Android in HttpMime, HC Fluent, and Caching modules
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1568979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bd2585f515
commit
34675f33d7
|
@ -33,6 +33,7 @@ import javax.xml.parsers.DocumentBuilder;
|
|||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.StatusLine;
|
||||
|
@ -40,7 +41,6 @@ import org.apache.http.client.ClientProtocolException;
|
|||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class FluentResponseHandling {
|
|||
}
|
||||
Charset charset = contentType.getCharset();
|
||||
if (charset == null) {
|
||||
charset = HTTP.DEF_CONTENT_CHARSET;
|
||||
charset = Consts.ISO_8859_1;
|
||||
}
|
||||
return docBuilder.parse(entity.getContent(), charset.name());
|
||||
} catch (ParserConfigurationException ex) {
|
||||
|
|
|
@ -31,8 +31,8 @@ import java.io.InputStream;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
public class Content {
|
||||
|
||||
|
@ -58,7 +58,7 @@ public class Content {
|
|||
public String asString() {
|
||||
Charset charset = this.type.getCharset();
|
||||
if (charset == null) {
|
||||
charset = HTTP.DEF_CONTENT_CHARSET;
|
||||
charset = Consts.ISO_8859_1;
|
||||
}
|
||||
try {
|
||||
return new String(this.raw, charset.name());
|
||||
|
|
|
@ -44,7 +44,6 @@ import org.apache.http.client.ClientProtocolException;
|
|||
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.HttpClientContext;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
|
@ -125,7 +124,8 @@ public class Executor {
|
|||
}
|
||||
|
||||
public Executor auth(final HttpHost host, final Credentials creds) {
|
||||
final AuthScope authScope = host != null ? new AuthScope(host) : AuthScope.ANY;
|
||||
final AuthScope authScope = host != null ?
|
||||
new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
|
||||
return auth(authScope, creds);
|
||||
}
|
||||
|
||||
|
@ -202,8 +202,7 @@ public class Executor {
|
|||
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
|
||||
localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
|
||||
localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
|
||||
final HttpRequestBase httprequest = request.prepareRequest();
|
||||
httprequest.reset();
|
||||
final InternalHttpRequest httprequest = request.prepareRequest();
|
||||
return new Response(this.httpclient.execute(httprequest, localContext));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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 java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.util.Args;
|
||||
|
||||
class InternalByteArrayEntity extends AbstractHttpEntity implements Cloneable {
|
||||
|
||||
private final byte[] b;
|
||||
private final int off, len;
|
||||
|
||||
public InternalByteArrayEntity(final byte[] b, final ContentType contentType) {
|
||||
super();
|
||||
Args.notNull(b, "Source byte array");
|
||||
this.b = b;
|
||||
this.off = 0;
|
||||
this.len = this.b.length;
|
||||
if (contentType != null) {
|
||||
setContentType(contentType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public InternalByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
|
||||
super();
|
||||
Args.notNull(b, "Source byte array");
|
||||
if ((off < 0) || (off > b.length) || (len < 0) ||
|
||||
((off + len) < 0) || ((off + len) > b.length)) {
|
||||
throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
|
||||
}
|
||||
this.b = b;
|
||||
this.off = off;
|
||||
this.len = len;
|
||||
if (contentType != null) {
|
||||
setContentType(contentType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public InternalByteArrayEntity(final byte[] b) {
|
||||
this(b, null);
|
||||
}
|
||||
|
||||
public InternalByteArrayEntity(final byte[] b, final int off, final int len) {
|
||||
this(b, off, len, null);
|
||||
}
|
||||
|
||||
public boolean isRepeatable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
return this.len;
|
||||
}
|
||||
|
||||
public InputStream getContent() {
|
||||
return new ByteArrayInputStream(this.b, this.off, this.len);
|
||||
}
|
||||
|
||||
public void writeTo(final OutputStream outstream) throws IOException {
|
||||
Args.notNull(outstream, "Output stream");
|
||||
outstream.write(this.b, this.off, this.len);
|
||||
outstream.flush();
|
||||
}
|
||||
|
||||
public boolean isStreaming() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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 java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.util.Args;
|
||||
|
||||
class InternalFileEntity extends AbstractHttpEntity implements Cloneable {
|
||||
|
||||
private final File file;
|
||||
|
||||
public InternalFileEntity(final File file, final ContentType contentType) {
|
||||
super();
|
||||
this.file = Args.notNull(file, "File");
|
||||
if (contentType != null) {
|
||||
setContentType(contentType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRepeatable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
return this.file.length();
|
||||
}
|
||||
|
||||
public InputStream getContent() throws IOException {
|
||||
return new FileInputStream(this.file);
|
||||
}
|
||||
|
||||
public void writeTo(final OutputStream outstream) throws IOException {
|
||||
Args.notNull(outstream, "Output stream");
|
||||
final InputStream instream = new FileInputStream(this.file);
|
||||
try {
|
||||
final byte[] tmp = new byte[4096];
|
||||
int l;
|
||||
while ((l = instream.read(tmp)) != -1) {
|
||||
outstream.write(tmp, 0, l);
|
||||
}
|
||||
outstream.flush();
|
||||
} finally {
|
||||
instream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStreaming() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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 java.net.URI;
|
||||
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.RequestLine;
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.Configurable;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.message.AbstractHttpMessage;
|
||||
import org.apache.http.message.BasicRequestLine;
|
||||
import org.apache.http.util.Args;
|
||||
|
||||
@NotThreadSafe
|
||||
class InternalHttpRequest extends AbstractHttpMessage implements HttpUriRequest, Configurable {
|
||||
|
||||
private final String method;
|
||||
private ProtocolVersion version;
|
||||
private URI uri;
|
||||
private RequestConfig config;
|
||||
|
||||
InternalHttpRequest(final String method, final URI requestURI) {
|
||||
Args.notBlank(method, "Method");
|
||||
Args.notNull(requestURI, "Request URI");
|
||||
this.method = method;
|
||||
this.uri = requestURI;
|
||||
}
|
||||
|
||||
public void setProtocolVersion(final ProtocolVersion version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProtocolVersion getProtocolVersion() {
|
||||
return version != null ? version : HttpVersion.HTTP_1_1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort() throws UnsupportedOperationException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAborted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestLine getRequestLine() {
|
||||
final String method = getMethod();
|
||||
final ProtocolVersion ver = getProtocolVersion();
|
||||
final URI uri = getURI();
|
||||
String uritext = null;
|
||||
if (uri != null) {
|
||||
uritext = uri.toASCIIString();
|
||||
}
|
||||
if (uritext == null || uritext.isEmpty()) {
|
||||
uritext = "/";
|
||||
}
|
||||
return new BasicRequestLine(method, uritext, ver);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RequestConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(final RequestConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void setURI(final URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getMethod() + " " + getURI() + " " + getProtocolVersion();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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 java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.util.Args;
|
||||
|
||||
class InternalInputStreamEntity extends AbstractHttpEntity {
|
||||
|
||||
private final InputStream content;
|
||||
private final long length;
|
||||
|
||||
public InternalInputStreamEntity(final InputStream instream, final long length, final ContentType contentType) {
|
||||
super();
|
||||
this.content = Args.notNull(instream, "Source input stream");
|
||||
this.length = length;
|
||||
if (contentType != null) {
|
||||
setContentType(contentType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRepeatable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
public InputStream getContent() throws IOException {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
public void writeTo(final OutputStream outstream) throws IOException {
|
||||
Args.notNull(outstream, "Output stream");
|
||||
final InputStream instream = this.content;
|
||||
try {
|
||||
final byte[] buffer = new byte[4096];
|
||||
int l;
|
||||
if (this.length < 0) {
|
||||
// consume until EOF
|
||||
while ((l = instream.read(buffer)) != -1) {
|
||||
outstream.write(buffer, 0, l);
|
||||
}
|
||||
} else {
|
||||
// consume no more than length
|
||||
long remaining = this.length;
|
||||
while (remaining > 0) {
|
||||
l = instream.read(buffer, 0, (int)Math.min(4096, remaining));
|
||||
if (l == -1) {
|
||||
break;
|
||||
}
|
||||
outstream.write(buffer, 0, l);
|
||||
remaining -= l;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
instream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStreaming() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -32,11 +32,14 @@ import java.io.InputStream;
|
|||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
|
@ -45,20 +48,15 @@ import org.apache.http.HttpVersion;
|
|||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
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.entity.ByteArrayEntity;
|
||||
import org.apache.http.client.utils.URLEncodedUtils;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.FileEntity;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
public class Request {
|
||||
|
@ -67,74 +65,74 @@ public class Request {
|
|||
public static final Locale DATE_LOCALE = Locale.US;
|
||||
public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT");
|
||||
|
||||
private final HttpRequestBase request;
|
||||
private final InternalHttpRequest request;
|
||||
private final RequestConfig.Builder configBuilder;
|
||||
|
||||
private SimpleDateFormat dateFormatter;
|
||||
|
||||
public static Request Get(final URI uri) {
|
||||
return new Request(new HttpGet(uri));
|
||||
return new Request(HttpGet.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Get(final String uri) {
|
||||
return new Request(new HttpGet(uri));
|
||||
return new Request(HttpGet.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Head(final URI uri) {
|
||||
return new Request(new HttpHead(uri));
|
||||
return new Request(HttpHead.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Head(final String uri) {
|
||||
return new Request(new HttpHead(uri));
|
||||
return new Request(HttpHead.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Post(final URI uri) {
|
||||
return new Request(new HttpPost(uri));
|
||||
return new Request(HttpPost.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Post(final String uri) {
|
||||
return new Request(new HttpPost(uri));
|
||||
return new Request(HttpPost.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Put(final URI uri) {
|
||||
return new Request(new HttpPut(uri));
|
||||
return new Request(HttpPut.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Put(final String uri) {
|
||||
return new Request(new HttpPut(uri));
|
||||
return new Request(HttpPut.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Trace(final URI uri) {
|
||||
return new Request(new HttpTrace(uri));
|
||||
return new Request(HttpTrace.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Trace(final String uri) {
|
||||
return new Request(new HttpTrace(uri));
|
||||
return new Request(HttpTrace.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Delete(final URI uri) {
|
||||
return new Request(new HttpDelete(uri));
|
||||
return new Request(HttpDelete.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Delete(final String uri) {
|
||||
return new Request(new HttpDelete(uri));
|
||||
return new Request(HttpDelete.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
public static Request Options(final URI uri) {
|
||||
return new Request(new HttpOptions(uri));
|
||||
return new Request(HttpOptions.METHOD_NAME, uri);
|
||||
}
|
||||
|
||||
public static Request Options(final String uri) {
|
||||
return new Request(new HttpOptions(uri));
|
||||
return new Request(HttpOptions.METHOD_NAME, URI.create(uri));
|
||||
}
|
||||
|
||||
Request(final HttpRequestBase request) {
|
||||
Request(final String method, final URI requestURI) {
|
||||
super();
|
||||
this.request = request;
|
||||
this.request = new InternalHttpRequest(method, requestURI);
|
||||
this.configBuilder = RequestConfig.custom();
|
||||
}
|
||||
|
||||
HttpRequestBase prepareRequest() {
|
||||
InternalHttpRequest prepareRequest() {
|
||||
this.request.setConfig(this.configBuilder.build());
|
||||
return this.request;
|
||||
}
|
||||
|
@ -304,39 +302,47 @@ public class Request {
|
|||
}
|
||||
|
||||
public Request bodyForm(final Iterable <? extends NameValuePair> formParams, final Charset charset) {
|
||||
return body(new UrlEncodedFormEntity(formParams, charset));
|
||||
final List<NameValuePair> paramList = new ArrayList<NameValuePair>();
|
||||
for (NameValuePair param : formParams) {
|
||||
paramList.add(param);
|
||||
}
|
||||
final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset);
|
||||
final String s = URLEncodedUtils.format(paramList, charset != null ? charset.name() : null);
|
||||
return bodyString(s, contentType);
|
||||
}
|
||||
|
||||
public Request bodyForm(final Iterable <? extends NameValuePair> formParams) {
|
||||
return bodyForm(formParams, HTTP.DEF_CONTENT_CHARSET);
|
||||
return bodyForm(formParams, Consts.ISO_8859_1);
|
||||
}
|
||||
|
||||
public Request bodyForm(final NameValuePair... formParams) {
|
||||
return bodyForm(Arrays.asList(formParams), HTTP.DEF_CONTENT_CHARSET);
|
||||
return bodyForm(Arrays.asList(formParams), Consts.ISO_8859_1);
|
||||
}
|
||||
|
||||
public Request bodyString(final String s, final ContentType contentType) {
|
||||
return body(new StringEntity(s, contentType));
|
||||
final Charset charset = contentType != null ? contentType.getCharset() : null;
|
||||
final byte[] raw = charset != null ? s.getBytes(charset) : s.getBytes();
|
||||
return body(new InternalByteArrayEntity(raw, contentType));
|
||||
}
|
||||
|
||||
public Request bodyFile(final File file, final ContentType contentType) {
|
||||
return body(new FileEntity(file, contentType));
|
||||
return body(new InternalFileEntity(file, contentType));
|
||||
}
|
||||
|
||||
public Request bodyByteArray(final byte[] b) {
|
||||
return body(new ByteArrayEntity(b));
|
||||
return body(new InternalByteArrayEntity(b));
|
||||
}
|
||||
|
||||
public Request bodyByteArray(final byte[] b, final int off, final int len) {
|
||||
return body(new ByteArrayEntity(b, off, len));
|
||||
return body(new InternalByteArrayEntity(b, off, len));
|
||||
}
|
||||
|
||||
public Request bodyStream(final InputStream instream) {
|
||||
return body(new InputStreamEntity(instream, -1));
|
||||
return body(new InternalInputStreamEntity(instream, -1, null));
|
||||
}
|
||||
|
||||
public Request bodyStream(final InputStream instream, final ContentType contentType) {
|
||||
return body(new InputStreamEntity(instream, -1, contentType));
|
||||
return body(new InternalInputStreamEntity(instream, -1, contentType));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -29,6 +29,7 @@ package org.apache.http.client.fluent;
|
|||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
@ -61,7 +62,11 @@ public class Response {
|
|||
return;
|
||||
}
|
||||
try {
|
||||
EntityUtils.consume(this.response.getEntity());
|
||||
final HttpEntity entity = this.response.getEntity();
|
||||
final InputStream content = entity.getContent();
|
||||
if (content != null) {
|
||||
content.close();
|
||||
}
|
||||
} catch (final Exception ignore) {
|
||||
} finally {
|
||||
this.consumed = true;
|
||||
|
@ -97,8 +102,11 @@ public class Response {
|
|||
try {
|
||||
final HttpEntity entity = this.response.getEntity();
|
||||
if (entity != null) {
|
||||
this.response.setEntity(new ByteArrayEntity(EntityUtils.toByteArray(entity),
|
||||
ContentType.getOrDefault(entity)));
|
||||
final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
|
||||
EntityUtils.toByteArray(entity));
|
||||
final ContentType contentType = ContentType.getOrDefault(entity);
|
||||
byteArrayEntity.setContentType(contentType.toString());
|
||||
this.response.setEntity(byteArrayEntity);
|
||||
}
|
||||
return this.response;
|
||||
} finally {
|
||||
|
|
|
@ -78,7 +78,6 @@ import org.apache.http.protocol.ExecutionContext;
|
|||
import org.apache.http.protocol.HTTP;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.util.Args;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.http.util.VersionInfo;
|
||||
|
||||
/**
|
||||
|
@ -381,7 +380,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
} catch (final Exception t) {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
try {
|
||||
EntityUtils.consume(entity);
|
||||
IOUtils.consume(entity);
|
||||
} catch (final Exception t2) {
|
||||
// Log this exception. The original exception is more
|
||||
// important and will be thrown to the caller.
|
||||
|
@ -399,7 +398,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
// Handling the response was successful. Ensure that the content has
|
||||
// been fully consumed.
|
||||
final HttpEntity entity = response.getEntity();
|
||||
EntityUtils.consume(entity);
|
||||
IOUtils.consume(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -804,7 +803,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
final HttpCacheEntry matchedEntry = matchingVariant.getEntry();
|
||||
|
||||
if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
|
||||
EntityUtils.consume(backendResponse.getEntity());
|
||||
IOUtils.consume(backendResponse.getEntity());
|
||||
return retryRequestUnconditionally(target, request, context,
|
||||
matchedEntry);
|
||||
}
|
||||
|
@ -875,7 +874,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
Date responseDate = getCurrentDate();
|
||||
|
||||
if (revalidationResponseIsTooOld(backendResponse, cacheEntry)) {
|
||||
EntityUtils.consume(backendResponse.getEntity());
|
||||
IOUtils.consume(backendResponse.getEntity());
|
||||
final HttpRequest unconditional = conditionalRequestBuilder
|
||||
.buildUnconditionalRequest(request, cacheEntry);
|
||||
requestDate = getCurrentDate();
|
||||
|
@ -907,7 +906,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
cachedResponse.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
|
||||
final HttpEntity errorBody = backendResponse.getEntity();
|
||||
if (errorBody != null) {
|
||||
EntityUtils.consume(errorBody);
|
||||
IOUtils.consume(errorBody);
|
||||
}
|
||||
return cachedResponse;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,6 @@ import org.apache.http.message.BasicHttpResponse;
|
|||
import org.apache.http.protocol.HTTP;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.util.Args;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.http.util.VersionInfo;
|
||||
|
||||
/**
|
||||
|
@ -635,7 +634,7 @@ public class CachingExec implements ClientExecChain {
|
|||
final Header resultEtagHeader = backendResponse.getFirstHeader(HeaderConstants.ETAG);
|
||||
if (resultEtagHeader == null) {
|
||||
log.warn("304 response did not contain ETag");
|
||||
EntityUtils.consume(backendResponse.getEntity());
|
||||
IOUtils.consume(backendResponse.getEntity());
|
||||
backendResponse.close();
|
||||
return callBackend(route, request, context, execAware);
|
||||
}
|
||||
|
@ -644,7 +643,7 @@ public class CachingExec implements ClientExecChain {
|
|||
final Variant matchingVariant = variants.get(resultEtag);
|
||||
if (matchingVariant == null) {
|
||||
log.debug("304 response did not contain ETag matching one sent in If-None-Match");
|
||||
EntityUtils.consume(backendResponse.getEntity());
|
||||
IOUtils.consume(backendResponse.getEntity());
|
||||
backendResponse.close();
|
||||
return callBackend(route, request, context, execAware);
|
||||
}
|
||||
|
@ -652,7 +651,7 @@ public class CachingExec implements ClientExecChain {
|
|||
final HttpCacheEntry matchedEntry = matchingVariant.getEntry();
|
||||
|
||||
if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
|
||||
EntityUtils.consume(backendResponse.getEntity());
|
||||
IOUtils.consume(backendResponse.getEntity());
|
||||
backendResponse.close();
|
||||
return retryRequestUnconditionally(route, request, context, execAware, matchedEntry);
|
||||
}
|
||||
|
|
|
@ -34,11 +34,24 @@ import java.io.OutputStream;
|
|||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
|
||||
@Immutable
|
||||
class IOUtils {
|
||||
|
||||
static void consume(final HttpEntity entity) throws IOException {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
if (entity.isStreaming()) {
|
||||
final InputStream instream = entity.getContent();
|
||||
if (instream != null) {
|
||||
instream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void copy(final InputStream in, final OutputStream out) throws IOException {
|
||||
final byte[] buf = new byte[2048];
|
||||
int len;
|
||||
|
|
|
@ -46,7 +46,6 @@ import org.apache.http.client.methods.HttpRequestWrapper;
|
|||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
* @since 4.1
|
||||
|
@ -94,7 +93,7 @@ class ResponseProtocolCompliance {
|
|||
private void consumeBody(final HttpResponse response) throws IOException {
|
||||
final HttpEntity body = response.getEntity();
|
||||
if (body != null) {
|
||||
EntityUtils.consume(body);
|
||||
IOUtils.consume(body);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ import java.lang.reflect.Method;
|
|||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
* A proxy class that can enhance an arbitrary {@link HttpResponse} with
|
||||
|
@ -64,7 +63,7 @@ class ResponseProxyHandler implements InvocationHandler {
|
|||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
EntityUtils.consume(original.getEntity());
|
||||
IOUtils.consume(original.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -62,7 +62,6 @@ import org.apache.http.entity.BasicHttpEntity;
|
|||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -510,7 +509,7 @@ public class TestBasicHttpCache {
|
|||
originResponse.setHeader("ETag", "\"etag\"");
|
||||
|
||||
final HttpResponse result = impl.cacheAndReturnResponse(host, request, originResponse, requestSent, responseReceived);
|
||||
EntityUtils.consume(result.getEntity());
|
||||
IOUtils.consume(result.getEntity());
|
||||
assertTrue(inputStream.wasClosed());
|
||||
}
|
||||
|
||||
|
|
|
@ -341,7 +341,7 @@ public class TestCachingExec extends TestCachingExecChain {
|
|||
}
|
||||
return 'y';
|
||||
}
|
||||
}));
|
||||
}, -1));
|
||||
|
||||
final CloseableHttpResponse resp = mockBackend.execute(
|
||||
EasyMock.isA(HttpRoute.class),
|
||||
|
|
|
@ -38,11 +38,10 @@ import org.apache.http.client.methods.HttpExecutionAware;
|
|||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpRequestWrapper;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.impl.execchain.ClientExecChain;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
|
@ -116,7 +115,7 @@ public class TestHttpCacheJiraNumber1147 {
|
|||
|
||||
final HttpResponse response1 = t.execute(route, get, context, null);
|
||||
Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
|
||||
EntityUtils.consume(response1.getEntity());
|
||||
IOUtils.consume(response1.getEntity());
|
||||
|
||||
EasyMock.verify(backend);
|
||||
|
||||
|
@ -132,7 +131,7 @@ public class TestHttpCacheJiraNumber1147 {
|
|||
|
||||
final HttpResponse response2 = t.execute(route, get, context, null);
|
||||
Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
|
||||
EntityUtils.consume(response2.getEntity());
|
||||
IOUtils.consume(response2.getEntity());
|
||||
|
||||
EasyMock.verify(backend);
|
||||
}
|
||||
|
|
|
@ -26,13 +26,19 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.MethodNotSupportedException;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.cache.CacheResponseStatus;
|
||||
|
@ -42,22 +48,14 @@ import org.apache.http.client.methods.HttpGet;
|
|||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.localserver.LocalTestServer;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HttpRequestHandler;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import org.apache.http.localserver.LocalTestServer;
|
||||
|
||||
/**
|
||||
* Test that after background validation that a subsequent request for non cached
|
||||
* conent can be made. This verifies that the connection has been release back to
|
||||
|
@ -205,7 +203,7 @@ public class TestStaleWhileRevalidationReleasesConnection {
|
|||
if(response!=null) {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
try {
|
||||
EntityUtils.consume(entity);
|
||||
IOUtils.consume(entity);
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue