Search in sources :

Example 31 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project indy by Commonjava.

the class ReplicationController method newGet.

private HttpGet newGet(final String url, final ReplicationDTO dto) {
    final HttpGet get = new HttpGet(url);
    final int proxyPort = dto.getProxyPort();
    HttpHost proxy;
    if (proxyPort < 1) {
        proxy = new HttpHost(dto.getProxyHost(), -1, "http");
    } else {
        proxy = new HttpHost(dto.getProxyHost(), dto.getProxyPort(), "http");
    }
    final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    get.setConfig(config);
    return get;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet)

Example 32 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project stanbol by apache.

the class RequestExecutor method execute.

/**
     * Executes a {@link Request} using this executor. <p>
     * Note that this cleans up all data of the previous executed request.
     * @param r the request to execute
     * @return this
     * @throws ClientProtocolException
     * @throws IOException
     */
public RequestExecutor execute(Request r) throws ClientProtocolException, IOException {
    clear();
    request = r.getRequest();
    RequestConfig rc = RequestConfig.custom().setRedirectsEnabled(r.getRedirects()).setRelativeRedirectsAllowed(true).build();
    request.setConfig(rc);
    // Execute request
    response = httpClient.execute(request);
    entity = response.getEntity();
    if (entity != null) {
        // We fully read the content every time, not super efficient but
        // how can we read it on demand while avoiding a (boring) cleanup() 
        // method on this class?
        content = EntityUtils.toByteArray(entity);
        contentType = ContentType.getOrDefault(entity);
        charset = contentType.getCharset();
        contentString = new String(content, charset != null ? charset : HTTP.DEF_CONTENT_CHARSET);
        //and close the stream
        entity.getContent().close();
    }
    return this;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig)

Example 33 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project stanbol by apache.

the class MultiThreadedTestBase method initialiseHttpClient.

@Before
public void initialiseHttpClient() {
    if (this.pooledHttpClient == null) {
        //init for the first test
        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(3).build();
        SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).build();
        connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultSocketConfig(socketConfig);
        connectionManager.setMaxTotal(20);
        connectionManager.setDefaultMaxPerRoute(20);
        pooledHttpClient = HttpClientBuilder.create().setUserAgent("Stanbol Integration Test").setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Before(org.junit.Before)

Example 34 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project opennms by OpenNMS.

the class GrafanaDashletConfigurationWindow method getGrafanaDashboards.

private Map<String, String> getGrafanaDashboards() throws GrafanaDashletException {
    /**
     * Loading the required properties...
     */
    final String grafanaApiKey = System.getProperty("org.opennms.grafanaBox.apiKey", "");
    final String grafanaProtocol = System.getProperty("org.opennms.grafanaBox.protocol", "http");
    final String grafanaHostname = System.getProperty("org.opennms.grafanaBox.hostname", "localhost");
    final int grafanaPort = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.port", "3000"));
    final int grafanaConnectionTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.connectionTimeout", "500"));
    final int grafanaSoTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.soTimeout", "500"));
    if (!"".equals(grafanaApiKey) && !"".equals(grafanaHostname) && !"".equals(grafanaProtocol) && ("http".equals(grafanaProtocol) || "https".equals(grafanaProtocol))) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(grafanaConnectionTimeout).setSocketTimeout(grafanaSoTimeout).build();
            final URI uri = new URIBuilder().setScheme(grafanaProtocol).setHost(grafanaHostname).setPort(grafanaPort).setPath("/api/search/").build();
            final HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            /**
             * Adding the API key...
             */
            httpGet.setHeader("Authorization", "Bearer " + grafanaApiKey);
            final Map<String, String> resultSet = new TreeMap<>();
            try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    /**
                     * Fill the result set...
                     */
                    final String responseString = IOUtils.toString(httpEntity.getContent(), StandardCharsets.UTF_8.name());
                    if (!Strings.isNullOrEmpty(responseString)) {
                        try {
                            final JSONArray arr = new JSONObject("{dashboards:" + responseString + "}").getJSONArray("dashboards");
                            for (int i = 0; i < arr.length(); i++) {
                                resultSet.put(arr.getJSONObject(i).getString("title"), arr.getJSONObject(i).getString("uri"));
                            }
                        } catch (JSONException e) {
                            throw new GrafanaDashletException(e.getMessage());
                        }
                    }
                }
            }
            return resultSet;
        } catch (Exception e) {
            throw new GrafanaDashletException(e.getMessage());
        }
    } else {
        throw new GrafanaDashletException("Invalid configuration");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TreeMap(java.util.TreeMap) URI(java.net.URI) JSONException(org.json.JSONException) URIBuilder(org.apache.http.client.utils.URIBuilder) JSONObject(org.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 35 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.

the class ProxyController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
    HttpGet method = new HttpGet(url);
    method.setConfig(requestConfig);
    InputStream in = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        try (CloseableHttpResponse resp = client.execute(method)) {
            int statusCode = resp.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                response.sendError(statusCode);
            } else {
                in = resp.getEntity().getContent();
                IOUtils.copy(in, response.getOutputStream());
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return null;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6