Search in sources :

Example 1 with HttpResponseImpl

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) HttpClientException(com.bluenimble.platform.http.HttpClientException) HttpHeader(com.bluenimble.platform.http.HttpHeader) HttpResponseImpl(com.bluenimble.platform.http.response.impls.HttpResponseImpl) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

HttpClientException (com.bluenimble.platform.http.HttpClientException)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpResponseImpl (com.bluenimble.platform.http.response.impls.HttpResponseImpl)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1