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

com.bimface.sdk.http.HttpRequestClient Maven / Gradle / Ivy

The newest version!
package com.bimface.sdk.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;
import com.bimface.sdk.config.ClientConfiguration;
import com.bimface.sdk.config.DefaultConfig;
import com.bimface.sdk.exception.BimfaceException;
import com.bimface.sdk.utils.StringUtils;
import com.bimface.sdk.utils.VersionInfoUtils;
import com.squareup.okhttp.ConnectionPool;
import com.squareup.okhttp.Dispatcher;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import okio.BufferedSink;

/**
 * http请求的客户端
 * 
 * @author by bimface on 2016-06-23.
 */
public final class HttpRequestClient implements HttpClient {

    private OkHttpClient httpClient;

    public HttpRequestClient(ClientConfiguration config) {

        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(config.getMaxRequests());
        dispatcher.setMaxRequestsPerHost(config.getMaxRequestsPerHost());
        ConnectionPool connectionPool = new ConnectionPool(config.getMaxIdleConnections(),
                                                           config.getKeepAliveDurationNs());
        httpClient = new OkHttpClient();
        httpClient.setDispatcher(dispatcher);
        httpClient.setConnectionPool(connectionPool);
        httpClient.networkInterceptors().add(new Interceptor() {

            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();

                com.squareup.okhttp.Response response = chain.proceed(request);
                // IpTag tag = (IpTag) request.tag();
                // String ip = chain.connection().getSocket().getRemoteSocketAddress().toString();
                // tag.ip = ip;
                return response;
            }
        });
        httpClient.setConnectTimeout(config.getConnectTimeout(), TimeUnit.SECONDS);
        httpClient.setReadTimeout(config.getReadTimeout(), TimeUnit.SECONDS);
        httpClient.setWriteTimeout(config.getWriteTimeout(), TimeUnit.SECONDS);

    }

    public Response get(String url) throws BimfaceException {
        return get(url, new HttpHeaders());
    }

    public Response get(String url, HttpHeaders headers) throws BimfaceException {
        Request.Builder requestBuilder = new Request.Builder().get().url(url);
        return send(requestBuilder, Headers.of(headers.getHeaders()));
    }

    public Response post(String url, byte[] body, HttpHeaders headers) throws BimfaceException {
        return post(url, body, headers, DefaultConfig.STREAM_MIME);
    }

    public Response post(String url, String body, HttpHeaders headers) throws BimfaceException {
        return post(url, StringUtils.utf8Bytes(body), headers, DefaultConfig.STREAM_MIME);
    }

    public Response post(String url, HttpFormEncoding params, HttpHeaders headers) throws BimfaceException {
        final FormEncodingBuilder f = new FormEncodingBuilder();
        for (String key : params.getParams().keySet()) {
            f.add(key, params.getParams().get(key));
        }
        return post(url, f.build(), headers);
    }

    public Response post(String url, byte[] body, HttpHeaders headers, String contentType) throws BimfaceException {
        RequestBody rbody;
        if (body != null && body.length > 0) {
            MediaType t = MediaType.parse(contentType);
            rbody = RequestBody.create(t, body);
        } else {
            rbody = RequestBody.create(null, new byte[0]);
        }
        return post(url, rbody, headers);
    }

    public Response post(String url, byte[] body, int offset, int size, HttpHeaders headers,
                         String contentType) throws BimfaceException {
        RequestBody rbody;
        if (body != null && body.length > 0) {
            MediaType t = MediaType.parse(contentType);
            rbody = requestIO(t, body, offset, size);
        } else {
            rbody = RequestBody.create(null, new byte[0]);
        }
        return post(url, rbody, headers);
    }

    public Response post(String url, InputStream inputStream, Long contentLength,
                         HttpHeaders headers) throws BimfaceException {
        return post(url, requestIO(inputStream, contentLength), headers);
    }

    private Response post(String url, RequestBody body, HttpHeaders headers) throws BimfaceException {
        Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
        return send(requestBuilder, Headers.of(headers.getHeaders()));
    }

    public Response put(String url, HttpHeaders headers) throws BimfaceException {
        return put(url, emptyRequestBody, headers);
    }

    public Response put(String url, Object body, HttpHeaders headers) throws BimfaceException {
        return put(url, JSONObject.toJSONString(body), headers);
    }

    public Response put(String url, String body, HttpHeaders headers) throws BimfaceException {
        return put(url, RequestBody.create(DefaultConfig.MEDIA_TYPE_JSON, body), headers);
    }

    public Response put(String url, InputStream inputStream, Long contentLength,
                        HttpHeaders headers) throws BimfaceException {
        return put(url, requestIO(inputStream, contentLength), headers);

    }

    private Response put(String url, RequestBody body, HttpHeaders headers) throws BimfaceException {
        Request.Builder requestBuilder = new Request.Builder().url(url).put(body);
        return send(requestBuilder, Headers.of(headers.getHeaders()));
    }

    public Response delete(String url) throws BimfaceException {
        return delete(url, new HttpHeaders());
    }

    public Response delete(String url, HttpHeaders headers) throws BimfaceException {
        Request.Builder requestBuilder = new Request.Builder().url(url).delete();
        return send(requestBuilder, Headers.of(headers.getHeaders()));
    }

    public Response send(final Request.Builder requestBuilder, Headers headers) throws BimfaceException {
        if (headers != null) {
            requestBuilder.headers(headers);
        }

        try {
            requestBuilder.header("User-Agent", VersionInfoUtils.getDefaultUserAgent());
            Response response = httpClient.newCall(requestBuilder.build()).execute();
            if (response.code() >= 300) {
                throw new BimfaceException(response.message());
            }
            return response;
        } catch (IOException e) {
            e.printStackTrace();
            throw new BimfaceException(e);
        }
    }

    private static RequestBody requestIO(final MediaType contentType, final byte[] content, final int offset,
                                         final int size) {
        if (content == null) throw new NullPointerException("content == null");

        return new RequestBody() {

            @Override
            public MediaType contentType() {
                return contentType;
            }

            @Override
            public long contentLength() {
                return size;
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                sink.write(content, offset, size);
            }
        };
    }

    private static RequestBody requestIO(final InputStream inputStream, final Long contentLength) {
        if (inputStream == null) throw new NullPointerException("content == null");

        return new RequestBody() {

            @Override
            public MediaType contentType() {
                return MediaType.parse(DefaultConfig.STREAM_MIME);
            }

            @Override
            public long contentLength() {
                return contentLength;
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                try {
                    final byte[] buffer = new byte[DefaultConfig.PUT_THRESHOLD];
                    int l;
                    if (contentLength < DefaultConfig.PUT_THRESHOLD) {
                        // consume until EOF
                        while ((l = inputStream.read(buffer)) != -1) {
                            sink.outputStream().write(buffer, 0, l);
                        }
                    } else {
                        // consume no more than length
                        long remaining = contentLength;
                        while (remaining > 0) {
                            l = inputStream.read(buffer, 0, (int) Math.min(DefaultConfig.PUT_THRESHOLD, remaining));
                            if (l == -1) {
                                break;
                            }
                            sink.outputStream().write(buffer, 0, l);
                            remaining -= l;
                        }
                    }
                } finally {
                    sink.close();
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy