Search in sources :

Example 1 with MediaType

use of com.okta.commons.http.MediaType in project okta-commons-java by okta.

the class HttpClientRequestExecutor method toSdkResponse.

protected Response toSdkResponse(HttpResponse httpResponse) throws IOException {
    int httpStatus = httpResponse.getStatusLine().getStatusCode();
    HttpHeaders headers = getHeaders(httpResponse);
    MediaType mediaType = headers.getContentType();
    HttpEntity entity = getHttpEntity(httpResponse);
    InputStream body = entity != null ? entity.getContent() : null;
    long contentLength;
    // ensure that the content has been fully acquired before closing the http stream
    if (body != null) {
        byte[] bytes = toBytes(entity);
        contentLength = entity.getContentLength();
        if (bytes != null) {
            body = new ByteArrayInputStream(bytes);
        } else {
            body = null;
        }
    } else {
        // force 0 content length when there is no body
        contentLength = 0;
    }
    Response response = new DefaultResponse(httpStatus, mediaType, body, contentLength);
    response.getHeaders().putAll(headers);
    return response;
}
Also used : DefaultResponse(com.okta.commons.http.DefaultResponse) Response(com.okta.commons.http.Response) HttpResponse(org.apache.http.HttpResponse) DefaultResponse(com.okta.commons.http.DefaultResponse) HttpHeaders(com.okta.commons.http.HttpHeaders) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaType(com.okta.commons.http.MediaType)

Example 2 with MediaType

use of com.okta.commons.http.MediaType in project okta-commons-java by okta.

the class OkHttpRequestExecutor method toSdkResponse.

private Response toSdkResponse(okhttp3.Response okResponse) throws IOException {
    int httpStatus = okResponse.code();
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(okResponse.headers().toMultimap());
    MediaType mediaType = headers.getContentType();
    ResponseBody body = okResponse.body();
    InputStream bodyInputStream = null;
    long contentLength;
    // ensure that the content has been fully acquired before closing the http stream
    if (body != null) {
        contentLength = body.contentLength();
        bodyInputStream = new ByteArrayInputStream(body.bytes());
    } else {
        // force 0 content length when there is no body
        contentLength = 0;
    }
    Response response = new DefaultResponse(httpStatus, mediaType, bodyInputStream, contentLength);
    response.getHeaders().putAll(headers);
    return response;
}
Also used : DefaultResponse(com.okta.commons.http.DefaultResponse) Response(com.okta.commons.http.Response) DefaultResponse(com.okta.commons.http.DefaultResponse) HttpHeaders(com.okta.commons.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaType(com.okta.commons.http.MediaType) ResponseBody(okhttp3.ResponseBody)

Aggregations

DefaultResponse (com.okta.commons.http.DefaultResponse)2 HttpHeaders (com.okta.commons.http.HttpHeaders)2 MediaType (com.okta.commons.http.MediaType)2 Response (com.okta.commons.http.Response)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ResponseBody (okhttp3.ResponseBody)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1