Java Code Examples for org.springframework.http.client.ClientHttpResponse#getStatusText()

The following examples show how to use org.springframework.http.client.ClientHttpResponse#getStatusText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestClientResponseErrorHandler.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example 2
Source File: JsonRpcResponseErrorHandler.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@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 + "]");
	}
}
 
Example 3
Source File: CloudControllerResponseErrorHandler.java    From cf-java-client-sap with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: RestErrorHandler.java    From Qualitis with Apache License 2.0 6 votes vote down vote up
@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 + "]");
    }
}
 
Example 5
Source File: RestClientResponseErrorHandler.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: VndErrorResponseErrorHandler.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@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));
	}

}
 
Example 7
Source File: DefaultResponseErrorHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 + "]");
	}
}
 
Example 8
Source File: CustomResponseErrorHandler.java    From sdk-rest with MIT License 5 votes vote down vote up
@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 + "]");
    }
}
 
Example 9
Source File: DefaultResponseErrorHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 + "]");
	}
}
 
Example 10
Source File: JsonRpcResponseErrorHandler.java    From jsonrpc4j with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: DefaultResponseErrorHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 12
Source File: DefaultResponseErrorHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: RestResponseErrorHandler.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: DefaultResponseErrorHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: RestTemplateXhrTransport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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;
}
 
Example 16
Source File: ContentTypeDispatchTest.java    From riptide with MIT License 4 votes vote down vote up
private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getStatusText());
}
 
Example 17
Source File: HttpResponseException.java    From riptide with MIT License 4 votes vote down vote up
public HttpResponseException(final String message, final ClientHttpResponse response) throws IOException {
    this(message, response.getRawStatusCode(), response.getStatusText(), response.getHeaders(),
            extractCharset(response), tryWith(response, HttpResponseException::readFromBody));
}
 
Example 18
Source File: RestTemplateXhrTransport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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;
}
 
Example 19
Source File: HeimdallResponseErrorHandler.java    From heimdall with Apache License 2.0 3 votes vote down vote up
/**
 * 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));
	}
}
 
Example 20
Source File: DefaultResponseErrorHandler.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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;
}