com.landawn.abacus.http.HttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (C) 2015 HaiYang Li
*
* Licensed 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.
*/
package com.landawn.abacus.http;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.http.HttpHeaders.Names;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.AsyncExecutor;
import com.landawn.abacus.util.BufferedReader;
import com.landawn.abacus.util.BufferedWriter;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.ExceptionUtil;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Objectory;
import com.landawn.abacus.util.URLEncodedUtil;
/**
* Any header can be set into the parameter settings
*
*
HttpClient is thread safe.
*
* @author Haiyang Li
* @since 0.8
*/
public final class HttpClient {
static final Logger logger = LoggerFactory.getLogger(HttpClient.class);
static {
if (IOUtil.IS_PLATFORM_ANDROID) {
// ignore
} else {
final int maxConnections = IOUtil.CPU_CORES * 16;
System.setProperty("http.keepAlive", "true");
System.setProperty("http.maxConnections", String.valueOf(maxConnections));
}
}
// ...
public static final int DEFAULT_MAX_CONNECTION = 16;
/** Unit is milliseconds. */
public static final int DEFAULT_CONNECTION_TIMEOUT = 8000;
public static final int DEFAULT_READ_TIMEOUT = 16000;
// ...
protected final String _url;
protected final int _maxConnection;
protected final long _connectionTimeoutInMillis;
protected final long _readTimeoutInMillis;
protected final HttpSettings _settings;
protected final AsyncExecutor _asyncExecutor;
protected final URL _netURL;
protected final AtomicInteger _activeConnectionCounter;
protected HttpClient(String url) {
this(url, DEFAULT_MAX_CONNECTION);
}
protected HttpClient(String url, int maxConnection) {
this(url, maxConnection, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_READ_TIMEOUT);
}
protected HttpClient(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis) {
this(url, maxConnection, connectionTimeoutInMillis, readTimeoutInMillis, null);
}
protected HttpClient(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis, HttpSettings settings)
throws UncheckedIOException {
this(url, maxConnection, connectionTimeoutInMillis, readTimeoutInMillis, settings, new AtomicInteger(0));
}
protected HttpClient(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis, HttpSettings settings,
final AtomicInteger sharedActiveConnectionCounter) {
if (N.isNullOrEmpty(url)) {
throw new IllegalArgumentException("url can't be null or empty");
}
if ((maxConnection < 0) || (connectionTimeoutInMillis < 0) || (readTimeoutInMillis < 0)) {
throw new IllegalArgumentException("maxConnection, connectionTimeoutInMillis or readTimeoutInMillis can't be less than 0: " + maxConnection + ", "
+ connectionTimeoutInMillis + ", " + readTimeoutInMillis);
}
this._url = url;
this._maxConnection = (maxConnection == 0) ? DEFAULT_MAX_CONNECTION : maxConnection;
this._connectionTimeoutInMillis = (connectionTimeoutInMillis == 0) ? DEFAULT_CONNECTION_TIMEOUT : connectionTimeoutInMillis;
this._readTimeoutInMillis = (readTimeoutInMillis == 0) ? DEFAULT_READ_TIMEOUT : readTimeoutInMillis;
this._settings = settings == null ? HttpSettings.create() : settings;
_asyncExecutor = new AsyncExecutor(Math.min(8, this._maxConnection), this._maxConnection, 300L, TimeUnit.SECONDS);
try {
this._netURL = new URL(url);
} catch (MalformedURLException e) {
throw ExceptionUtil.toRuntimeException(e);
}
this._activeConnectionCounter = sharedActiveConnectionCounter;
}
/**
*
* @param url
* @return
*/
public static HttpClient create(String url) {
return new HttpClient(url);
}
/**
*
* @param url
* @param maxConnection
* @return
*/
public static HttpClient create(String url, int maxConnection) {
return new HttpClient(url, maxConnection);
}
/**
*
* @param url
* @param connectionTimeoutInMillis
* @param readTimeoutInMillis
* @return
*/
public static HttpClient create(String url, long connectionTimeoutInMillis, long readTimeoutInMillis) {
return new HttpClient(url, DEFAULT_MAX_CONNECTION, connectionTimeoutInMillis, readTimeoutInMillis);
}
/**
*
* @param url
* @param maxConnection
* @param connectionTimeoutInMillis
* @param readTimeoutInMillis
* @return
*/
public static HttpClient create(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis) {
return new HttpClient(url, maxConnection, connectionTimeoutInMillis, readTimeoutInMillis);
}
/**
*
* @param url
* @param maxConnection
* @param connectionTimeoutInMillis
* @param readTimeoutInMillis
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public static HttpClient create(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis, HttpSettings settings)
throws UncheckedIOException {
return new HttpClient(url, maxConnection, connectionTimeoutInMillis, readTimeoutInMillis, settings);
}
/**
*
* @param url
* @param maxConnection
* @param connectionTimeoutInMillis
* @param readTimeoutInMillis
* @param settings
* @param sharedActiveConnectionCounter
* @return
*/
public static HttpClient create(String url, int maxConnection, long connectionTimeoutInMillis, long readTimeoutInMillis, HttpSettings settings,
final AtomicInteger sharedActiveConnectionCounter) {
return new HttpClient(url, maxConnection, connectionTimeoutInMillis, readTimeoutInMillis, settings, sharedActiveConnectionCounter);
}
public String url() {
return _url;
}
/**
*
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String get() throws UncheckedIOException {
return get(String.class);
}
/**
*
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String get(final HttpSettings settings) throws UncheckedIOException {
return get(String.class, settings);
}
/**
*
* @param queryParameters
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String get(final Object queryParameters) throws UncheckedIOException {
return get(String.class, queryParameters);
}
/**
*
* @param queryParameters
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String get(final Object queryParameters, final HttpSettings settings) throws UncheckedIOException {
return get(String.class, queryParameters, settings);
}
/**
*
* @param
* @param resultClass
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T get(final Class resultClass) throws UncheckedIOException {
return get(resultClass, null, _settings);
}
/**
*
* @param
* @param resultClass
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T get(final Class resultClass, final HttpSettings settings) throws UncheckedIOException {
return get(resultClass, null, settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T get(final Class resultClass, final Object queryParameters) throws UncheckedIOException {
return get(resultClass, queryParameters, _settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T get(final Class resultClass, final Object queryParameters, final HttpSettings settings) throws UncheckedIOException {
return execute(resultClass, HttpMethod.GET, queryParameters, settings);
}
public ContinuableFuture asyncGet() {
return asyncGet(String.class);
}
/**
*
* @param settings
* @return
*/
public ContinuableFuture asyncGet(final HttpSettings settings) {
return asyncGet(String.class, settings);
}
/**
*
* @param queryParameters
* @return
*/
public ContinuableFuture asyncGet(final Object queryParameters) {
return asyncGet(String.class, queryParameters);
}
/**
*
* @param queryParameters
* @param settings
* @return
*/
public ContinuableFuture asyncGet(final Object queryParameters, final HttpSettings settings) {
return asyncGet(String.class, queryParameters, settings);
}
/**
*
* @param
* @param resultClass
* @return
*/
public ContinuableFuture asyncGet(final Class resultClass) {
return asyncGet(resultClass, null, _settings);
}
/**
*
* @param
* @param resultClass
* @param settings
* @return
*/
public ContinuableFuture asyncGet(final Class resultClass, final HttpSettings settings) {
return asyncGet(resultClass, null, settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @return
*/
public ContinuableFuture asyncGet(final Class resultClass, final Object queryParameters) {
return asyncGet(resultClass, queryParameters, _settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @param settings
* @return
*/
public ContinuableFuture asyncGet(final Class resultClass, final Object queryParameters, final HttpSettings settings) {
return asyncExecute(resultClass, HttpMethod.GET, queryParameters, settings);
}
/**
*
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String delete() throws UncheckedIOException {
return delete(String.class);
}
/**
*
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String delete(final HttpSettings settings) throws UncheckedIOException {
return delete(String.class, settings);
}
/**
*
* @param queryParameters
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String delete(final Object queryParameters) throws UncheckedIOException {
return delete(String.class, queryParameters);
}
/**
*
* @param queryParameters
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String delete(final Object queryParameters, final HttpSettings settings) throws UncheckedIOException {
return delete(String.class, queryParameters, settings);
}
/**
*
* @param
* @param resultClass
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T delete(final Class resultClass) throws UncheckedIOException {
return delete(resultClass, null, _settings);
}
/**
*
* @param
* @param resultClass
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T delete(final Class resultClass, final HttpSettings settings) throws UncheckedIOException {
return delete(resultClass, null, settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T delete(final Class resultClass, final Object queryParameters) throws UncheckedIOException {
return delete(resultClass, queryParameters, _settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T delete(final Class resultClass, final Object queryParameters, final HttpSettings settings) throws UncheckedIOException {
return execute(resultClass, HttpMethod.DELETE, queryParameters, settings);
}
public ContinuableFuture asyncDelete() {
return asyncDelete(String.class);
}
/**
*
* @param settings
* @return
*/
public ContinuableFuture asyncDelete(final HttpSettings settings) {
return asyncDelete(String.class, settings);
}
/**
*
* @param queryParameters
* @return
*/
public ContinuableFuture asyncDelete(final Object queryParameters) {
return asyncDelete(String.class, queryParameters);
}
/**
*
* @param queryParameters
* @param settings
* @return
*/
public ContinuableFuture asyncDelete(final Object queryParameters, final HttpSettings settings) {
return asyncDelete(String.class, queryParameters, settings);
}
/**
*
* @param
* @param resultClass
* @return
*/
public ContinuableFuture asyncDelete(final Class resultClass) {
return asyncDelete(resultClass, null, _settings);
}
/**
*
* @param
* @param resultClass
* @param settings
* @return
*/
public ContinuableFuture asyncDelete(final Class resultClass, final HttpSettings settings) {
return asyncDelete(resultClass, null, settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @return
*/
public ContinuableFuture asyncDelete(final Class resultClass, final Object queryParameters) {
return asyncDelete(resultClass, queryParameters, _settings);
}
/**
*
* @param
* @param resultClass
* @param queryParameters
* @param settings
* @return
*/
public ContinuableFuture asyncDelete(final Class resultClass, final Object queryParameters, final HttpSettings settings) {
return asyncExecute(resultClass, HttpMethod.DELETE, queryParameters, settings);
}
/**
*
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String post(final Object request) throws UncheckedIOException {
return post(String.class, request);
}
/**
*
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String post(final Object request, final HttpSettings settings) throws UncheckedIOException {
return post(String.class, request, settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T post(final Class resultClass, final Object request) throws UncheckedIOException {
return post(resultClass, request, _settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T post(final Class resultClass, final Object request, final HttpSettings settings) throws UncheckedIOException {
return execute(resultClass, HttpMethod.POST, request, settings);
}
/**
*
* @param request
* @return
*/
public ContinuableFuture asyncPost(final Object request) {
return asyncPost(String.class, request);
}
/**
*
* @param request
* @param settings
* @return
*/
public ContinuableFuture asyncPost(final Object request, final HttpSettings settings) {
return asyncPost(String.class, request, settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @return
*/
public ContinuableFuture asyncPost(final Class resultClass, final Object request) {
return asyncPost(resultClass, request, _settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @param settings
* @return
*/
public ContinuableFuture asyncPost(final Class resultClass, final Object request, final HttpSettings settings) {
return asyncExecute(resultClass, HttpMethod.POST, request, settings);
}
/**
*
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String put(final Object request) throws UncheckedIOException {
return put(String.class, request);
}
/**
*
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String put(final Object request, final HttpSettings settings) throws UncheckedIOException {
return put(String.class, request, settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T put(final Class resultClass, final Object request) throws UncheckedIOException {
return put(resultClass, request, _settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T put(final Class resultClass, final Object request, final HttpSettings settings) throws UncheckedIOException {
return execute(resultClass, HttpMethod.PUT, request, settings);
}
/**
*
* @param request
* @return
*/
public ContinuableFuture asyncPut(final Object request) {
return asyncPut(String.class, request);
}
/**
*
* @param request
* @param settings
* @return
*/
public ContinuableFuture asyncPut(final Object request, final HttpSettings settings) {
return asyncPut(String.class, request, settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @return
*/
public ContinuableFuture asyncPut(final Class resultClass, final Object request) {
return asyncPut(resultClass, request, _settings);
}
/**
*
* @param
* @param resultClass
* @param request
* @param settings
* @return
*/
public ContinuableFuture asyncPut(final Class resultClass, final Object request, final HttpSettings settings) {
return asyncExecute(resultClass, HttpMethod.PUT, request, settings);
}
// TODO HTTP METHOD PATCH is not supported by HttpURLConnection.
// /**
// *
// * @param request
// * @return
// * @throws UncheckedIOException the unchecked IO exception
// */
// public String patch(final Object request) throws UncheckedIOException {
// return patch(String.class, request);
// }
//
// /**
// *
// * @param request
// * @param settings
// * @return
// * @throws UncheckedIOException the unchecked IO exception
// */
// public String patch(final Object request, final HttpSettings settings) throws UncheckedIOException {
// return patch(String.class, request, settings);
// }
//
// /**
// *
// * @param
// * @param resultClass
// * @param request
// * @return
// * @throws UncheckedIOException the unchecked IO exception
// */
// public T patch(final Class resultClass, final Object request) throws UncheckedIOException {
// return patch(resultClass, request, _settings);
// }
//
// /**
// *
// * @param
// * @param resultClass
// * @param request
// * @param settings
// * @return
// * @throws UncheckedIOException the unchecked IO exception
// */
// public T patch(final Class resultClass, final Object request, final HttpSettings settings) throws UncheckedIOException {
// return execute(resultClass, HttpMethod.PATCH, request, settings);
// }
//
// /**
// *
// * @param request
// * @return
// */
// public ContinuableFuture asyncPatch(final Object request) {
// return asyncPatch(String.class, request);
// }
//
// /**
// *
// * @param request
// * @param settings
// * @return
// */
// public ContinuableFuture asyncPatch(final Object request, final HttpSettings settings) {
// return asyncPatch(String.class, request, settings);
// }
//
// /**
// *
// * @param
// * @param resultClass
// * @param request
// * @return
// */
// public ContinuableFuture asyncPatch(final Class resultClass, final Object request) {
// return asyncPatch(resultClass, request, _settings);
// }
//
// /**
// *
// * @param
// * @param resultClass
// * @param request
// * @param settings
// * @return
// */
// public ContinuableFuture asyncPatch(final Class resultClass, final Object request, final HttpSettings settings) {
// return asyncExecute(resultClass, HttpMethod.PATCH, request, settings);
// }
/**
*
* @param httpMethod
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String execute(final HttpMethod httpMethod, final Object request) throws UncheckedIOException {
return execute(String.class, httpMethod, request);
}
/**
*
* @param httpMethod
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public String execute(final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
return execute(String.class, httpMethod, request, settings);
}
/**
*
* @param
* @param resultClass
* @param httpMethod
* @param request
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T execute(final Class resultClass, final HttpMethod httpMethod, final Object request) throws UncheckedIOException {
return execute(resultClass, httpMethod, request, _settings);
}
/**
*
* @param
* @param resultClass
* @param httpMethod
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
public T execute(final Class resultClass, final HttpMethod httpMethod, final Object request, final HttpSettings settings)
throws UncheckedIOException {
return execute(resultClass, null, null, httpMethod, request, settings);
}
/**
*
* @param output
* @param httpMethod
* @param request
* @param settings
* @throws UncheckedIOException the unchecked IO exception
*/
public void execute(final File output, final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
OutputStream os = null;
try {
os = new FileOutputStream(output);
execute(os, httpMethod, request, settings);
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.close(os);
}
}
/**
*
* @param output
* @param httpMethod
* @param request
* @param settings
* @throws UncheckedIOException the unchecked IO exception
*/
public void execute(final OutputStream output, final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
execute(null, output, null, httpMethod, request, settings);
}
/**
*
* @param output
* @param httpMethod
* @param request
* @param settings
* @throws UncheckedIOException the unchecked IO exception
*/
public void execute(final Writer output, final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
execute(null, null, output, httpMethod, request, settings);
}
/**
*
* @param
* @param resultClass
* @param outputStream
* @param outputWriter
* @param httpMethod
* @param request
* @param settings
* @return
* @throws UncheckedIOException the unchecked IO exception
*/
private T execute(final Class resultClass, final OutputStream outputStream, final Writer outputWriter, final HttpMethod httpMethod,
final Object request, final HttpSettings settings) throws UncheckedIOException {
final Charset requestCharset = HttpUtil.getRequestCharset(settings == null || settings.headers().isEmpty() ? _settings.headers() : settings.headers());
final ContentFormat requestContentFormat = getContentFormat(settings);
final boolean doOutput = request != null && !(httpMethod.equals(HttpMethod.GET) || httpMethod.equals(HttpMethod.DELETE));
final HttpURLConnection connection = openConnection(httpMethod, request, doOutput, settings);
final long sentRequestAtMillis = System.currentTimeMillis();
InputStream is = null;
OutputStream os = null;
try {
if (request != null && (requireBody(httpMethod))) {
os = HttpUtil.getOutputStream(connection, requestContentFormat, getContentType(settings), getContentEncoding(settings));
Type