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