001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2015 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.connector;
015
016import java.net.URISyntaxException;
017import java.net.URL;
018import java.net.http.HttpClient;
019import java.net.http.HttpRequest;
020import java.util.Properties;
021
022import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
023import org.slf4j.LoggerFactory;
024
025/**
026 * A generic HTTP connector. It creates {@link HttpRequest.Builder} that can be
027 * individually customized according to the user's needs.
028 *
029 * @since 3.0.0
030 */
031public class HttpConnector {
032    private static final String USER_AGENT;
033
034    private final NetworkSettings networkSettings;
035    private final HttpClient httpClient;
036
037    static {
038        var agent = new StringBuilder("acme4j");
039
040        try (var in = HttpConnector.class.getResourceAsStream("/org/shredzone/acme4j/version.properties")) {
041            var prop = new Properties();
042            prop.load(in);
043            agent.append('/').append(prop.getProperty("version"));
044        } catch (Exception ex) {
045            // Ignore, just don't use a version
046            LoggerFactory.getLogger(HttpConnector.class).warn("Could not read library version", ex);
047        }
048
049        agent.append(" Java/").append(System.getProperty("java.version"));
050        USER_AGENT = agent.toString();
051    }
052
053    /**
054     * Returns the default User-Agent to be used.
055     *
056     * @return User-Agent
057     */
058    public static String defaultUserAgent() {
059        return USER_AGENT;
060    }
061
062    /**
063     * Creates a new {@link HttpConnector} that is using the given
064     * {@link NetworkSettings} and {@link HttpClient}.
065     *
066     * @param networkSettings Network settings to use
067     * @param httpClient HTTP client to use for requests
068     * @since 4.0.0
069     */
070    @SuppressFBWarnings("EI_EXPOSE_REP2")   // behavior is intended
071    public HttpConnector(NetworkSettings networkSettings, HttpClient httpClient) {
072        this.networkSettings = networkSettings;
073        this.httpClient = httpClient;
074    }
075
076    /**
077     * Creates a new {@link HttpRequest.Builder} that is preconfigured and bound to the
078     * given URL. Subclasses can override this method to extend the configuration, or
079     * create a different builder.
080     *
081     * @param url
082     *            {@link URL} to connect to
083     * @return {@link HttpRequest.Builder} connected to the {@link URL}
084     */
085    public HttpRequest.Builder createRequestBuilder(URL url) {
086        try {
087            return HttpRequest.newBuilder(url.toURI())
088                    .header("User-Agent", USER_AGENT)
089                    .timeout(networkSettings.getTimeout());
090        } catch (URISyntaxException ex) {
091            throw new IllegalArgumentException("Invalid URL", ex);
092        }
093    }
094
095    /**
096     * Returns the {@link HttpClient} instance used by this connector.
097     *
098     * @return {@link HttpClient} instance
099     * @since 4.0.0
100     */
101    public HttpClient getHttpClient() {
102        return httpClient;
103    }
104
105}