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

com.uid2.shared.util.URLConnectionHttpClient Maven / Gradle / Ivy

package com.uid2.shared.util;

import com.uid2.shared.Utils;

import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
import java.util.Optional;

public class URLConnectionHttpClient {
    private static class URLConnectionHttpResponse implements HttpResponse {
        private final T body;
        private final int statusCode;

        public URLConnectionHttpResponse(int statusCode, T body) {
            this.statusCode = statusCode;
            this.body = body;
        }

        @Override
        public int statusCode() {
            return statusCode;
        }

        @Override
        public HttpRequest request() {
            return null;
        }

        @Override
        public Optional> previousResponse() {
            return Optional.empty();
        }

        @Override
        public HttpHeaders headers() {
            return null;
        }

        @Override
        public T body() {
            return body;
        }

        @Override
        public Optional sslSession() {
            return Optional.empty();
        }

        @Override
        public URI uri() {
            return null;
        }

        @Override
        public HttpClient.Version version() {
            return null;
        }
    }

    private final Proxy proxy;

    public URLConnectionHttpClient(Proxy proxy) {
        this.proxy = proxy;
    }

    public HttpResponse get(String url, Map headers) throws IOException {
        HttpURLConnection connection = openConnection(url);
        connection.setRequestMethod("GET");
        if (headers != null) {
            for (Map.Entry header : headers.entrySet()) {
                connection.setRequestProperty(header.getKey(), header.getValue());
            }
        }

        int responseCode = connection.getResponseCode();
        String responseBody = responseCode == 200 ? Utils.readToEnd(connection.getInputStream()) : Utils.readToEnd(connection.getErrorStream());

        return new URLConnectionHttpResponse<>(responseCode, responseBody);
    }

    public HttpResponse post(String url, String body, Map headers) throws IOException {
        HttpURLConnection connection = openConnection(url);
        connection.setRequestMethod("POST");
        if (headers != null) {
            for (Map.Entry header : headers.entrySet()) {
                connection.setRequestProperty(header.getKey(), header.getValue());
            }
        }

        connection.setDoOutput(true);
        connection.getOutputStream().write(body.getBytes());

        int responseCode = connection.getResponseCode();
        String responseBody = responseCode == 200 ? Utils.readToEnd(connection.getInputStream()) : Utils.readToEnd(connection.getErrorStream());

        return new URLConnectionHttpResponse<>(responseCode, responseBody);
    }

    private HttpURLConnection openConnection(String url) throws IOException {
        if (proxy == null) {
            return (HttpURLConnection) new URL(url).openConnection();
        } else {
            return (HttpURLConnection) new URL(url).openConnection(proxy);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy