Search in sources :

Example 1 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project cryptomator by cryptomator.

the class WelcomeController method checkForUpdates.

private void checkForUpdates() {
    checkForUpdatesStatus.setText(localization.getString("welcome.checkForUpdates.label.currentlyChecking"));
    checkForUpdatesIndicator.setVisible(true);
    asyncTaskService.asyncTaskOf(() -> {
        RequestConfig requestConfig = //
        RequestConfig.custom().setConnectTimeout(//
        5000).setConnectionRequestTimeout(//
        5000).setSocketTimeout(//
        5000).build();
        HttpClientBuilder httpClientBuilder = //
        HttpClients.custom().disableCookieManagement().setDefaultRequestConfig(//
        requestConfig).setUserAgent("Cryptomator VersionChecker/" + ApplicationVersion.orElse("SNAPSHOT"));
        LOG.debug("Checking for updates...");
        try (CloseableHttpClient client = httpClientBuilder.build()) {
            HttpGet request = new HttpGet("https://cryptomator.org/downloads/latestVersion.json");
            try (CloseableHttpResponse response = client.execute(request)) {
                if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) {
                    try (InputStream in = response.getEntity().getContent()) {
                        Gson gson = new GsonBuilder().setLenient().create();
                        Reader utf8Reader = new InputStreamReader(in, StandardCharsets.UTF_8);
                        Map<String, String> map = gson.fromJson(utf8Reader, new TypeToken<Map<String, String>>() {
                        }.getType());
                        if (map != null) {
                            this.compareVersions(map);
                        }
                    }
                }
            }
        }
    }).andFinally(() -> {
        checkForUpdatesStatus.setText("");
        checkForUpdatesIndicator.setVisible(false);
    }).run();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 2 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project pinot by linkedin.

the class DruidThroughput method main.

