hostPatternToProxyHost;
+
+ /**
+ * Creates a new {@link ProxyMapping} from the provided {@code List} of proxy mapping strings.
+ *
+ * A proxy mapping string must have the following format: {@code hostnameRegex;www-proxy-uri } with semicolon as a delimiter.
+ * This format enables easy configuration via SPI config string in standalone.xml.
+ *
+ * For example
+ * {@code ^.*.(google.com|googleapis.com)$;http://www-proxy.mycorp.local:8080}
+ *
+ *
+ * @param mappings
+ */
+ public ProxyMapping(List mappings) {
+ this(parseProxyMappings(mappings));
+ }
+
+ /**
+ * Creates a {@link ProxyMapping} from the provided mappings.
+ *
+ * @param mappings
+ */
+ public ProxyMapping(Map mappings) {
+ this.hostPatternToProxyHost = Collections.unmodifiableMap(mappings);
+ }
+
+ private static Map parseProxyMappings(List mapping) {
+
+ if (mapping == null || mapping.isEmpty()) {
+ return Collections.emptyMap();
+ }
+
+ // Preserve the order provided via mapping
+ Map map = new LinkedHashMap<>();
+
+ for (String entry : mapping) {
+ String[] hostPatternRegexWithProxyHost = entry.split(DELIMITER);
+ String hostPatternRegex = hostPatternRegexWithProxyHost[0];
+ String proxyUrl = hostPatternRegexWithProxyHost[1];
+
+ URI uri = URI.create(proxyUrl);
+ HttpHost proxy = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
+
+ Pattern hostPattern = Pattern.compile(hostPatternRegex);
+ map.put(hostPattern, proxy);
+ }
+
+ return map;
+ }
+
+ public boolean isEmpty() {
+ return this.hostPatternToProxyHost.isEmpty();
+ }
+
+ /**
+ * @param hostname
+ * @return the {@link HttpHost} proxy associated with the first matching hostname {@link Pattern} or {@literal null} if none matches.
+ */
+ public HttpHost getProxyFor(String hostname) {
+
+ Objects.requireNonNull(hostname, "hostname");
+
+ for (Map.Entry entry : hostPatternToProxyHost.entrySet()) {
+
+ Pattern hostnamePattern = entry.getKey();
+ HttpHost proxy = entry.getValue();
+
+ if (hostnamePattern.matcher(hostname).matches()) {
+ return proxy;
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/services/src/main/java/org/keycloak/connections/httpclient/ProxyMappingAwareRoutePlanner.java b/services/src/main/java/org/keycloak/connections/httpclient/ProxyMappingAwareRoutePlanner.java
new file mode 100644
index 0000000000..7ce1c5afa1
--- /dev/null
+++ b/services/src/main/java/org/keycloak/connections/httpclient/ProxyMappingAwareRoutePlanner.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * 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 org.keycloak.connections.httpclient;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.impl.conn.DefaultRoutePlanner;
+import org.apache.http.impl.conn.DefaultSchemePortResolver;
+import org.apache.http.protocol.HttpContext;
+import org.jboss.logging.Logger;
+
+/**
+ * A {@link DefaultRoutePlanner} that determines the proxy to use for a given target hostname by consulting a {@link ProxyMapping}.
+ *
+ * @author Thomas Darimont
+ */
+public class ProxyMappingAwareRoutePlanner extends DefaultRoutePlanner {
+
+ private static final Logger LOG = Logger.getLogger(ProxyMappingAwareRoutePlanner.class);
+
+ private final ProxyMapping proxyMapping;
+
+ public ProxyMappingAwareRoutePlanner(ProxyMapping proxyMapping) {
+ super(DefaultSchemePortResolver.INSTANCE);
+ this.proxyMapping = proxyMapping;
+ }
+
+ @Override
+ protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
+
+ HttpHost proxy = proxyMapping.getProxyFor(target.getHostName());
+
+ LOG.debugf("Returning proxy=%s for targetHost=%s", proxy ,target.getHostName());
+
+ return proxy;
+ }
+}
diff --git a/services/src/test/java/org/keycloak/connections/httpclient/ProxyMappingTest.java b/services/src/test/java/org/keycloak/connections/httpclient/ProxyMappingTest.java
new file mode 100644
index 0000000000..47d8ac1d3e
--- /dev/null
+++ b/services/src/test/java/org/keycloak/connections/httpclient/ProxyMappingTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2017 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * 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 org.keycloak.connections.httpclient;
+
+import org.apache.http.HttpHost;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * @author Thomas Darimont
+ */
+public class ProxyMappingTest {
+
+ private static final List DEFAULT_MAPPINGS = Arrays.asList( //
+ "^.*.(google.com|googleapis.com)$;http://proxy1:8080", //
+ "^.*.(facebook.com)$;http://proxy2:8080" //
+ );
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ ProxyMapping proxyMapping;
+
+ @Before
+ public void setup() {
+ proxyMapping = new ProxyMapping(DEFAULT_MAPPINGS);
+ }
+
+ @Test
+ public void proxyMappingFromEmptyMapShouldBeEmpty() {
+ assertThat(new ProxyMapping(Collections.emptyMap()).isEmpty(), is(true));
+ }
+
+ @Test
+ public void proxyMappingFromEmptyListShouldBeEmpty() {
+ assertThat(new ProxyMapping(new ArrayList<>()).isEmpty(), is(true));
+ }
+
+ @Test
+ public void shouldReturnProxy1ForConfiguredProxyMapping() {
+
+ HttpHost proxy = proxyMapping.getProxyFor("account.google.com");
+ assertThat(proxy, is(notNullValue()));
+ assertThat(proxy.getHostName(), is("proxy1"));
+ }
+
+ @Test
+ public void shouldReturnProxy1ForConfiguredProxyMappingWithSubDomain() {
+
+ HttpHost proxy = proxyMapping.getProxyFor("awesome.account.google.com");
+ assertThat(proxy, is(notNullValue()));
+ assertThat(proxy.getHostName(), is("proxy1"));
+ }
+
+ @Test
+ public void shouldReturnProxy2ForConfiguredProxyMapping() {
+
+ HttpHost proxy = proxyMapping.getProxyFor("login.facebook.com");
+ assertThat(proxy, is(notNullValue()));
+ assertThat(proxy.getHostName(), is("proxy2"));
+ }
+
+ @Test
+ public void shouldReturnNoProxyForUnknownHost() {
+
+ HttpHost proxy = proxyMapping.getProxyFor("login.microsoft.com");
+ assertThat(proxy, is(nullValue()));
+ }
+
+ @Test
+ public void shouldRejectNull() {
+
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("hostname");
+
+ proxyMapping.getProxyFor(null);
+ }
+}
\ No newline at end of file