下面列出了org.springframework.http.client.ClientHttpResponse#getStatusText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override public void handleError(ClientHttpResponse response)
throws IOException {
HttpStatus statusCode = response.getStatusCode();
switch (statusCode.series()) {
case CLIENT_ERROR:
LOGGER.error("Client Error! response: status code: {}, status text: {}, header: {}, body: {}, charset: {}",
statusCode.value(), response.getStatusText(), response.getHeaders(), Arrays.toString(getResponseBody(response)), getCharset(response));
throw new HttpRestTemplateException(String.valueOf(statusCode.value()), statusCode.value() + " " + response.getStatusText());
case SERVER_ERROR:
LOGGER.error("Server Error! response: status code: {}, status text: {}, header: {}, body: {}, charset: {}",
statusCode.value(), response.getStatusText(), response.getHeaders(), Arrays.toString(getResponseBody(response)), getCharset(response));
throw new HttpRestTemplateException(String.valueOf(statusCode.value()), statusCode.value() + " " + response.getStatusText());
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
/**
* HTTPステータスコード:5xx(サーバエラー)に対応した例外をスローします。ステータスコードと例外の対応は以下のとおりです。
* @param statusCode HTTPステータス
* @param response HTTPレスポンス
* @throws IOException I/O例外
*/
protected void handleServerError(HttpStatus statusCode, ClientHttpResponse response) throws IOException {
switch (statusCode) {
case INTERNAL_SERVER_ERROR:
if (L.isDebugEnabled()) {
L.debug(Strings.substitute(R.getString("D-SPRINGMVC-REST-CLIENT-HANDLER#0011"),
Maps.hash("status", statusCode.toString())
.map("message", getResponseBodyAsString(response))));
}
throw new InternalServerErrorException(response.getHeaders(), getResponseBody(response), getCharset(response));
case SERVICE_UNAVAILABLE:
if (L.isDebugEnabled()) {
L.debug(Strings.substitute(R.getString("D-SPRINGMVC-REST-CLIENT-HANDLER#0012"),
Maps.hash("status", statusCode.toString())
.map("message", getResponseBodyAsString(response))));
}
throw new ServiceUnavailableException(response.getHeaders(), getResponseBody(response), getCharset(response));
default:
if (L.isDebugEnabled()) {
L.debug(Strings.substitute(R.getString("D-SPRINGMVC-REST-CLIENT-HANDLER#0013"),
Maps.hash("status", statusCode.toString())
.map("message", getResponseBodyAsString(response))));
}
throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
}
}
private static CloudOperationException getException(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
String statusText = response.getStatusText();
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
if (response.getBody() != null) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = mapper.readValue(response.getBody(), Map.class);
String description = getTrimmedDescription(responseBody);
return new CloudOperationException(statusCode, statusText, description);
} catch (IOException e) {
// Fall through. Handled below.
}
}
return new CloudOperationException(statusCode, statusText);
}
@Override
public void handleError(ClientHttpResponse response)
throws IOException {
final HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
/**
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
* response status code.
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
* @see #handleError(ClientHttpResponse, HttpStatus)
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
if (statusCode == null) {
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
}
handleError(response, statusCode);
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
if (statusCode == null) {
throw new KmssServiceException("errors.unkown", new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),response.getHeaders(), getResponseBody(response), getCharset(response)));
}
handleError(response, statusCode);
}
/**
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
* response status code.
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
* @see #handleError(ClientHttpResponse, HttpStatus)
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
if (statusCode == null) {
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
}
handleError(response, statusCode);
}
/**
* Handle the error in the given response with the given resolved status code.
* <p>The default implementation throws an {@link HttpClientErrorException}
* if the status code is {@link HttpStatus.Series#CLIENT_ERROR}, an
* {@link HttpServerErrorException} if it is {@link HttpStatus.Series#SERVER_ERROR},
* and an {@link UnknownHttpStatusCodeException} in other cases.
* @since 5.0
* @see HttpClientErrorException#create
* @see HttpServerErrorException#create
*/
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
String statusText = response.getStatusText();
HttpHeaders headers = response.getHeaders();
byte[] body = getResponseBody(response);
Charset charset = getCharset(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw HttpClientErrorException.create(statusCode, statusText, headers, body, charset);
case SERVER_ERROR:
throw HttpServerErrorException.create(statusCode, statusText, headers, body, charset);
default:
throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset);
}
}
private HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
HttpStatus statusCode;
try {
statusCode = response.getStatusCode();
} catch (IllegalArgumentException ex) {
if (L.isDebugEnabled()) {
L.debug(R.getString("D-SPRINGMVC-REST-CLIENT-HANDLER#0014"), ex);
}
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
}
return statusCode;
}
private HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
final HttpStatus statusCode;
try {
statusCode = response.getStatusCode();
} catch (IllegalArgumentException ex) {
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(),
response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
}
return statusCode;
}
/**
* This default implementation throws a {@link HttpClientErrorException} if the response status code
* is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
* if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
* and a {@link RestClientException} in other cases.
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(),
getResponseBody(response), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(),
getResponseBody(response), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
/**
* This default implementation throws a {@link HttpClientErrorException} if the response status code
* is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
* if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
* and a {@link RestClientException} in other cases.
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
VndErrors vndErrors = null;
try {
if (HttpStatus.FORBIDDEN.equals(response.getStatusCode())) {
vndErrors = new VndErrors(vndErrorExtractor.extractData(response));
}
else {
vndErrors = vndErrorsExtractor.extractData(response);
}
}
catch (Exception e) {
super.handleError(response);
}
if (vndErrors != null) {
throw new DataFlowClientException(vndErrors);
}
else {
//see https://github.com/spring-cloud/spring-cloud-dataflow/issues/2898
final String message = StringUtils.hasText(response.getStatusText())
? response.getStatusText()
: String.valueOf(response.getStatusCode());
throw new DataFlowClientException(
new VndErrors(String.valueOf(response.getStatusCode()), message));
}
}
@Override
public Object extractData(ClientHttpResponse response) throws IOException {
HttpStatus httpStatus = HttpStatus.resolve(response.getRawStatusCode());
if (httpStatus == null) {
throw new UnknownHttpStatusCodeException(
response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, null);
}
if (httpStatus != HttpStatus.OK) {
throw new HttpServerErrorException(
httpStatus, response.getStatusText(), response.getHeaders(), null, null);
}
if (logger.isTraceEnabled()) {
logger.trace("XHR receive headers: " + response.getHeaders());
}
InputStream is = response.getBody();
ByteArrayOutputStream os = new ByteArrayOutputStream();
while (true) {
if (this.sockJsSession.isDisconnected()) {
if (logger.isDebugEnabled()) {
logger.debug("SockJS sockJsSession closed, closing response.");
}
response.close();
break;
}
int b = is.read();
if (b == -1) {
if (os.size() > 0) {
handleFrame(os);
}
if (logger.isTraceEnabled()) {
logger.trace("XHR receive completed");
}
break;
}
if (b == '\n') {
handleFrame(os);
}
else {
os.write(b);
}
}
return null;
}
private void fail(final ClientHttpResponse response) throws IOException {
throw new AssertionError(response.getStatusText());
}
@Override
public Object extractData(ClientHttpResponse response) throws IOException {
HttpStatus httpStatus = HttpStatus.resolve(response.getRawStatusCode());
if (httpStatus == null) {
throw new UnknownHttpStatusCodeException(
response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, null);
}
if (httpStatus != HttpStatus.OK) {
throw new HttpServerErrorException(
httpStatus, response.getStatusText(), response.getHeaders(), null, null);
}
if (logger.isTraceEnabled()) {
logger.trace("XHR receive headers: " + response.getHeaders());
}
InputStream is = response.getBody();
ByteArrayOutputStream os = new ByteArrayOutputStream();
while (true) {
if (this.sockJsSession.isDisconnected()) {
if (logger.isDebugEnabled()) {
logger.debug("SockJS sockJsSession closed, closing response.");
}
response.close();
break;
}
int b = is.read();
if (b == -1) {
if (os.size() > 0) {
handleFrame(os);
}
if (logger.isTraceEnabled()) {
logger.trace("XHR receive completed");
}
break;
}
if (b == '\n') {
handleFrame(os);
}
else {
os.write(b);
}
}
return null;
}
public HttpResponseException(final String message, final ClientHttpResponse response) throws IOException {
this(message, response.getRawStatusCode(), response.getStatusText(), response.getHeaders(),
extractCharset(response), tryWith(response, HttpResponseException::readFromBody));
}
/**
* Determine the HTTP status of the given response.
* @param response the response to inspect
* @return the associated HTTP status
* @throws IOException in case of I/O errors
* @throws UnknownHttpStatusCodeException in case of an unknown status code
* that cannot be represented with the {@link HttpStatus} enum
* @since 4.3.8
* @deprecated as of 5.0, in favor of {@link #handleError(ClientHttpResponse, HttpStatus)}
*/
@Deprecated
protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
if (statusCode == null) {
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
}
return statusCode;
}
/**
* Determine the HTTP status of the given response.
* <p>Note: Only called from {@link #handleError}, not from {@link #hasError}.
* @param response the response to inspect
* @return the associated HTTP status
* @throws IOException in case of I/O errors
* @throws UnknownHttpStatusCodeException in case of an unknown status code
* that cannot be represented with the {@link HttpStatus} enum
* @since 4.3.8
*/
protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
try {
return response.getStatusCode();
}
catch (IllegalArgumentException ex) {
throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
}
}