@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    final int numQueries = QUERIES.length;
    final Random random = new Random(RANDOM_SEED);
    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicLong totalResponseTime = new AtomicLong(0L);
    final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS);
    for (int i = 0; i < NUM_CLIENTS; i++) {
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                try (CloseableHttpClient client = HttpClients.createDefault()) {
                    HttpPost post = new HttpPost("http://localhost:8082/druid/v2/?pretty");
                    post.addHeader("content-type", "application/json");
                    CloseableHttpResponse res;
                    while (true) {
                        try (BufferedReader reader = new BufferedReader(new FileReader(QUERY_FILE_DIR + File.separator + random.nextInt(numQueries) + ".json"))) {
                            int length = reader.read(BUFFER);
                            post.setEntity(new StringEntity(new String(BUFFER, 0, length)));
                        }
                        long start = System.currentTimeMillis();
                        res = client.execute(post);
                        res.close();
                        counter.getAndIncrement();
                        totalResponseTime.getAndAdd(System.currentTimeMillis() - start);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    long startTime = System.currentTimeMillis();
    while (true) {
        Thread.sleep(REPORT_INTERVAL_MILLIS);
        double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
        int count = counter.get();
        double avgResponseTime = ((double) totalResponseTime.get()) / count;
        System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: " + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 3 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project pinot by linkedin.

the class PinotResponseTime method main.

public static void main(String[] args) throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpPost post = new HttpPost("http://localhost:8099/query");
        CloseableHttpResponse res;
        if (STORE_RESULT) {
            File dir = new File(RESULT_DIR);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }
        int length;
        // Make sure all segments online
        System.out.println("Test if number of records is " + RECORD_NUMBER);
        post.setEntity(new StringEntity("{\"pql\":\"select count(*) from tpch_lineitem\"}"));
        while (true) {
            System.out.print('*');
            res = client.execute(post);
            boolean valid;
            try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
                length = in.read(BUFFER);
                valid = new String(BUFFER, 0, length, "UTF-8").contains("\"value\":\"" + RECORD_NUMBER + "\"");
            }
            res.close();
            if (valid) {
                break;
            } else {
                Thread.sleep(5000);
            }
        }
        System.out.println("Number of Records Test Passed");
        // Start Benchmark
        for (int i = 0; i < QUERIES.length; i++) {
            System.out.println("--------------------------------------------------------------------------------");
            System.out.println("Start running query: " + QUERIES[i]);
            post.setEntity(new StringEntity("{\"pql\":\"" + QUERIES[i] + "\"}"));
            // Warm-up Rounds
            System.out.println("Run " + WARMUP_ROUND + " times to warm up cache...");
            for (int j = 0; j < WARMUP_ROUND; j++) {
                res = client.execute(post);
                if (!isValid(res, null)) {
                    System.out.println("\nInvalid Response, Sleep 20 Seconds...");
                    Thread.sleep(20000);
                }
                res.close();
                System.out.print('*');
            }
            System.out.println();
            // Test Rounds
            int[] time = new int[TEST_ROUND];
            int totalTime = 0;
            int validIdx = 0;
            System.out.println("Run " + TEST_ROUND + " times to get average time...");
            while (validIdx < TEST_ROUND) {
                long startTime = System.currentTimeMillis();
                res = client.execute(post);
                long endTime = System.currentTimeMillis();
                boolean valid;
                if (STORE_RESULT && validIdx == 0) {
                    valid = isValid(res, RESULT_DIR + File.separator + i + ".json");
                } else {
                    valid = isValid(res, null);
                }
                if (!valid) {
                    System.out.println("\nInvalid Response, Sleep 20 Seconds...");
                    Thread.sleep(20000);
                    res.close();
                    continue;
                }
                res.close();
                time[validIdx] = (int) (endTime - startTime);
                totalTime += time[validIdx];
                System.out.print(time[validIdx] + "ms ");
                validIdx++;
            }
            System.out.println();
            // Process Results
            double avgTime = (double) totalTime / TEST_ROUND;
            double stdDev = 0;
            for (int temp : time) {
                stdDev += (temp - avgTime) * (temp - avgTime) / TEST_ROUND;
            }
            stdDev = Math.sqrt(stdDev);
            System.out.println("The average response time for the query is: " + avgTime + "ms");
            System.out.println("The standard deviation is: " + stdDev);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BufferedInputStream(java.io.BufferedInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File)

Example 4 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project elasticsearch-analysis-ik by medcl.

the class Dictionary method getRemoteWords.

/**
	 * 从远程服务器上下载自定义词条
	 */
private static List<String> getRemoteWords(String location) {
    List<String> buffer = new ArrayList<String>();
    RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response;
    BufferedReader in;
    HttpGet get = new HttpGet(location);
    get.setConfig(rc);
    try {
        response = httpclient.execute(get);
        if (response.getStatusLine().getStatusCode() == 200) {
            String charset = "UTF-8";
            // 获取编码,默认为utf-8
            if (response.getEntity().getContentType().getValue().contains("charset=")) {
                String contentType = response.getEntity().getContentType().getValue();
                charset = contentType.substring(contentType.lastIndexOf("=") + 1);
            }
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
            String line;
            while ((line = in.readLine()) != null) {
                buffer.add(line);
            }
            in.close();
            response.close();
            return buffer;
        }
        response.close();
    } catch (ClientProtocolException e) {
        logger.error("getRemoteWords {} error", e, location);
    } catch (IllegalStateException e) {
        logger.error("getRemoteWords {} error", e, location);
    } catch (IOException e) {
        logger.error("getRemoteWords {} error", e, location);
    }
    return buffer;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 5 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesProxyWithAuth.

@Test
public void usesProxyWithAuth() throws Exception {
    HttpClientConfiguration config = new HttpClientConfiguration();
    AuthConfiguration auth = new AuthConfiguration("secret", "stuff");
    ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080, "http", auth);
    config.setProxyConfiguration(proxy);
    CloseableHttpClient httpClient = checkProxy(config, new HttpHost("dropwizard.io", 80), new HttpHost("192.168.52.11", 8080, "http"));
    CredentialsProvider credentialsProvider = (CredentialsProvider) FieldUtils.getField(httpClient.getClass(), "credentialsProvider", true).get(httpClient);
    assertThat(credentialsProvider.getCredentials(new AuthScope("192.168.52.11", 8080))).isEqualTo(new UsernamePasswordCredentials("secret", "stuff"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1200 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)603 HttpGet (org.apache.http.client.methods.HttpGet)562 Test (org.junit.Test)460 IOException (java.io.IOException)317 HttpResponse (org.apache.http.HttpResponse)304 HttpPost (org.apache.http.client.methods.HttpPost)244 HttpEntity (org.apache.http.HttpEntity)237 StringEntity (org.apache.http.entity.StringEntity)156 URI (java.net.URI)154 RequestConfig (org.apache.http.client.config.RequestConfig)129 StatusLine (org.apache.http.StatusLine)108 HttpHost (org.apache.http.HttpHost)105 InputStream (java.io.InputStream)86 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)78 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)76 ArrayList (java.util.ArrayList)75 Header (org.apache.http.Header)74 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)71 CredentialsProvider (org.apache.http.client.CredentialsProvider)68