Search in sources :

Example 96 with CloseableHttpClient

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

the class RestAPITestBase method executeFunctionThroughRestCall.

CloseableHttpResponse executeFunctionThroughRestCall(String function, String regionName, String filter, String jsonBody, String groups, String members) {
    System.out.println("Entering executeFunctionThroughRestCall");
    CloseableHttpResponse value = null;
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        Random randomGenerator = new Random();
        int restURLIndex = randomGenerator.nextInt(restURLs.size());
        HttpPost post = createHTTPPost(function, regionName, filter, restURLIndex, groups, members, jsonBody);
        System.out.println("Request: POST " + post.toString());
        value = httpclient.execute(post);
    } catch (Exception e) {
        fail("unexpected exception", e);
    }
    return value;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Random(java.util.Random) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 97 with CloseableHttpClient

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

the class TestQuery method query_describe_conneg.

@Test
public void query_describe_conneg() throws IOException {
    try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) {
        String query = "DESCRIBE ?s WHERE {?s ?p ?o}";
        for (MediaType type : rdfOfferTest.entries()) {
            String contentType = type.toHeaderString();
            try (QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
                qExec.setModelContentType(contentType);
                qExec.setClient(client);
                Model m = qExec.execDescribe();
                String x = qExec.getHttpResponseContentType();
                assertEquals(contentType, x);
                assertFalse(m.isEmpty());
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) Model(org.apache.jena.rdf.model.Model) MediaType(org.apache.jena.atlas.web.MediaType) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 98 with CloseableHttpClient

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

the class HTTPHC4Impl method setupClient.

private CloseableHttpClient setupClient(URL url) {
    Map<HttpClientKey, CloseableHttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
    final String host = url.getHost();
    String proxyHost = getProxyHost();
    int proxyPort = getProxyPortInt();
    String proxyPass = getProxyPass();
    String proxyUser = getProxyUser();
    // static proxy is the globally define proxy eg command line or properties
    boolean useStaticProxy = isStaticProxy(host);
    // dynamic proxy is the proxy defined for this sampler
    boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
    boolean useProxy = useStaticProxy || useDynamicProxy;
    // if both dynamic and static are used, the dynamic proxy has priority over static
    if (!useDynamicProxy) {
        proxyHost = PROXY_HOST;
        proxyPort = PROXY_PORT;
        proxyUser = PROXY_USER;
        proxyPass = PROXY_PASS;
    }
    // Lookup key - must agree with all the values used to create the HttpClient.
    HttpClientKey key = new HttpClientKey(url, useProxy, proxyHost, proxyPort, proxyUser, proxyPass);
    CloseableHttpClient httpClient = null;
    boolean concurrentDwn = this.testElement.isConcurrentDwn();
    if (concurrentDwn) {
        httpClient = (CloseableHttpClient) JMeterContextService.getContext().getSamplerContext().get(HTTPCLIENT_TOKEN);
    }
    if (httpClient == null) {
        httpClient = mapHttpClientPerHttpClientKey.get(key);
    }
    if (httpClient != null && resetSSLContext && HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
        ((AbstractHttpClient) httpClient).clearRequestInterceptors();
        ((AbstractHttpClient) httpClient).clearResponseInterceptors();
        httpClient.getConnectionManager().closeIdleConnections(1L, TimeUnit.MICROSECONDS);
        httpClient = null;
        JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
        sslMgr.resetContext();
        resetSSLContext = false;
    }
    if (httpClient == null) {
        // One-time init for this client
        HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);
        DnsResolver resolver = this.testElement.getDNSResolver();
        if (resolver == null) {
            resolver = SystemDefaultDnsResolver.INSTANCE;
        }
        MeasuringConnectionManager connManager = new MeasuringConnectionManager(createSchemeRegistry(), resolver, TIME_TO_LIVE, VALIDITY_AFTER_INACTIVITY_TIMEOUT);
        // to be realistic JMeter must set an higher value to DefaultMaxPerRoute
        if (concurrentDwn) {
            try {
                int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
                connManager.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, connManager.getDefaultMaxPerRoute()));
            } catch (NumberFormatException nfe) {
            // no need to log -> will be done by the sampler
            }
        }
        httpClient = new DefaultHttpClient(connManager, clientParams) {

            @Override
            protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
                return new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED);
            }
        };
        if (IDLE_TIMEOUT > 0) {
            ((AbstractHttpClient) httpClient).setKeepAliveStrategy(IDLE_STRATEGY);
        }
        // see https://issues.apache.org/jira/browse/HTTPCORE-397
        ((AbstractHttpClient) httpClient).setReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE);
        ((AbstractHttpClient) httpClient).addResponseInterceptor(RESPONSE_CONTENT_ENCODING);
        // HACK
        ((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER);
        ((AbstractHttpClient) httpClient).addRequestInterceptor(METRICS_RESETTER);
        // Override the default schemes as necessary
        SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
        if (SLOW_HTTP != null) {
            schemeRegistry.register(SLOW_HTTP);
        }
        // Set up proxy details
        if (useProxy) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (proxyUser.length() > 0) {
                ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUser, proxyPass, LOCALHOST, PROXY_DOMAIN));
            }
        }
        // Bug 52126 - we do our own cookie handling
        clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookieSpecs.IGNORE_COOKIES);
        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }
        // save the agent for next time round
        mapHttpClientPerHttpClientKey.put(key, httpClient);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }
    }
    if (concurrentDwn) {
        JMeterContextService.getContext().getSamplerContext().put(HTTPCLIENT_TOKEN, httpClient);
    }
    // TODO - should this be done when the client is created?
    // If so, then the details need to be added as part of HttpClientKey
    setConnectionAuthorization(httpClient, url, getAuthManager(), key);
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) JsseSSLManager(org.apache.jmeter.util.JsseSSLManager) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) NTCredentials(org.apache.http.auth.NTCredentials) AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams) SyncBasicHttpParams(org.apache.http.params.SyncBasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpHost(org.apache.http.HttpHost) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) AuthScope(org.apache.http.auth.AuthScope) SyncBasicHttpParams(org.apache.http.params.SyncBasicHttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Example 99 with CloseableHttpClient

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

the class HTTPHC4Impl method sample.

@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    if (log.isDebugEnabled()) {
        log.debug("Start : sample {} method {} followingRedirect {} depth {}", url, method, areFollowingRedirect, frameDepth);
    }
    HTTPSampleResult res = createSampleResult(url, method);
    CloseableHttpClient httpClient = setupClient(url);
    HttpRequestBase httpRequest = null;
    try {
        URI uri = url.toURI();
        if (method.equals(HTTPConstants.POST)) {
            httpRequest = new HttpPost(uri);
        } else if (method.equals(HTTPConstants.GET)) {
            // otherwise we use HttpGetWithEntity
            if (!areFollowingRedirect && ((!hasArguments() && getSendFileAsPostBody()) || getSendParameterValuesAsPostBody())) {
                httpRequest = new HttpGetWithEntity(uri);
            } else {
                httpRequest = new HttpGet(uri);
            }
        } else if (method.equals(HTTPConstants.PUT)) {
            httpRequest = new HttpPut(uri);
        } else if (method.equals(HTTPConstants.HEAD)) {
            httpRequest = new HttpHead(uri);
        } else if (method.equals(HTTPConstants.TRACE)) {
            httpRequest = new HttpTrace(uri);
        } else if (method.equals(HTTPConstants.OPTIONS)) {
            httpRequest = new HttpOptions(uri);
        } else if (method.equals(HTTPConstants.DELETE)) {
            httpRequest = new HttpDelete(uri);
        } else if (method.equals(HTTPConstants.PATCH)) {
            httpRequest = new HttpPatch(uri);
        } else if (HttpWebdav.isWebdavMethod(method)) {
            httpRequest = new HttpWebdav(method, uri);
        } else {
            throw new IllegalArgumentException("Unexpected method: '" + method + "'");
        }
        // can throw IOException
        setupRequest(url, httpRequest, res);
    } catch (Exception e) {
        res.sampleStart();
        res.sampleEnd();
        errorResult(e, res);
        return res;
    }
    HttpContext localContext = new BasicHttpContext();
    setupClientContextBeforeSample(localContext);
    res.sampleStart();
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method) && cacheManager.inCache(url)) {
        return updateSampleResultForResourceInCache(res);
    }
    CloseableHttpResponse httpResponse = null;
    try {
        currentRequest = httpRequest;
        handleMethod(method, res, httpRequest, localContext);
        // store the SampleResult in LocalContext to compute connect time
        localContext.setAttribute(SAMPLER_RESULT_TOKEN, res);
        // perform the sample
        httpResponse = executeRequest(httpClient, httpRequest, localContext, url);
        // Needs to be done after execute to pick up all the headers
        final HttpRequest request = (HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
        extractClientContextAfterSample(localContext);
        // We've finished with the request, so we can add the LocalAddress to it for display
        final InetAddress localAddr = (InetAddress) httpRequest.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS);
        if (localAddr != null) {
            request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString());
        }
        res.setRequestHeaders(getConnectionHeaders(request));
        Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        if (contentType != null) {
            String ct = contentType.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            res.setResponseData(readResponse(res, entity.getContent(), entity.getContentLength()));
        }
        // Done with the sampling proper.
        res.sampleEnd();
        currentRequest = null;
        // Now collect the results into the HTTPSampleResult:
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        res.setResponseCode(Integer.toString(statusCode));
        res.setResponseMessage(statusLine.getReasonPhrase());
        res.setSuccessful(isSuccessCode(statusCode));
        res.setResponseHeaders(getResponseHeaders(httpResponse, localContext));
        if (res.isRedirect()) {
            final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
            if (headerLocation == null) {
                // HTTP protocol violation, but avoids NPE
                throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
            }
            String redirectLocation = headerLocation.getValue();
            res.setRedirectLocation(redirectLocation);
        }
        // record some sizes to allow HTTPSampleResult.getBytes() with different options
        HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS);
        long headerBytes = // condensed length (without \r)
        (long) res.getResponseHeaders().length() + // Add \r for each header
        (long) httpResponse.getAllHeaders().length + // Add \r for initial header
        1L + // final \r\n before data
        2L;
        long totalBytes = metrics.getReceivedBytesCount();
        res.setHeadersSize((int) headerBytes);
        res.setBodySize(totalBytes - headerBytes);
        res.setSentBytes(metrics.getSentBytesCount());
        if (log.isDebugEnabled()) {
            log.debug("ResponseHeadersSize={} Content-Length={} Total={}", res.getHeadersSize(), res.getBodySizeAsLong(), (res.getHeadersSize() + res.getBodySizeAsLong()));
        }
        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
            HttpHost target = (HttpHost) localContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            URI redirectURI = req.getURI();
            if (redirectURI.isAbsolute()) {
                res.setURL(redirectURI.toURL());
            } else {
                res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
            }
        }
        // Store any cookies received in the cookie manager:
        saveConnectionCookies(httpResponse, res.getURL(), getCookieManager());
        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(httpResponse, res);
        }
        // Follow redirects and download page resources if appropriate:
        res = resultProcessing(areFollowingRedirect, frameDepth, res);
    } catch (IOException e) {
        log.debug("IOException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        // pick up headers if failed to execute the request
        if (res.getRequestHeaders() != null) {
            log.debug("Overwriting request old headers: {}", res.getRequestHeaders());
        }
        res.setRequestHeaders(getConnectionHeaders((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST)));
        errorResult(e, res);
        return res;
    } catch (RuntimeException e) {
        log.debug("RuntimeException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        errorResult(e, res);
        return res;
    } finally {
        JOrphanUtils.closeQuietly(httpResponse);
        currentRequest = null;
        JMeterContextService.getContext().getSamplerContext().remove(HTTPCLIENT_TOKEN);
    }
    return res;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) URL(java.net.URL) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CacheManager(org.apache.jmeter.protocol.http.control.CacheManager) HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StatusLine(org.apache.http.StatusLine) HttpTrace(org.apache.http.client.methods.HttpTrace) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) InetAddress(java.net.InetAddress)

