Search in sources :

Example 16 with RequestConfig

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

the class PodcastService method doRefreshChannel.

@SuppressWarnings({ "unchecked" })
private void doRefreshChannel(PodcastChannel channel, boolean downloadEpisodes) {
    InputStream in = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        channel.setStatus(PodcastStatus.DOWNLOADING);
        channel.setErrorMessage(null);
        podcastDao.updateChannel(channel);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 2 minutes
        2 * 60 * 1000).setSocketTimeout(// 10 minutes
        10 * 60 * 1000).build();
        HttpGet method = new HttpGet(channel.getUrl());
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            in = response.getEntity().getContent();
            Document document = new SAXBuilder().build(in);
            Element channelElement = document.getRootElement().getChild("channel");
            channel.setTitle(StringUtil.removeMarkup(channelElement.getChildTextTrim("title")));
            channel.setDescription(StringUtil.removeMarkup(channelElement.getChildTextTrim("description")));
            channel.setImageUrl(getChannelImageUrl(channelElement));
            channel.setStatus(PodcastStatus.COMPLETED);
            channel.setErrorMessage(null);
            podcastDao.updateChannel(channel);
            downloadImage(channel);
            refreshEpisodes(channel, channelElement.getChildren("item"));
        }
    } catch (Exception x) {
        LOG.warn("Failed to get/parse RSS file for Podcast channel " + channel.getUrl(), x);
        channel.setStatus(PodcastStatus.ERROR);
        channel.setErrorMessage(getErrorMessage(x));
        podcastDao.updateChannel(channel);
    } finally {
        IOUtils.closeQuietly(in);
    }
    if (downloadEpisodes) {
        for (final PodcastEpisode episode : getEpisodes(channel.getId())) {
            if (episode.getStatus() == PodcastStatus.NEW && episode.getUrl() != null) {
                downloadEpisode(episode);
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jdom.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jdom.Document) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 17 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
    RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
    CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
    executor.setConnectionRequestTimeout(4567);
    HttpPost httpPost2 = executor.createHttpPost(config);
    assertNotNull(httpPost2.getConfig());
    assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
    // Default connection timeout merged
    assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 18 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project apn-proxy by apn-proxy.

the class TestProxyWithHttpClient method test.

private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(2000);
    cm.setDefaultMaxPerRoute(40);
    cm.setDefaultConnectionConfig(connectionConfig);
    CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm).disableContentCompression().disableCookieManagement().build();
    HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());
    RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true).setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpGet request = new HttpGet(uri);
    request.setConfig(config);
    try {
        CloseableHttpResponse httpResponse = httpClient.execute(request);
        Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
        if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
            Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
        }
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseHandler.handleResponse(httpResponse);
        httpResponse.close();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) ConnectionConfig(org.apache.http.config.ConnectionConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 19 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project felix by apache.

the class SessionHandlingTest method testSessionTimeout.

@Test
public void testSessionTimeout() throws Exception {
    setupContext("test1", "/");
    setupLatches(1);
    setupServlet("foo", new String[] { "/foo" }, 1, "test1");
    assertTrue(initLatch.await(5, TimeUnit.SECONDS));
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
    final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCookieStore(new BasicCookieStore()).build();
    JsonObject json;
    // session should not be available
    // check for foo servlet
    json = getJSONResponse(httpclient, "/foo");
    assertFalse(json.getBoolean("session"));
    // create session for  context of servlet foo
    // check session and timeout (should be 10 seconds)
    json = getJSONResponse(httpclient, "/foo?create=true&timeout=true");
    assertTrue(json.getBoolean("session"));
    assertEquals("10", json.getString("timeout"));
    final String sessionId1 = json.getString("sessionId");
    assertNotNull(sessionId1);
    // after four seconds the session should still be there
    Thread.sleep(4000);
    json = getJSONResponse(httpclient, "/foo");
    assertTrue(json.getBoolean("session"));
    assertEquals("10", json.getString("timeout"));
    assertEquals(sessionId1, json.getString("sessionId"));
    // wait 10 seconds, session should be gone
    Thread.sleep(10000);
    json = getJSONResponse(httpclient, "/foo");
    assertFalse(json.getBoolean("session"));
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 20 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project SEPA by arces-wot.

the class SPARQL11Protocol method update.

/**
 * Implements a SPARQL 1.1 update operation
 * (https://www.w3.org/TR/sparql11-protocol/)
 *
 * <pre>
 * update via URL-encoded POST
 * - HTTP Method: POST
 * - Query String Parameters: None
 * - Request Content Type: <b>application/x-www-form-urlencoded</b>
 * - Request Message Body: URL-encoded, ampersand-separated query parameters. <b>update</b> (exactly 1). using-graph-uri (0 or more). using-named-graph-uri (0 or more)
 *
 * update via POST directly
 * - HTTP Method: POST
 * - Query String parameters: using-graph-uri (0 or more); using-named-graph-uri (0 or more)
 * - Request Content Type: <b>application/sparql-update</b>
 * - Request Message Body: Unencoded SPARQL update request string
 * </pre>
 *
 * UPDATE 2.2 update operation The response to an update request indicates
 * success or failure of the request via HTTP response status code.
 */
public Response update(UpdateRequest req, int timeout) {
    StringEntity requestEntity = null;
    CloseableHttpResponse httpResponse = null;
    HttpEntity responseEntity = null;
    int responseCode = 0;
    String responseBody = null;
    try {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
        // Set request entity
        if (properties.getQueryMethod().equals(HTTPMethod.GET)) {
            // ***********************
            // OpenLink VIRTUOSO PATCH
            // ***********************
            // SPARQL 1.1 Update are issued as GET requests using the "query" URL parameter
            // The "default-graph-uri" parameter is REQUIRED
            String query = "query=" + URLEncoder.encode(req.getSPARQL(), "UTF-8") + "&format=" + URLEncoder.encode(properties.getUpdateAcceptHeader(), "UTF-8");
            if (properties.getDefaultGraphURI() != null) {
                query += "&default-graph-uri=" + URLEncoder.encode(properties.getDefaultGraphURI(), "UTF-8");
            }
            String url;
            if (properties.getHttpPort() != -1)
                url = "http://" + properties.getHost() + ":" + properties.getHttpPort() + "/" + properties.getUpdatePath() + "?" + query;
            else
                url = "http://" + properties.getHost() + "/" + properties.getUpdatePath() + "?" + query;
            HttpGet queryGetRequest;
            queryGetRequest = new HttpGet(url);
            queryGetRequest.setConfig(requestConfig);
            updateRequest = queryGetRequest;
        } else {
            if (properties.getUpdateMethod().equals(HTTPMethod.URL_ENCODED_POST)) {
                requestEntity = new StringEntity("update=" + URLEncoder.encode(req.getSPARQL(), "UTF-8"));
            } else if (properties.getUpdateMethod().equals(HTTPMethod.POST)) {
                requestEntity = new StringEntity(req.getSPARQL(), Consts.UTF_8);
            }
            updatePostRequest.setEntity(requestEntity);
            updatePostRequest.setConfig(requestConfig);
            updateRequest = updatePostRequest;
        }
        // Execute HTTP request
        logger.debug("Execute SPARQL 1.1 QUERY (timeout: " + timeout + " ms) " + updatePostRequest.toString(), timeout);
        long timing = System.nanoTime();
        httpResponse = httpClient.execute(updateRequest);
        timing = System.nanoTime() - timing;
        logger.debug("UPDATE_TIME (" + timing / 1000000 + " ms)");
        // Status code
        responseCode = httpResponse.getStatusLine().getStatusCode();
        // Body
        responseEntity = httpResponse.getEntity();
        responseBody = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
        EntityUtils.consume(responseEntity);
    } catch (IOException e) {
        return new ErrorResponse(req.getToken(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        try {
            if (httpResponse != null)
                httpResponse.close();
        } catch (IOException e) {
            return new ErrorResponse(req.getToken(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
        requestEntity = null;
        responseEntity = null;
    }
    if (responseCode >= 400) {
        try {
            return new ErrorResponse(req.getToken(), new JsonParser().parse(responseBody).getAsJsonObject());
        } catch (JsonParseException e) {
            return new ErrorResponse(req.getToken(), responseCode, responseBody);
        }
    }
    return new UpdateResponse(req.getToken(), responseBody);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) StringEntity(org.apache.http.entity.StringEntity) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonParser(com.google.gson.JsonParser)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)146 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)97 HttpGet (org.apache.http.client.methods.HttpGet)94 IOException (java.io.IOException)78 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)65 HttpResponse (org.apache.http.HttpResponse)60 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)55 URI (java.net.URI)46 StringEntity (org.apache.http.entity.StringEntity)43 Map (java.util.Map)41 Test (org.junit.Test)41 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 HttpHost (org.apache.http.HttpHost)32 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)32 HttpClient (org.apache.http.client.HttpClient)31 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)27 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)24 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)24