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); }
private void verifyNotDelegated(Response response) throws IOException { textDelegateDecoder.decode(response, String.class); verify(delegate, never()).decode(any(), any()); } }
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) { try { return (Exception) decoder.decode(response, GitHubClientError.class); } catch (IOException fallbackToDefault) { return defaultDecoder.decode(methodKey, response); } } }
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 public Object decode(Response response, Type type) throws IOException, FeignException { if (Types.getRawType(type).equals(com.google.common.base.Optional.class)) { if (response.status() == 204) { return com.google.common.base.Optional.absent(); } else { Object decoded = checkNotNull(delegate.decode(response, getInnerType(type)), "Unexpected null content for response status %s", response.status()); return com.google.common.base.Optional.of(decoded); } } else { return delegate.decode(response, type); } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(InputStream.class)) { byte[] body = Util.toByteArray(response.body().asInputStream()); return new ByteArrayInputStream(body); } else { return delegate.decode(response, type); } } }
@Override public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException { Collection<String> contentTypes = response.headers().get("Content-Type"); if (contentTypes.contains(MimeTypeUtils.TEXT_PLAIN.toString())) { return defaultDecoder.decode(response, type); } else { return gsonDecoder.decode(response, type); } } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(InputStream.class)) { byte[] body = response.body() != null ? Util.toByteArray(response.body().asInputStream()) : new byte[0]; return new ByteArrayInputStream(body); } else { return delegate.decode(response, type); } } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(InputStream.class)) { byte[] body = response.body() != null ? Util.toByteArray(response.body().asInputStream()) : new byte[0]; return new ByteArrayInputStream(body); } else { return delegate.decode(response, type); } } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(InputStream.class)) { byte[] body = response.body() != null ? Util.toByteArray(response.body().asInputStream()) : new byte[0]; return new ByteArrayInputStream(body); } else { return delegate.decode(response, type); } } }
@Override public Object decode(Response response, Type type) throws FeignException, IOException { Object object = delegate.decode(response, type); Preconditions.checkNotNull(object, "Unexpected null body", SafeArg.of("status", response.status())); return object; } }
@Override public Object decode(Response response, Type type) throws FeignException, IOException { Object object = delegate.decode(response, type); Preconditions.checkNotNull(object, "Unexpected null body", SafeArg.of("status", response.status())); return object; } }
@Test public void testUsesDelegateWithComplexHeader() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON)); when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE); Response response = Response.create(200, "OK", headers, new byte[0]); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, DELEGATE_RESPONSE); }
@Test public void testUsesDelegateWithNonTextContentType() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.APPLICATION_JSON)); when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE); Response response = Response.create(200, "OK", headers, new byte[0]); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, DELEGATE_RESPONSE); }
@Test public void testUsesDelegateWhenReturnTypeNotInputStream() throws Exception { String returned = "string"; when(delegate.decode(any(), any())).thenReturn(returned); Response response = Response.create(200, "OK", ImmutableMap.of(), returned, StandardCharsets.UTF_8); String decodedObject = (String) inputStreamDelegateDecoder.decode(response, String.class); assertEquals(returned, decodedObject); }
@Test public void testUsesStringDecoderWithTextPlain() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN)); Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, "text response"); verifyZeroInteractions(delegate); }
@Test public void testDecodesAsInputStream() throws Exception { String data = "data"; Response response = Response.create(200, "OK", ImmutableMap.of(), data, StandardCharsets.UTF_8); InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class); assertThat(new String(Util.toByteArray(decoded), StandardCharsets.UTF_8), is(data)); }
@Test public void throws_nullpointerexception_when_body_is_null() { Response response = Response.create(200, "OK", headers, null, StandardCharsets.UTF_8); Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class)) .isInstanceOf(NullPointerException.class) .hasLogMessage("Unexpected null body") .hasArgs(SafeArg.of("status", 200)); }
@Test public void throws_nullpointerexception_when_body_is_string_null() { Response response = Response.create(200, "OK", headers, "null", StandardCharsets.UTF_8); Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class)) .isInstanceOf(NullPointerException.class) .hasLogMessage("Unexpected null body") .hasArgs(SafeArg.of("status", 200)); }