com.jsunsoft.http.Response Maven / Gradle / Ivy
Show all versions of http-request Show documentation
package com.jsunsoft.http;
/*
* Copyright 2017 Benik Arakelyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.jsunsoft.http.annotations.Beta;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import java.io.IOException;
import java.net.URI;
public interface Response extends ClassicHttpResponse {
/**
* Read the entity input stream as an instance of specified Java type using a {@link ResponseBodyReader}.
*
* Note: method will throw any unchecked exception which will occurred in specified {@link ResponseBodyReader}.
*
* @param responseType Java type the response entity will be converted to.
* @param response entity type.
*
* @return Response entity
*
* @throws ResponseBodyProcessingException when body processing failed.
*/
T readEntity(Class responseType);
/**
* Read the entity input stream as an instance of specified Java type using a {@link ResponseBodyReader}.
*
* Note: method will throw any unchecked exception which will occurred in specified {@link ResponseBodyReader}.
*
* @param responseType Java type the response entity will be converted to.
* @param response entity type.
*
* @return Response entity
*
* @throws ResponseBodyProcessingException when body processing failed.
*/
T readEntity(TypeReference responseType);
/**
* Read the entity input stream as an instance of specified Java type using a {@link ResponseBodyReader}.
*
* Note: method will throw any unchecked exception which will occurred in specified {@link ResponseBodyReader}.
*
* @param responseType Java type the response entity will be converted to.
* @param response entity type.
*
* @return Response entity
*
* @throws IOException If the stream could not be created or error occurs reading the input stream.
* @throws ResponseBodyReaderException If Cannot deserialize content
*/
@Beta
T readEntityChecked(Class responseType) throws IOException;
/**
* Read the entity input stream as an instance of specified Java type using a {@link ResponseBodyReader}.
*
*
* Note: method will throw any unchecked exception which will occurred in specified {@link ResponseBodyReader}.
*
* @param responseType Java type the response entity will be converted to.
* @param response entity type.
*
* @return Response entity
*
* @throws IOException If the stream could not be created or error occurs reading the input stream.
* @throws ResponseBodyReaderException If Cannot deserialize content
*/
@Beta
T readEntityChecked(TypeReference responseType) throws IOException;
/**
* @return the request URI
*/
URI getURI();
default boolean hasEntity() {
return getEntity() != null;
}
/**
* @return Content type of response
*/
default ContentType getContentType() {
return HttpRequestUtils.getContentTypeFromHttpEntity(getEntity());
}
/**
* @return the status code.
*
* @see #getCode()
* @deprecated use getCode instead
*/
@Deprecated
default int getStatusCode() {
return getCode();
}
/**
* @return Returns true if status code contains [200, 300) else false
*/
default boolean isSuccess() {
return HttpRequestUtils.isSuccess(getCode());
}
/**
* @return Returns true if status code isn't contains [200, 300) else false
*/
default boolean isNonSuccess() {
return !isSuccess();
}
}