Search in sources :

Example 36 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project SimplifyReader by chentao0707.

the class HurlStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 37 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.

the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorSingleHeader.

@Test
public void testInterceptorSingleHeader() throws IOException, HttpException {
    ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
    response.addHeader("Test", "This header should not trigger the interceptor.");
    interceptor.process(response, null);
    assertThat(response.getAllHeaders()).as("Number of Headers should be unchanged.").hasSize(1);
    assertThat(response.getAllHeaders()[0].getName()).as("Remaining Header should be same as the given.").isEqualTo("Test");
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 38 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.

the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorMultipleHeaderFilteredWarningAndMultipleTriggers.

@Test
public void testInterceptorMultipleHeaderFilteredWarningAndMultipleTriggers() throws IOException, HttpException {
    ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
    response.addHeader("Test", "This header should not trigger the interceptor.");
    response.addHeader("Warning", "This warning should not trigger the interceptor.");
    response.addHeader("Warning", "This text contains the trigger: but in a future major version, direct access to system indices and their aliases will not be allowed - and should be filtered out");
    response.addHeader("Warning", "This text contains the trigger: setting was deprecated in Elasticsearch - and should be filtered out");
    assertThat(response.getAllHeaders()).as("Number of Headers should be 4 before start.").hasSize(4);
    interceptor.process(response, null);
    assertThat(response.getAllHeaders()).as("Number of Headers should be 2 less after running the interceptor.").hasSize(2);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 39 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.

the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorMultipleHeaderIgnoredWarning.

@Test
public void testInterceptorMultipleHeaderIgnoredWarning() throws IOException, HttpException {
    ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
    response.addHeader("Test", "This header should not trigger the interceptor.");
    response.addHeader("Warning", "This warning should not trigger the interceptor.");
    interceptor.process(response, null);
    assertThat(response.getAllHeaders()).as("Number of Headers should be unchanged.").hasSize(2);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 40 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.

the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorMultipleHeaderFilteredWarning.

@Test
public void testInterceptorMultipleHeaderFilteredWarning() throws IOException, HttpException {
    ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
    response.addHeader("Test", "This header should not trigger the interceptor.");
    response.addHeader("Warning", "This warning should not trigger the interceptor.");
    response.addHeader("Warning", "This text contains the trigger: setting was deprecated in Elasticsearch - and should be filtered out");
    assertThat(response.getAllHeaders()).as("Number of Headers should be 3 before start.").hasSize(3);
    interceptor.process(response, null);
    assertThat(response.getAllHeaders()).as("Number of Headers should be 1 less after running the interceptor.").hasSize(2);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

BasicHttpResponse (org.apache.http.message.BasicHttpResponse)107 BasicStatusLine (org.apache.http.message.BasicStatusLine)64 ProtocolVersion (org.apache.http.ProtocolVersion)58 HttpResponse (org.apache.http.HttpResponse)51 StatusLine (org.apache.http.StatusLine)39 Test (org.junit.Test)39 StringEntity (org.apache.http.entity.StringEntity)34 IOException (java.io.IOException)33 List (java.util.List)24 Header (org.apache.http.Header)22 HttpHost (org.apache.http.HttpHost)19 HashMap (java.util.HashMap)18 URL (java.net.URL)17 BasicHeader (org.apache.http.message.BasicHeader)17 HttpURLConnection (java.net.HttpURLConnection)16 HttpEntity (org.apache.http.HttpEntity)14 MainResponse (org.elasticsearch.action.main.MainResponse)14 Before (org.junit.Before)13 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)12