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

The following examples show how to use org.springframework.http.client.ClientHttpResponse#getRawStatusCode() . 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: RestTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Handle the given response, performing appropriate logging and
 * invoking the {@link ResponseErrorHandler} if necessary.
 * <p>Can be overridden in subclasses.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param response the resulting {@link ClientHttpResponse}
 * @throws IOException if propagated from {@link ResponseErrorHandler}
 * @since 4.1.6
 * @see #setErrorHandler
 */
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
	ResponseErrorHandler errorHandler = getErrorHandler();
	boolean hasError = errorHandler.hasError(response);
	if (logger.isDebugEnabled()) {
		try {
			int code = response.getRawStatusCode();
			HttpStatus status = HttpStatus.resolve(code);
			logger.debug("Response " + (status != null ? status : code));
		}
		catch (IOException ex) {
			// ignore
		}
	}
	if (hasError) {
		errorHandler.handleError(url, method, response);
	}
}
 
Example 2
Source File: SpringRestTemplateInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void afterExecute(@Advice.Return @Nullable ClientHttpResponse clientHttpResponse,
                                 @Advice.Local("span") @Nullable Span span,
                                 @Advice.Thrown @Nullable Throwable t) throws IOException {
    if (span != null) {
        try {
            if (clientHttpResponse != null) {
                int statusCode = clientHttpResponse.getRawStatusCode();
                span.getContext().getHttp().withStatusCode(statusCode);
            }
            span.captureException(t);
        } finally {
            span.deactivate().end();
        }
    }
}
 
Example 3
Source File: RestTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Handle the given response, performing appropriate logging and
 * invoking the {@link ResponseErrorHandler} if necessary.
 * <p>Can be overridden in subclasses.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param response the resulting {@link ClientHttpResponse}
 * @throws IOException if propagated from {@link ResponseErrorHandler}
 * @since 4.1.6
 * @see #setErrorHandler
 */
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
	ResponseErrorHandler errorHandler = getErrorHandler();
	boolean hasError = errorHandler.hasError(response);
	if (logger.isDebugEnabled()) {
		try {
			int code = response.getRawStatusCode();
			HttpStatus status = HttpStatus.resolve(code);
			logger.debug("Response " + (status != null ? status : code));
		}
		catch (IOException ex) {
			// ignore
		}
	}
	if (hasError) {
		errorHandler.handleError(url, method, response);
	}
}
 
Example 4
Source File: CustomResponseErrorHandler.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
@Override
public void handleError(ClientHttpResponse response) throws IOException {
	int code = response.getRawStatusCode();
	String content = CharStreams.toString(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
	
	Map<?, ?> responseItmes = null;
	if(code == 404 && StringUtils.isNotBlank(content)){
		responseItmes = JsonUtils.toObject(content, Map.class);
		throw new JeesuiteBaseException(404, "Page Not Found["+responseItmes.get("path")+"]");
	}

	int errorCode = 500;
	String errorMsg = content;
	try {responseItmes = JsonUtils.toObject(content, Map.class);} catch (Exception e) {}
	if(responseItmes != null){
		if(responseItmes.containsKey("code")){
			errorCode = Integer.parseInt(responseItmes.get("code").toString());
		}
		if(responseItmes.containsKey("msg")){
			errorMsg = responseItmes.get("msg").toString();
		}else if(responseItmes.containsKey("message")){
			errorMsg = responseItmes.get("message").toString();
		}
	}
	
	if(StringUtils.isBlank(errorMsg)){
		errorMsg = DEFAULT_ERROR_MSG;
	}
	
	throw new JeesuiteBaseException(errorCode, errorMsg + "(Remote)");
}
 
Example 5
Source File: WxResponseErrorHandler.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
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 6
Source File: SpringHttpClientTagAdapter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public @Nullable Integer getResponseHttpStatus(@Nullable ClientHttpResponse response) {
    if (response == null) {
        return null;
    }

    try {
        return response.getRawStatusCode();
    }
    catch (IOException ioe) {
        return null;
    }
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: CaptureTest.java    From riptide with MIT License 4 votes vote down vote up
private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
Example 12
Source File: CustomResponseErrorHandler.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
	return response.getRawStatusCode() != 200;
}
 
Example 13
Source File: RestResponseErrorHandler.java    From mPaaS with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
    int rawStatusCode = response.getRawStatusCode();
    HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
    return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode));
}
 
Example 14
Source File: RouteTest.java    From riptide with MIT License 4 votes vote down vote up
private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
Example 15
Source File: CallTest.java    From riptide with MIT License 4 votes vote down vote up
private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
Example 16
Source File: StreamsTest.java    From riptide with MIT License 4 votes vote down vote up
private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
Example 17
Source File: DefaultResponseErrorHandler.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
 * {@link #hasError(int)} (for an unknown status code) with the response status code.
 * @see ClientHttpResponse#getRawStatusCode()
 * @see #hasError(HttpStatus)
 * @see #hasError(int)
 */
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
	int rawStatusCode = response.getRawStatusCode();
	HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
	return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode));
}
 
Example 18
Source File: DefaultResponseErrorHandler.java    From java-technology-stack 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;
}
 
Example 19
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;
}
 
Example 20
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));
	}
}