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-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* 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.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import com.landawn.abacus.exception.AbacusException;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.BufferedReader;
import com.landawn.abacus.util.BufferedWriter;
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.
*
* @since 0.8
*
* @author Haiyang Li
*/
public final class HttpClient extends AbstractHttpClient {
static final Logger logger = LoggerFactory.getLogger(HttpClient.class);
static {
if (IOUtil.IS_PLATFORM_ANDROID) {
// ignore
} else {
final int maxConnections = IOUtil.CPU_CORES * 8;
System.setProperty("http.keepAlive", "true");
System.setProperty("http.maxConnections", String.valueOf(maxConnections));
}
}
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 connTimeout, long readTimeout) {
this(url, maxConnection, connTimeout, readTimeout, null);
}
protected HttpClient(String url, int maxConnection, long connTimeout, long readTimeout, HttpSettings settings) throws UncheckedIOException {
this(url, maxConnection, connTimeout, readTimeout, settings, new AtomicInteger(0));
}
protected HttpClient(String url, int maxConnection, long connTimeout, long readTimeout, HttpSettings settings,
final AtomicInteger sharedActiveConnectionCounter) {
super(url, maxConnection, connTimeout, readTimeout, settings);
try {
this._netURL = new URL(url);
} catch (MalformedURLException e) {
throw N.toRuntimeException(e);
}
this._activeConnectionCounter = sharedActiveConnectionCounter;
}
public static HttpClient create(String url) {
return new HttpClient(url);
}
public static HttpClient create(String url, int maxConnection) {
return new HttpClient(url, maxConnection);
}
public static HttpClient create(String url, long connTimeout, long readTimeout) {
return new HttpClient(url, DEFAULT_MAX_CONNECTION, connTimeout, readTimeout);
}
public static HttpClient create(String url, int maxConnection, long connTimeout, long readTimeout) {
return new HttpClient(url, maxConnection, connTimeout, readTimeout);
}
public static HttpClient create(String url, int maxConnection, long connTimeout, long readTimeout, HttpSettings settings) throws UncheckedIOException {
return new HttpClient(url, maxConnection, connTimeout, readTimeout, settings);
}
public static HttpClient create(String url, int maxConnection, long connTimeout, long readTimeout, HttpSettings settings,
final AtomicInteger sharedActiveConnectionCounter) {
return new HttpClient(url, maxConnection, connTimeout, readTimeout, settings, sharedActiveConnectionCounter);
}
@Override
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);
}
@Override
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);
}
}
@Override
public void execute(final OutputStream output, final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
execute(null, output, null, httpMethod, request, settings);
}
@Override
public void execute(final Writer output, final HttpMethod httpMethod, final Object request, final HttpSettings settings) throws UncheckedIOException {
execute(null, null, output, httpMethod, request, settings);
}
private T execute(final Class resultClass, final OutputStream outputStream, final Writer outputWriter, final HttpMethod httpMethod,
final Object request, final HttpSettings settings) throws UncheckedIOException {
final ContentFormat requestContentFormat = getContentFormat(settings);
final HttpURLConnection connection = openConnection(httpMethod, request, request != null, settings);
final Charset requestCharset = HTTP.getCharset(settings == null || settings.headers().isEmpty() ? _settings.headers() : settings.headers());
final long sentRequestAtMillis = System.currentTimeMillis();
InputStream is = null;
OutputStream os = null;
try {
if (request != null && (httpMethod.equals(HttpMethod.POST) || httpMethod.equals(HttpMethod.PUT))) {
os = HTTP.getOutputStream(connection, requestContentFormat, getContentType(settings), getContentEncoding(settings));
Type