Example 100 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project lucene-solr by apache.

the class SolrExceptionTest method testSolrException.

public void testSolrException() throws Throwable {
    // test a connection to a solr server that probably doesn't exist
    // this is a very simple test and most of the test should be considered verified 
    // if the compiler won't let you by without the try/catch
    boolean gotExpectedError = false;
    CloseableHttpClient httpClient = null;
    try {
        // switched to a local address to avoid going out on the net, ns lookup issues, etc.
        // set a 1ms timeout to let the connection fail faster.
        httpClient = HttpClientUtil.createClient(null);
        try (HttpSolrClient client = getHttpSolrClient("http://[ff01::114]:11235/solr/", httpClient)) {
            client.setConnectionTimeout(1);
            SolrQuery query = new SolrQuery("test123");
            client.query(query);
        }
        httpClient.close();
    } catch (SolrServerException sse) {
        gotExpectedError = true;
    /***
      assertTrue(UnknownHostException.class == sse.getRootCause().getClass()
              //If one is using OpenDNS, then you don't get UnknownHostException, instead you get back that the query couldn't execute
              || (sse.getRootCause().getClass() == SolrException.class && ((SolrException) sse.getRootCause()).code() == 302 && sse.getMessage().equals("Error executing query")));
      ***/
    } finally {
        if (httpClient != null)
            HttpClientUtil.close(httpClient);
    }
    assertTrue(gotExpectedError);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrTestCaseJ4.getHttpSolrClient(org.apache.solr.SolrTestCaseJ4.getHttpSolrClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)353 HttpGet (org.apache.http.client.methods.HttpGet)181 Test (org.junit.Test)178 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)172 HttpResponse (org.apache.http.HttpResponse)105 HttpEntity (org.apache.http.HttpEntity)82 IOException (java.io.IOException)75 HttpPost (org.apache.http.client.methods.HttpPost)62 StringEntity (org.apache.http.entity.StringEntity)59 InputStream (java.io.InputStream)40 StatusLine (org.apache.http.StatusLine)40 URI (java.net.URI)34 HttpHost (org.apache.http.HttpHost)29 RequestConfig (org.apache.http.client.config.RequestConfig)26 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 Header (org.apache.http.Header)20 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)19 AuthScope (org.apache.http.auth.AuthScope)18 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)18 CredentialsProvider (org.apache.http.client.CredentialsProvider)16