Search in sources :

Example 46 with HttpRequestBase

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

the class CacheHeaderTest method doETag.

// test ETag
@Override
protected void doETag(String method) throws Exception {
    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("ETag");
    assertNotNull("We got no ETag in the response", head);
    assertTrue("Not a valid ETag", head.getValue().startsWith("\"") && head.getValue().endsWith("\""));
    String etag = head.getValue();
    // If-None-Match tests
    // we set a non matching ETag
    get = getSelectMethod(method);
    get.addHeader("If-None-Match", "\"xyz123456\"");
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-None-Match: Got no response code 200 in response to non matching ETag", 200, response.getStatusLine().getStatusCode());
    // now we set matching ETags
    get = getSelectMethod(method);
    get.addHeader("If-None-Match", "\"xyz1223\"");
    get.addHeader("If-None-Match", "\"1231323423\", \"1211211\",   " + etag);
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-None-Match: Got no response 304 to matching ETag", 304, response.getStatusLine().getStatusCode());
    // we now set the special star ETag
    get = getSelectMethod(method);
    get.addHeader("If-None-Match", "*");
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-None-Match: Got no response 304 for star ETag", 304, response.getStatusLine().getStatusCode());
    // If-Match tests
    // we set a non matching ETag
    get = getSelectMethod(method);
    get.addHeader("If-Match", "\"xyz123456\"");
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-Match: Got no response code 412 in response to non matching ETag", 412, response.getStatusLine().getStatusCode());
    // now we set matching ETags
    get = getSelectMethod(method);
    get.addHeader("If-Match", "\"xyz1223\"");
    get.addHeader("If-Match", "\"1231323423\", \"1211211\",   " + etag);
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-Match: Got no response 200 to matching ETag", 200, response.getStatusLine().getStatusCode());
    // now we set the special star ETag
    get = getSelectMethod(method);
    get.addHeader("If-Match", "*");
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("If-Match: Got no response 200 to star ETag", 200, response.getStatusLine().getStatusCode());
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse)

Example 47 with HttpRequestBase

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

the class CacheHeaderTestBase method getSelectMethod.

protected HttpRequestBase getSelectMethod(String method, String... params) throws URISyntaxException {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    HttpRequestBase m = null;
    ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
    if (params.length == 0) {
        qparams.add(new BasicNameValuePair("q", "solr"));
        qparams.add(new BasicNameValuePair("qt", "standard"));
    }
    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() + "/select?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
    if ("GET".equals(method)) {
        m = new HttpGet(uri);
    } else if ("HEAD".equals(method)) {
        m = new HttpHead(uri);
    } else if ("POST".equals(method)) {
        m = new HttpPost(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 48 with HttpRequestBase

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

the class JettyWebappTest method testAdminUI.

public void testAdminUI() throws Exception {
    // Currently not an extensive test, but it does fire up the JSP pages and make 
    // sure they compile ok
    String adminPath = "http://127.0.0.1:" + port + context + "/";
    byte[] bytes = IOUtils.toByteArray(new URL(adminPath).openStream());
    // real error will be an exception
    assertNotNull(bytes);
    HttpClient client = HttpClients.createDefault();
    HttpRequestBase m = new HttpGet(adminPath);
    HttpResponse response = client.execute(m, HttpClientUtil.createNewHttpClientRequestContext());
    assertEquals(200, response.getStatusLine().getStatusCode());
    Header header = response.getFirstHeader("X-Frame-Options");
    assertEquals("DENY", header.getValue().toUpperCase(Locale.ROOT));
    m.releaseConnection();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URL(java.net.URL)

Example 49 with HttpRequestBase

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

the class HttpSolrCall method remoteQuery.

private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
        String urlstr = coreUrl + queryParams.toQueryString();
        boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
        if ("GET".equals(req.getMethod())) {
            method = new HttpGet(urlstr);
        } else if ("HEAD".equals(req.getMethod())) {
            method = new HttpHead(urlstr);
        } else if (isPostOrPutRequest) {
            HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
            // Prevent close of container streams
            InputStream in = new CloseShieldInputStream(req.getInputStream());
            HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
            entityRequest.setEntity(entity);
            method = entityRequest;
        } else if ("DELETE".equals(req.getMethod())) {
            method = new HttpDelete(urlstr);
        } else {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
        }
        for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
            String headerName = e.nextElement();
            if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) {
                method.addHeader(headerName, req.getHeader(headerName));
            }
        }
        // These headers not supported for HttpEntityEnclosingRequests
        if (method instanceof HttpEntityEnclosingRequest) {
            method.removeHeaders(TRANSFER_ENCODING_HEADER);
            method.removeHeaders(CONTENT_LENGTH_HEADER);
        }
        final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext());
        int httpStatus = response.getStatusLine().getStatusCode();
        httpEntity = response.getEntity();
        resp.setStatus(httpStatus);
        for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) {
            Header header = responseHeaders.nextHeader();
            // encoding issues with Tomcat
            if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
                resp.addHeader(header.getName(), header.getValue());
            }
        }
        if (httpEntity != null) {
            if (httpEntity.getContentEncoding() != null)
                resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
            if (httpEntity.getContentType() != null)
                resp.setContentType(httpEntity.getContentType().getValue());
            InputStream is = httpEntity.getContent();
            OutputStream os = resp.getOutputStream();
            IOUtils.copyLarge(is, os);
        }
    } catch (IOException e) {
        sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e));
    } finally {
        Utils.consumeFully(httpEntity);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HeaderIterator(org.apache.http.HeaderIterator) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) SolrException(org.apache.solr.common.SolrException)

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