public class HttpResponseUtil extends Object
HttpResponse
, StatusLine
, etc.Modifier and Type | Method and Description |
---|---|
static Map<String,String> |
getCondensedHeaders(org.apache.http.HttpResponse response)
Fetches all the response headers from the given
HttpResponse and returns a Map of header name and
corresponding header values. |
static boolean |
isContentType(org.apache.http.HttpEntity entity,
org.apache.http.entity.ContentType contentType)
Determines if the content type of the entity matches the provided content type.
|
static boolean |
isContentType(org.apache.http.HttpResponse response,
org.apache.http.entity.ContentType contentType)
Determines if the content type of the response entity matches the provided content type.
|
static boolean |
isEntityAvailable(org.apache.http.HttpResponse response)
Determines if a content is available.
|
static boolean |
isJson(org.apache.http.HttpResponse response)
Determines if the content type of the response entity is
ContentType.APPLICATION_JSON . |
static boolean |
isMimeTypeMatching(org.apache.http.entity.ContentType contentType,
org.apache.http.entity.ContentType otherContentType)
Determines both
ContentType by the mime type. |
static boolean |
isResponseEntityNull(org.apache.http.HttpResponse response)
Returns true if the response from the service has no data.
|
static boolean |
isStatus(org.apache.http.HttpResponse response,
int statusCode)
Determines if the provided status code matches the status code of the provided response.
|
static boolean |
isStatus(org.apache.http.StatusLine statusLine,
int statusCode)
Determines if the provided status code matches the status code of the provided response.
|
static ConnectorStatus |
toConnectorStatus(org.apache.http.StatusLine status)
Converts the provided
StatusLine to a ConnectorStatus . |
static OperationStatus |
toOperationStatus(org.apache.http.StatusLine status)
|
static Payload |
toPayload(org.apache.http.HttpResponse response)
Converts the entity content of the provided response to a
Payload |
static ExtendedPayload |
toPayload(org.apache.http.HttpResponse response,
PayloadMetadata metadata)
Converts the entity content of the provided response to a
ExtendedPayload with the provided
PayloadMetadata |
static String |
toStatusMessage(org.apache.http.StatusLine status)
Converts the provided
StatusLine to a status message |
public static Payload toPayload(org.apache.http.HttpResponse response) throws IOException
Payload
response
- the HTTP responseIOException
public static ExtendedPayload toPayload(org.apache.http.HttpResponse response, PayloadMetadata metadata) throws IOException
ExtendedPayload
with the provided
PayloadMetadata
response
- the HTTP responsemetadata
- for the payload being createdIOException
public static String toStatusMessage(org.apache.http.StatusLine status)
StatusLine
to a status messagestatus
- the status lineStatusLine.getReasonPhrase()
if available, otherwise an inferred message based on
StatusLine.getStatusCode()
if available, null otherwisepublic static OperationStatus toOperationStatus(org.apache.http.StatusLine status)
StatusLine
to an OperationStatus
based on
StatusLine.getStatusCode()
. Note this method deviates from previous determinations based on status code
as any "valid" status code that is not a success is treated as an OperationStatus.APPLICATION_ERROR
.
Previous rules only treated 2xx as success and 3xx/4xx as application errors. All other codes were treated as
failures. However, lots services are poorly behaved and don't fit nicely into that box.
Success Status Codes: 2xx
Valid Status Codes: 1xx, 2xx, 3xx, 4xx, 5xxstatus
- the status lineOperationStatus.SUCCESS
if the status code is available and a success, otherwise
OperationStatus.APPLICATION_ERROR
if the status code is available and valid,
OperationStatus.FAILURE
otherwisepublic static ConnectorStatus toConnectorStatus(org.apache.http.StatusLine status)
StatusLine
to a ConnectorStatus
. The status message and operation status
are determined using toStatusMessage(StatusLine)
and toOperationStatus(StatusLine)
respectively. The status code is a string representation of StatusLine.getStatusCode()
.status
- the status lineConnectorStatus.UNKNOWN
otherwisepublic static boolean isJson(org.apache.http.HttpResponse response)
ContentType.APPLICATION_JSON
. This method
adheres to the same contract as isContentType(HttpResponse, ContentType)
.response
- the HTTP responseContentType.APPLICATION_JSON
public static boolean isContentType(org.apache.http.HttpResponse response, org.apache.http.entity.ContentType contentType)
response
- the HTTP responsecontentType
- the contentpublic static boolean isContentType(org.apache.http.HttpEntity entity, org.apache.http.entity.ContentType contentType)
entity
- the entitycontentType
- the contentpublic static boolean isMimeTypeMatching(org.apache.http.entity.ContentType contentType, org.apache.http.entity.ContentType otherContentType)
ContentType
by the mime type. this is not case-sensitive. returns true if both content type
is the same. returns false otherwise.contentType
- otherContentType
- public static boolean isResponseEntityNull(org.apache.http.HttpResponse response)
response
- public static boolean isStatus(org.apache.http.HttpResponse response, int statusCode)
response
- the HTTP responsestatusCode
- the status codeHttpResponse
is not null, HttpResponse.getStatusLine()
is not null, and
StatusLine.getStatusCode()
matches the provided status code. false otherwisepublic static boolean isStatus(org.apache.http.StatusLine statusLine, int statusCode)
statusLine
- the HTTP response status linestatusCode
- the status codeStatusLine
is not null and StatusLine.getStatusCode()
matches the provided
status code. false otherwisepublic static Map<String,String> getCondensedHeaders(org.apache.http.HttpResponse response)
HttpResponse
and returns a Map
of header name and
corresponding header values. If more than one headers with the same name and different casing exist, the values
will be combined with a ", " and wrapped under the first header naming.response
- http response from an endpointpublic static boolean isEntityAvailable(org.apache.http.HttpResponse response)
response
- the HTTP response