use of com.bluenimble.platform.http.response.impls.HttpResponseImpl in project serverless by bluenimble.
the class DefaultHttpClient method send.
public HttpResponse send(final HttpRequest request) throws HttpClientException {
HttpURLConnection hc = null;
try {
if (request == null || request.getURI() == null) {
throw new HttpClientException("No request to proceed");
}
URL url = request.getURI().toURL();
URLConnection connection = null;
if (request.getProxy() != null) {
connection = url.openConnection(request.getProxy());
} else {
connection = url.openConnection();
}
hc = (HttpURLConnection) connection;
if (hc instanceof HttpsURLConnection) {
HttpsURLConnection https = (HttpsURLConnection) hc;
if (trustAll) {
https.setSSLSocketFactory(TrustAllSocketFactory);
https.setHostnameVerifier(TrustAllHostVerifier);
}
}
hc.setConnectTimeout(request.getConnectTimeout());
hc.setReadTimeout(request.getReadTimeout());
hc.setRequestMethod(request.getName());
if (request.getName().equals(HttpMethods.POST) || request.getName().equals(HttpMethods.PUT)) {
connection.setDoOutput(true);
}
if (!(connection instanceof HttpURLConnection)) {
throw new HttpClientException("Only Http request can be handled");
}
setRequestCookie(request);
request.write(hc);
InputStream iobody = null;
int status = hc.getResponseCode();
HttpResponseImpl response = new HttpResponseImpl(request.getId());
response.setStatus(status);
addResponseHeaders(response, hc);
String charset = request.getCharset();
HttpHeader cth = response.getHeader(HttpHeaders.CONTENT_TYPE);
if (cth != null) {
String[] values = cth.getValues();
if (values != null && values.length > 0) {
String contentType = values[0];
response.setContentType(contentType);
for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}
}
}
response.setCharset(charset);
if (request.getSuccessCodes().contains(String.valueOf(status))) {
iobody = hc.getInputStream();
} else {
iobody = hc.getErrorStream();
}
if (GZip.equals(hc.getContentEncoding())) {
iobody = new GZIPInputStream(iobody);
}
response.setBody(new HttpMessageBodyImpl(new InputStreamHttpMessageBodyPart(iobody)));
updateCookies(response);
return response;
} catch (Throwable th) {
throw new HttpClientException(th);
}
}
Aggregations