public void invokeAfterPropertiesSet() throws Exception { feign.codec.ErrorDecoder errorDecoder = null; String[] beanNameArray = this.applicationContext.getBeanNamesForType(feign.codec.ErrorDecoder.class); for (int i = 0; beanNameArray != null && i < beanNameArray.length; i++) { String beanName = beanNameArray[i]; Object beanInst = this.applicationContext.getBean(beanName); if (CompensableFeignErrorDecoder.class.isInstance(beanInst)) { continue; } else if (errorDecoder != null) { throw new RuntimeException("There are more than one feign.codec.ErrorDecoder exists!"); } else { errorDecoder = (feign.codec.ErrorDecoder) beanInst; } } if (errorDecoder == null) { errorDecoder = new ErrorDecoder.Default(); } // end-if (errorDecoder == null) this.delegate = errorDecoder; }
public Object decode(Response resp, Type type) throws IOException, DecodeException, FeignException { Request request = resp.request(); String reqTransactionStr = this.getHeaderValue(request, HEADER_TRANCACTION_KEY); String reqPropagationStr = this.getHeaderValue(request, HEADER_PROPAGATION_KEY); String respTransactionStr = this.getHeaderValue(resp, HEADER_TRANCACTION_KEY); String respPropagationStr = this.getHeaderValue(resp, HEADER_PROPAGATION_KEY); if (StringUtils.isBlank(reqTransactionStr)) { return this.delegate.decode(resp, type); } else if (StringUtils.isBlank(reqPropagationStr)) { return this.delegate.decode(resp, type); } try { String transactionStr = StringUtils.isBlank(respTransactionStr) ? reqTransactionStr : respTransactionStr; String propagationStr = StringUtils.isBlank(respPropagationStr) ? reqPropagationStr : respPropagationStr; byte[] byteArray = Base64.getDecoder().decode(transactionStr); // ByteUtils.stringToByteArray(transactionStr); TransactionContext transactionContext = (TransactionContext) SerializeUtils.deserializeObject(byteArray); SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); RemoteCoordinator remoteCoordinator = beanRegistry.getConsumeCoordinator(propagationStr); TransactionResponseImpl response = new TransactionResponseImpl(); response.setTransactionContext(transactionContext); response.setSourceTransactionCoordinator(remoteCoordinator); } catch (IOException ex) { logger.error("Error occurred while decoding response({})!", resp, ex); } return this.delegate.decode(resp, type); }
public Exception decode(String methodKey, Response resp) { Request request = resp.request(); String reqTransactionStr = this.getHeaderValue(request, HEADER_TRANCACTION_KEY); String reqPropagationStr = this.getHeaderValue(request, HEADER_PROPAGATION_KEY); String respTransactionStr = this.getHeaderValue(resp, HEADER_TRANCACTION_KEY); String respPropagationStr = this.getHeaderValue(resp, HEADER_PROPAGATION_KEY); if (StringUtils.isBlank(reqTransactionStr)) { return this.delegate.decode(methodKey, resp); } else if (StringUtils.isBlank(reqPropagationStr)) { return this.delegate.decode(methodKey, resp); } // int status = resp.status(); try { String transactionStr = StringUtils.isBlank(respTransactionStr) ? reqTransactionStr : respTransactionStr; String propagationStr = StringUtils.isBlank(respPropagationStr) ? reqPropagationStr : respPropagationStr; byte[] byteArray = Base64.getDecoder().decode(transactionStr); // ByteUtils.stringToByteArray(transactionStr); TransactionContext transactionContext = (TransactionContext) SerializeUtils.deserializeObject(byteArray); SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); RemoteCoordinator remoteCoordinator = beanRegistry.getConsumeCoordinator(propagationStr); TransactionResponseImpl response = new TransactionResponseImpl(); response.setTransactionContext(transactionContext); response.setSourceTransactionCoordinator(remoteCoordinator); } catch (IOException ex) { logger.error("Error occurred while decoding response: methodKey= {}, response= {}", methodKey, resp, ex); } return this.delegate.decode(methodKey, resp); }
Object decode(Response response) throws Throwable { try { return decoder.decode(response, metadata.returnType()); } catch (FeignException e) { throw e; } catch (RuntimeException e) { throw new DecodeException(e.getMessage(), e); } }
@Override protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable, Map<String, Object> variables) { Object body = argv[metadata.bodyIndex()]; checkArgument(body != null, "Body parameter %s was null", metadata.bodyIndex()); try { encoder.encode(body, metadata.bodyType(), mutable); } catch (EncodeException e) { throw e; } catch (RuntimeException e) { throw new EncodeException(e.getMessage(), e); } return super.resolve(argv, mutable, variables); } }
/** * Constructor with the default Feign's encoder as a delegate. */ public SpringFormEncoder () { this(new Encoder.Default()); }
@Override public Exception decode(String methodKey, Response response) { FeignException exception = errorStatus(methodKey, response); Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER)); if (retryAfter != null) { return new RetryableException(exception.getMessage(), exception, retryAfter); } return exception; }
public <T> T build(final Class<T> clazz, final String endpoint, final ErrorDecoder decoder) { final Feign.Builder builder = Feign.builder(); if (null != this.options) { builder.options(this.options); } if (null != this.defaults) { builder.retryer(this.defaults); } builder.encoder(new JsonObjectEncoder()); builder.decoder(new JsonObjectDecoder()); if (null != decoder) { builder.errorDecoder(decoder); } return builder.target(clazz, endpoint); }
@Override public Exception decode(String methodKey, Response response) { try { DefaultRestErrorResponse apiErrorResponse = (DefaultRestErrorResponse) decoder.decode(response, DefaultRestErrorResponse.class); if (apiErrorResponse != null && errorResponseMappers.containsKey(apiErrorResponse.getErrorCode())) { return getExceptionSupplierFromExceptionClass( errorResponseMappers.get(apiErrorResponse.getErrorCode()), apiErrorResponse.getMessage()); } } catch (IOException e) { log.error("Error decoding error response", e); } catch (Exception e) { log.error("Error instantiating the exception mapped for the REST service error response '{}'", response, e); } return fallbackErrorDecoder.decode(methodKey, response); }
private void verifyNotDelegated(Response response) throws IOException { textDelegateDecoder.decode(response, String.class); verify(delegate, never()).decode(any(), any()); } }
/** * Transforms HTTP response body into object using decoder. * * @param response HTTP response * * @return decoded result * * @throws IOException IO exception during the reading of InputStream of response * @throws DecodeException when decoding failed due to a checked or unchecked exception besides * IOException * @throws FeignException when decoding succeeds, but conveys the operation failed */ private Object decode(final Response response) throws IOException, FeignException { try { return decoder.decode(response, metadata.returnType()); } catch (final FeignException feignException) { /* All feign exception including decode exceptions */ throw feignException; } catch (final RuntimeException unexpectedException) { /* Any unexpected exception */ throw new DecodeException(unexpectedException.getMessage(), unexpectedException); } }
private Response makeDefaultDecoderReplyWithHeadersWhenReceivingResponse( int status, Exception exception, Map<String, Collection<String>> headers) { Response response = createResponse(status, headers); when(defaultDecoder.decode(EMPTY_METHOD_KEY, response)).thenReturn(exception); return response; }
/** * Constructor with the default Feign's encoder as a delegate. */ public FormEncoder () { this(new Encoder.Default()); }
@org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean(feign.codec.ErrorDecoder.class) @org.springframework.context.annotation.Bean public feign.codec.ErrorDecoder errorDecoder() { return new ErrorDecoder.Default(); }
private void verifyDelegated(Response response) throws IOException { textDelegateDecoder.decode(response, mock(Type.class)); verify(delegate).decode(any(), any()); }
@Override public Exception decode(String methodKey, Response response) { if (response.status() >= 400 && response.status() <= 499) { throw new WebApplicationException(response.toString()); } return defaultErrorDecoder.decode(methodKey, response); }
@Override public Object decode(final Response response, final Type type) throws IOException, FeignException { final Object ret; if (isParameterizeHttpEntity(type)) { final Type type_ = ((ParameterizedType) type).getActualTypeArguments()[0]; final Object decodedObject = decoder.decode(response, type_); ret = createResponse(decodedObject, response); } else if (isHttpEntity(type)) { ret = createResponse(null, response); } else { ret = decoder.decode(response, type); } return ret; }
@Override public Object decode(final Response response, final Type type) throws IOException, FeignException { final Object ret; if (isParameterizeHttpEntity(type)) { final Type type_ = ((ParameterizedType) type).getActualTypeArguments()[0]; final Object decodedObject = decoder.decode(response, type_); ret = createResponse(decodedObject, response); } else if (isHttpEntity(type)) { ret = createResponse(null, response); } else { ret = decoder.decode(response, type); } return ret; }