use of org.eclipse.jetty.client.ProxyConfiguration.Proxy in project smarthome by eclipse.
the class HttpUtil method executeUrlAndGetReponse.
/**
* Executes the given <code>url</code> with the given <code>httpMethod</code>
*
* @param httpMethod the HTTP method to use
* @param url the url to execute
* @param httpHeaders optional HTTP headers which has to be set on request
* @param content the content to be send to the given <code>url</code> or <code>null</code> if no content should be
* send.
* @param contentType the content type of the given <code>content</code>
* @param timeout the socket timeout in milliseconds to wait for data
* @param proxyHost the hostname of the proxy
* @param proxyPort the port of the proxy
* @param proxyUser the username to authenticate with the proxy
* @param proxyPassword the password to authenticate with the proxy
* @param nonProxyHosts the hosts that won't be routed through the proxy
* @return the response as a ContentResponse object or <code>NULL</code> when the request went wrong
* @throws IOException when the request execution failed, timed out or it was interrupted
*/
private static ContentResponse executeUrlAndGetReponse(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword, String nonProxyHosts) throws IOException {
startHttpClient(CLIENT);
HttpProxy proxy = null;
// only configure a proxy if a host is provided
if (StringUtils.isNotBlank(proxyHost) && proxyPort != null && shouldUseProxy(url, nonProxyHosts)) {
AuthenticationStore authStore = CLIENT.getAuthenticationStore();
ProxyConfiguration proxyConfig = CLIENT.getProxyConfiguration();
List<Proxy> proxies = proxyConfig.getProxies();
proxy = new HttpProxy(proxyHost, proxyPort);
proxies.add(proxy);
authStore.addAuthentication(new BasicAuthentication(proxy.getURI(), Authentication.ANY_REALM, proxyUser, proxyPassword));
}
HttpMethod method = HttpUtil.createHttpMethod(httpMethod);
Request request = CLIENT.newRequest(url).method(method).timeout(timeout, TimeUnit.MILLISECONDS);
if (httpHeaders != null) {
for (String httpHeaderKey : httpHeaders.stringPropertyNames()) {
request.header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey));
}
}
// add basic auth header, if url contains user info
try {
URI uri = new URI(url);
if (uri.getUserInfo() != null) {
String[] userInfo = uri.getUserInfo().split(":");
String user = userInfo[0];
String password = userInfo[1];
String basicAuthentication = "Basic " + B64Code.encode(user + ":" + password, StringUtil.__ISO_8859_1);
request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
}
} catch (URISyntaxException e) {
logger.debug("String {} can not be parsed as URI reference", url);
}
// add content if a valid method is given ...
if (content != null && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT))) {
request.content(new InputStreamContentProvider(content), contentType);
}
if (logger.isDebugEnabled()) {
logger.debug("About to execute {}", request.getURI());
}
try {
ContentResponse response = request.send();
int statusCode = response.getStatus();
if (statusCode >= HttpStatus.BAD_REQUEST_400) {
String statusLine = statusCode + " " + response.getReason();
logger.debug("Method failed: {}", statusLine);
}
return response;
} catch (Exception e) {
throw new IOException(e);
} finally {
if (proxy != null) {
// Remove the proxy, that has been added for this request
CLIENT.getProxyConfiguration().getProxies().remove(proxy);
}
}
}
Aggregations