org.springframework.http.client.ClientHttpResponse#getRawStatusCode ( )源码实例Demo

下面列出了org.springframework.http.client.ClientHttpResponse#getRawStatusCode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


/**
 * 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);
	}
}
 

/**
 * 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);
	}
}
 

@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();
        }
    }
}
 

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;
}
 

/**
 * 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);
}
 
源代码6 项目: mPaaS   文件: RestResponseErrorHandler.java

@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);
}
 

@Override
public @Nullable Integer getResponseHttpStatus(@Nullable ClientHttpResponse response) {
    if (response == null) {
        return null;
    }

    try {
        return response.getRawStatusCode();
    }
    catch (IOException ioe) {
        return null;
    }
}
 

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));
    }
}
 

@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)");
}
 
源代码11 项目: riptide   文件: StreamsTest.java

private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
源代码12 项目: mPaaS   文件: RestResponseErrorHandler.java

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
    int rawStatusCode = response.getRawStatusCode();
    HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
    return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode));
}
 
源代码13 项目: riptide   文件: CaptureTest.java

private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
	return response.getRawStatusCode() != 200;
}
 
源代码15 项目: riptide   文件: RouteTest.java

private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 
源代码16 项目: riptide   文件: CallTest.java

private void fail(final ClientHttpResponse response) throws IOException {
    throw new AssertionError(response.getRawStatusCode());
}
 

/**
 * 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;
}
 

/**
 * 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));
}
 

/**
 * 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));
	}
}