Skip to content

Commit b88a576

Browse files
Ed HillmannEd Hillmann
authored andcommitted
Support connecting through an HTTP Proxy
1 parent 00b2f0f commit b88a576

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

core/azure-core/src/main/java/com/microsoft/windowsazure/Configuration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public class Configuration {
4545
*/
4646
public static final String PROPERTY_LOG_HTTP_REQUESTS = "com.microsoft.windowsazure.Configuration.logHttpRequests";
4747

48+
/**
49+
* Property name to control whether the Rest client uses a Proxy connection to the remote resources. Defines the host
50+
* of the proxy to use
51+
*/
52+
public static final String PROPERTY_HTTP_PROXY_HOST = "http.proxyHost";
53+
54+
/**
55+
* Property name to control whether the Rest client uses a Proxy connection to the remote resources. Defines the port
56+
* of the proxy to use
57+
*/
58+
public static final String PROPERTY_HTTP_PROXY_PORT = "http.proxyPort";
59+
4860
/**
4961
* The configuration instance.
5062
*/

core/azure-core/src/main/java/com/microsoft/windowsazure/core/pipeline/jersey/Exports.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525

2626
import static com.microsoft.windowsazure.core.utils.ExportUtils.getPropertyIfExists;
27+
import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
2728

2829
public class Exports implements Builder.Exports {
2930

@@ -69,7 +70,18 @@ public <S> Client create(String profile, Class<S> service,
6970
ClientConfig.class, properties);
7071
ClientConfigSettings settings = builder.build(profile, service,
7172
ClientConfigSettings.class, properties);
72-
Client client = Client.create(clientConfig);
73+
Client client;
74+
String proxyHost = (String) getPropertyIfExists(profile,
75+
properties, Configuration.PROPERTY_HTTP_PROXY_HOST);
76+
Integer proxyPort = (Integer) getPropertyIfExists(profile,
77+
properties, Configuration.PROPERTY_HTTP_PROXY_PORT);
78+
if ((proxyHost != null) && (proxyPort != null)) {
79+
URLConnectionClientHandler clientHandler = new URLConnectionClientHandler(
80+
new ProxyConnectionFactory(proxyHost, proxyPort));
81+
client = new Client(clientHandler, clientConfig);
82+
} else {
83+
client = Client.create(clientConfig);
84+
}
7385
settings.applyConfig(client);
7486
return client;
7587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.microsoft.windowsazure.core.pipeline.jersey;
15+
16+
import java.io.IOException;
17+
import java.net.HttpURLConnection;
18+
import java.net.InetSocketAddress;
19+
import java.net.Proxy;
20+
import java.net.URL;
21+
22+
import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
23+
24+
/**
25+
* Extends <code>HttpURLConnectionFactory</code> so that an HTTP Proxy can be used when connecting to the
26+
* remote resource.
27+
*
28+
* @author Ed Hillmann
29+
*/
30+
public class ProxyConnectionFactory implements HttpURLConnectionFactory {
31+
32+
private final String proxyHost;
33+
private final int proxyPort;
34+
35+
public ProxyConnectionFactory(String proxyHost, int proxyPort) {
36+
this.proxyHost = proxyHost;
37+
this.proxyPort = proxyPort;
38+
}
39+
40+
@Override
41+
public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
42+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
43+
return (HttpURLConnection) url.openConnection(proxy);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)