Search in sources :

Example 1 with Proxy

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);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) URISyntaxException(java.net.URISyntaxException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpProxy(org.eclipse.jetty.client.HttpProxy) Proxy(org.eclipse.jetty.client.ProxyConfiguration.Proxy) ProxyConfiguration(org.eclipse.jetty.client.ProxyConfiguration) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) HttpMethod(org.eclipse.jetty.http.HttpMethod) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Aggregations

IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpProxy (org.eclipse.jetty.client.HttpProxy)1 ProxyConfiguration (org.eclipse.jetty.client.ProxyConfiguration)1 Proxy (org.eclipse.jetty.client.ProxyConfiguration.Proxy)1 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Request (org.eclipse.jetty.client.api.Request)1 BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)1 InputStreamContentProvider (org.eclipse.jetty.client.util.InputStreamContentProvider)1 HttpMethod (org.eclipse.jetty.http.HttpMethod)1