src/main/java/com/github/markusbernhardt/proxy/selector/fixed/FixedProxySelector.java
raw symbols SCIP| 1 | package com.github.markusbernhardt.proxy.selector.fixed; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.InetSocketAddress; |
| 5 | import java.net.Proxy; |
| 6 | import java.net.ProxySelector; |
| 7 | import java.net.SocketAddress; |
| 8 | import java.net.URI; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.Collections; |
| 11 | import java.util.List; |
| 12 | |
| 13 | /***************************************************************************** |
| 14 | * This proxy selector is configured with a fixed proxy. This proxy will be |
| 15 | * returned for all URIs passed to the select method. |
| 16 | * |
| 17 | * @author Markus Bernhardt, Copyright 2016 |
| 18 | * @author Bernd Rosstauscher, Copyright 2009 |
| 19 | ****************************************************************************/ |
| 20 | |
| 21 | public class FixedProxySelector extends ProxySelector { |
| 22 | |
| 23 | private final List<Proxy> proxyList; |
| 24 | |
| 25 | /************************************************************************* |
| 26 | * Constructor |
| 27 | * |
| 28 | * @param proxy |
| 29 | * the proxy to use. |
| 30 | ************************************************************************/ |
| 31 | |
| 32 | public FixedProxySelector(Proxy proxy) { |
| 33 | super(); |
| 34 | |
| 35 | List<Proxy> list = new ArrayList<Proxy>(1); |
| 36 | list.add(proxy); |
| 37 | this.proxyList = Collections.unmodifiableList(list); |
| 38 | } |
| 39 | |
| 40 | /************************************************************************* |
| 41 | * Constructor |
| 42 | * |
| 43 | * @param proxyHost |
| 44 | * the host name or IP address of the proxy to use. |
| 45 | * @param proxyPort |
| 46 | * the port of the proxy. |
| 47 | ************************************************************************/ |
| 48 | |
| 49 | public FixedProxySelector(String proxyHost, int proxyPort) { |
| 50 | this(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(proxyHost, proxyPort))); |
| 51 | } |
| 52 | |
| 53 | /************************************************************************* |
| 54 | * connectFailed |
| 55 | * |
| 56 | * @see java.net.ProxySelector#connectFailed(java.net.URI, |
| 57 | * java.net.SocketAddress, java.io.IOException) |
| 58 | ************************************************************************/ |
| 59 | |
| 60 | @Override |
| 61 | public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 62 | // Not used |
| 63 | } |
| 64 | |
| 65 | /************************************************************************* |
| 66 | * select |
| 67 | * |
| 68 | * @see java.net.ProxySelector#select(java.net.URI) |
| 69 | ************************************************************************/ |
| 70 | |
| 71 | @Override |
| 72 | public List<Proxy> select(URI uri) { |
| 73 | return this.proxyList; |
| 74 | } |
| 75 | |
| 76 | } |