001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2019 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.Proxy;
017import java.time.Duration;
018
019import edu.umd.cs.findbugs.annotations.Nullable;
020
021/**
022 * Contains network settings to be used for network connections.
023 *
024 * @since 2.8
025 */
026public class NetworkSettings {
027
028    private Proxy proxy = Proxy.NO_PROXY;
029    private Duration timeout = Duration.ofSeconds(10);
030
031    /**
032     * Gets the {@link Proxy} to be used for connections.
033     */
034    public Proxy getProxy() {
035        return proxy;
036    }
037
038    /**
039     * Sets a {@link Proxy} that is to be used for all connections. If {@code null},
040     * {@link Proxy#NO_PROXY} is used, which is also the default.
041     */
042    public void setProxy(@Nullable Proxy proxy) {
043        this.proxy = proxy != null ? proxy : Proxy.NO_PROXY;
044    }
045
046    /**
047     * Gets the current network timeout.
048     */
049    public Duration getTimeout() {
050        return timeout;
051    }
052
053    /**
054     * Sets the network timeout to be used for connections. Defaults to 10 seconds.
055     *
056     * @param timeout
057     *         Network timeout {@link Duration}
058     */
059    public void setTimeout(Duration timeout) {
060        if (timeout == null || timeout.isNegative() || timeout.isZero()) {
061            throw new IllegalArgumentException("Timeout must be positive");
062        }
063        if (timeout.toMillis() > Integer.MAX_VALUE) {
064            throw new IllegalArgumentException("Timeout is out of range");
065        }
066
067        this.timeout = timeout;
068    }
069
070}