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;
}
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);
}
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());
}
}
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());
}
}
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);
}
Aggregations