Search in sources :

Example 51 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project CodeUtils by boredream.

the class HttpUtils method getOrPostFile.

public static byte[] getOrPostFile(int method, String url, Map<String, String> postParams, Map<String, String> headers) throws Exception {
    HashMap<String, String> map = new HashMap<String, String>();
    if (headers != null) {
        map.putAll(headers);
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, method, postParams);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // 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);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    return bytes;
}
Also used : HashMap(java.util.HashMap) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 52 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project data-prep by Talend.

the class DefaultsTest method shouldReadEmptyJsonResponse.

@Test
public void shouldReadEmptyJsonResponse() throws Exception {
    // Given
    final ObjectMapper mapper = new ObjectMapper();
    final BasicHttpResponse httpResponse = buildResponse();
    httpResponse.setEntity(new StringEntity(""));
    // When
    final Response value = Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse);
    // Then
    assertNull(value);
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 53 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project data-prep by Talend.

the class DefaultsTest method shouldPipeInputStream_handleIOError.

@Test
public void shouldPipeInputStream_handleIOError() throws Exception {
    // When
    final BasicHttpResponse response = buildResponse();
    response.setEntity(new StringEntity("") {

        @Override
        public InputStream getContent() throws IOException {
            throw new IOException("Unexpected exception");
        }
    });
    try {
        Defaults.pipeStream().apply(buildRequest(), response);
    } catch (TDPException e) {
        // Then
        assertEquals(CommonErrorCodes.UNEXPECTED_EXCEPTION, e.getCode());
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 54 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project data-prep by Talend.

the class DefaultsTest method shouldReadJsonResponse_ioException.

@Test
public void shouldReadJsonResponse_ioException() throws Exception {
    // Given
    final ObjectMapper mapper = new ObjectMapper();
    final BasicHttpResponse httpResponse = buildResponse();
    httpResponse.setEntity(new StringEntity("") {

        @Override
        public InputStream getContent() throws IOException {
            throw new IOException();
        }
    });
    // When
    try {
        Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse);
    } catch (TDPException e) {
        // Then
        assertEquals(CommonErrorCodes.UNEXPECTED_EXCEPTION, e.getCode());
    }
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) TDPException(org.talend.dataprep.exception.TDPException) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 55 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project data-prep by Talend.

the class DefaultsTest method shouldReadJsonResponse.

@Test
public void shouldReadJsonResponse() throws Exception {
    // Given
    final Response response = new Response();
    response.value = "My Value";
    final ObjectMapper mapper = new ObjectMapper();
    final BasicHttpResponse httpResponse = buildResponse();
    httpResponse.setEntity(new StringEntity(mapper.writeValueAsString(response)));
    // When
    final Response value = Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse);
    // Then
    assertEquals("My Value", value.value);
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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