@Override public Object decode(Response response, Type type) throws IOException, FeignException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(response.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } // In the case of multiple content types, or an unknown content type, we'll use the delegate instead. if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").startsWith(MediaType.TEXT_PLAIN)) { return stringDecoder.decode(response, type); } return delegate.decode(response, type); } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(response.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } // In the case of multiple content types, or an unknown content type, we'll use the delegate instead. if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").startsWith(MediaType.TEXT_PLAIN)) { return stringDecoder.decode(response, type); } return delegate.decode(response, type); } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } // In the case of multiple content types, or an unknown content type, we'll use the delegate instead. if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").equals(MediaType.TEXT_PLAIN)) { defaultEncoder.encode(object, bodyType, template); } else { delegate.encode(object, bodyType, template); } } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } // In the case of multiple content types, or an unknown content type, we'll use the delegate instead. if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").equals(MediaType.TEXT_PLAIN)) { defaultEncoder.encode(object, bodyType, template); } else { delegate.encode(object, bodyType, template); } } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (!contentTypes.contains(MIME_TYPE)) { delegate.encode(object, bodyType, template); return; } try { JavaType javaType = cborObjectMapper.getTypeFactory().constructType(bodyType); template.body(cborObjectMapper.writerFor(javaType).writeValueAsBytes(object), StandardCharsets.UTF_8); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(response.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").startsWith(ConjureCborDelegateEncoder.MIME_TYPE)) { // some sillyness to test whether the input stram is empty // if it's empty, we want to return null rather than having jackson throw int pushbackBufferSize = 1; PushbackInputStream pushbackInputStream = new PushbackInputStream( response.body().asInputStream(), pushbackBufferSize); int firstByte = pushbackInputStream.read(); if (firstByte == -1) { return null; // we don't have any data in the stream } // put the byte back pushbackInputStream.unread(firstByte); return cborObjectMapper.readValue(pushbackInputStream, cborObjectMapper.constructType(type)); } else { return delegate.decode(response, type); } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (!contentTypes.contains(MIME_TYPE)) { delegate.encode(object, bodyType, template); return; } try { JavaType javaType = cborObjectMapper.getTypeFactory().constructType(bodyType); template.body(cborObjectMapper.writerFor(javaType).writeValueAsBytes(object), StandardCharsets.UTF_8); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(response.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").startsWith(ConjureCborDelegateEncoder.MIME_TYPE)) { // some sillyness to test whether the input stram is empty // if it's empty, we want to return null rather than having jackson throw int pushbackBufferSize = 1; PushbackInputStream pushbackInputStream = new PushbackInputStream( response.body().asInputStream(), pushbackBufferSize); int firstByte = pushbackInputStream.read(); if (firstByte == -1) { return null; // we don't have any data in the stream } // put the byte back pushbackInputStream.unread(firstByte); return cborObjectMapper.readValue(pushbackInputStream, cborObjectMapper.constructType(type)); } else { return delegate.decode(response, type); } }
@Test public void caseInsensitiveGetReturnsNullForNotExistingHeader() { assertThat(HeaderAccessUtils.caseInsensitiveGet(TEST_HEADERS_MAP, "invalid"), is(nullValue())); }
@Test public void caseInsensitiveGetReturnsAllExistingHeaders() { assertThat(HeaderAccessUtils.caseInsensitiveGet(TEST_HEADERS_MAP, "HeADER"), contains("value1", "value2", "value3", "value4", "value5")); } }