Search in sources :

Example 16 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.

the class HttpSolrClient method httpUriRequest.

/**
   * @lucene.experimental
   */
public HttpUriRequestResponse httpUriRequest(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException {
    HttpUriRequestResponse mrr = new HttpUriRequestResponse();
    final HttpRequestBase method = createMethod(request, null);
    ExecutorService pool = ExecutorUtil.newMDCAwareFixedThreadPool(1, new SolrjNamedThreadFactory("httpUriRequest"));
    try {
        MDC.put("HttpSolrClient.url", baseUrl);
        mrr.future = pool.submit(() -> executeMethod(method, processor));
    } finally {
        pool.shutdown();
        MDC.remove("HttpSolrClient.url");
    }
    assert method != null;
    mrr.httpUriRequest = method;
    return mrr;
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory)

Example 17 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.

the class CacheHeaderTestBase method getUpdateMethod.

protected HttpRequestBase getUpdateMethod(String method, String... params) throws URISyntaxException {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    HttpRequestBase m = null;
    ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
    for (int i = 0; i < params.length / 2; i++) {
        qparams.add(new BasicNameValuePair(params[i * 2], params[i * 2 + 1]));
    }
    URI uri = URI.create(client.getBaseURL() + "/update?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
    if ("GET".equals(method)) {
        m = new HttpGet(uri);
    } else if ("POST".equals(method)) {
        m = new HttpPost(uri);
    } else if ("HEAD".equals(method)) {
        m = new HttpHead(uri);
    }
    return m;
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead)

Example 18 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.

the class NoCacheHeaderTest method doCacheControl.

@Override
protected void doCacheControl(String method) throws Exception {
    HttpRequestBase m = getSelectMethod(method);
    HttpResponse response = getClient().execute(m);
    checkResponseBody(method, response);
    Header head = response.getFirstHeader("Cache-Control");
    assertNull("We got a cache-control header in response", head);
    head = response.getFirstHeader("Expires");
    assertNull("We got an Expires header in response", head);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse)

Example 19 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.

the class CacheHeaderTest method doLastModified.

@Override
protected void doLastModified(String method) throws Exception {
    // We do a first request to get the last modified
    // This must result in a 200 OK response
    HttpRequestBase get = getSelectMethod(method);
    HttpResponse response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Got no response code 200 in initial request", 200, response.getStatusLine().getStatusCode());
    Header head = response.getFirstHeader("Last-Modified");
    assertNotNull("We got no Last-Modified header", head);
    Date lastModified = DateUtils.parseDate(head.getValue());
    // If-Modified-Since tests
    get = getSelectMethod(method);
    get.addHeader("If-Modified-Since", DateUtils.formatDate(new Date()));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 304 NotModified response with current date", 304, response.getStatusLine().getStatusCode());
    get = getSelectMethod(method);
    get.addHeader("If-Modified-Since", DateUtils.formatDate(new Date(lastModified.getTime() - 10000)));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 200 OK response with If-Modified-Since in the past", 200, response.getStatusLine().getStatusCode());
    // If-Unmodified-Since tests
    get = getSelectMethod(method);
    get.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date(lastModified.getTime() - 10000)));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 412 Precondition failed with If-Unmodified-Since in the past", 412, response.getStatusLine().getStatusCode());
    get = getSelectMethod(method);
    get.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date()));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 200 OK response with If-Unmodified-Since and current date", 200, response.getStatusLine().getStatusCode());
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date)

Example 20 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.

the class CacheHeaderTest method doCacheControl.

@Override
protected void doCacheControl(String method) throws Exception {
    if ("POST".equals(method)) {
        HttpRequestBase m = getSelectMethod(method);
        HttpResponse response = getClient().execute(m);
        checkResponseBody(method, response);
        Header head = response.getFirstHeader("Cache-Control");
        assertNull("We got a cache-control header in response to POST", head);
        head = response.getFirstHeader("Expires");
        assertNull("We got an Expires  header in response to POST", head);
    } else {
        HttpRequestBase m = getSelectMethod(method);
        HttpResponse response = getClient().execute(m);
        checkResponseBody(method, response);
        Header head = response.getFirstHeader("Cache-Control");
        assertNotNull("We got no cache-control header", head);
        head = response.getFirstHeader("Expires");
        assertNotNull("We got no Expires header in response", head);
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse)

Aggregations

HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)49 HttpResponse (org.apache.http.HttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)15 IOException (java.io.IOException)11 Header (org.apache.http.Header)11 HttpPost (org.apache.http.client.methods.HttpPost)10 HttpEntity (org.apache.http.HttpEntity)9 URI (java.net.URI)7 ArrayList (java.util.ArrayList)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 HttpHead (org.apache.http.client.methods.HttpHead)6 Test (org.junit.Test)6 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)5 StringEntity (org.apache.http.entity.StringEntity)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 SimpleStringExtractorHandler (com.newsrob.util.SimpleStringExtractorHandler)4 Timing (com.newsrob.util.Timing)4 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 HttpPut (org.apache.http.client.methods.HttpPut)4