JsignProxySelector.java
/*
* Copyright 2026 Emmanuel Bourg and contributors
*
* 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 net.jsign;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import static java.net.Proxy.Type.*;
/**
* Proxy selector for Jsign.
*
* @author Emmanuel Bourg
* @since 8.0
*/
class JsignProxySelector extends ProxySelector {
private final Logger log = Logger.getLogger(getClass().getName());
/** The address of the proxy server */
private final InetSocketAddress address;
/** The patterns for the non-proxy hosts */
private final List<Pattern> nonProxyHostsPatterns = new ArrayList<>();
/**
* Creates a new proxy selector for the specified proxy settings.
*
* @param settings the proxy settings
*/
public JsignProxySelector(ProxySettings settings) {
if (!settings.url.trim().startsWith("http")) {
settings.url = "http://" + settings.url.trim();
}
URI uri = URI.create(settings.url);
int port = uri.getPort() < 0 ? 80 : uri.getPort();
address = new InetSocketAddress(uri.getHost(), port);
if (settings.nonProxyHosts != null) {
for (String pattern : settings.nonProxyHosts.split("\\|")) {
nonProxyHostsPatterns.add(Pattern.compile(pattern.replace(".", "\\.").replace("*", ".*"), Pattern.CASE_INSENSITIVE));
}
}
}
@Override
public List<Proxy> select(URI uri) {
Proxy proxy;
if (isNonProxyHost(uri.getHost())) {
proxy = Proxy.NO_PROXY;
} else {
proxy = new Proxy(uri.getScheme().equals("socket") ? SOCKS : HTTP, address);
}
log.fine("Proxy selected for " + uri + " : " + proxy);
return Collections.singletonList(proxy);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
public boolean isNonProxyHost(String host) {
for (Pattern pattern : nonProxyHostsPatterns) {
if (pattern.matcher(host).matches()) {
return true;
}
}
return false;
}
}