1 //
2 // ========================================================================
3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.client.api;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24 /**
25 * The configuration of the forward proxy to use with {@link org.eclipse.jetty.client.HttpClient}.
26 * <p />
27 * Configuration parameters include the host and port of the forward proxy, and a list of
28 * {@link #getExcludedOrigins() origins} that are excluded from being proxied.
29 *
30 * @see org.eclipse.jetty.client.HttpClient#setProxyConfiguration(ProxyConfiguration)
31 */
32 public class ProxyConfiguration
33 {
34 private final Set<String> excluded = new HashSet<>();
35 private final String host;
36 private final int port;
37
38 public ProxyConfiguration(String host, int port)
39 {
40 this.host = host;
41 this.port = port;
42 }
43
44 /**
45 * @return the host name of the forward proxy
46 */
47 public String getHost()
48 {
49 return host;
50 }
51
52 /**
53 * @return the port of the forward proxy
54 */
55 public int getPort()
56 {
57 return port;
58 }
59
60 /**
61 * Matches the given {@code host} and {@code port} with the list of excluded origins,
62 * returning true if the origin is to be proxied, false if it is excluded from proxying.
63 * @param host the host to match
64 * @param port the port to match
65 * @return true if the origin made of {@code host} and {@code port} is to be proxied,
66 * false if it is excluded from proxying.
67 */
68 public boolean matches(String host, int port)
69 {
70 String hostPort = host + ":" + port;
71 return !getExcludedOrigins().contains(hostPort);
72 }
73
74 /**
75 * @return the list of origins to exclude from proxying, in the form "host:port".
76 */
77 public Set<String> getExcludedOrigins()
78 {
79 return excluded;
80 }
81 }