@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(); } // 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 { if (bodyType.equals(InputStream.class)) { try { template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } } else { delegate.encode(object, bodyType, template); } } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(InputStream.class)) { try { template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } } else { delegate.encode(object, bodyType, template); } } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(InputStream.class)) { try { template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } } else { delegate.encode(object, bodyType, template); } } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(InputStream.class)) { try { template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } } else { delegate.encode(object, bodyType, template); } } }
@Override protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable, Map<String, Object> variables) { Map<String, Object> formVariables = new LinkedHashMap<String, Object>(); for (Entry<String, Object> entry : variables.entrySet()) { if (metadata.formParams().contains(entry.getKey())) { formVariables.put(entry.getKey(), entry.getValue()); } } try { encoder.encode(formVariables, Encoder.MAP_STRING_WILDCARD, mutable); } catch (EncodeException e) { throw e; } catch (RuntimeException e) { throw new EncodeException(e.getMessage(), e); } return super.resolve(argv, mutable, variables); } }
@Override protected RequestTemplate resolve( final Object[] argv, final RequestTemplate mutable, final Map<String, Object> variables) { final Map<String, Object> formVariables = new HashMap<>(); for (final Map.Entry<String, Object> entry : variables.entrySet()) { if (metadata.formParams().contains(entry.getKey())) { formVariables.put(entry.getKey(), entry.getValue()); } } try { encoder.encode(formVariables, Encoder.MAP_STRING_WILDCARD, mutable); } catch (final EncodeException encodeException) { throw encodeException; } catch (final RuntimeException unexpectedException) { throw new EncodeException(unexpectedException.getMessage(), unexpectedException); } return super.resolve(argv, mutable, variables); } }
@Override protected void write (Output output, String key, Object value) throws EncodeException { val fake = new RequestTemplate(); delegate.encode(value, value.getClass(), fake); val bytes = fake.requestBody().asBytes(); val string = new String(bytes, output.getCharset()).replaceAll("\n", ""); parameterWriter.write(output, key, string); } }
@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 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 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 @SuppressWarnings("unchecked") public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException { String contentTypeValue = getContentTypeValue(template.headers()); val contentType = ContentType.of(contentTypeValue); if (!processors.containsKey(contentType)) { delegate.encode(object, bodyType, template); return; } Map<String, Object> data; if (MAP_STRING_WILDCARD.equals(bodyType)) { data = (Map<String, Object>) object; } else if (isUserPojo(object)) { data = toMap(object); } else { delegate.encode(object, bodyType, template); return; } val charset = getCharset(contentTypeValue); processors.get(contentType).process(template, charset, data); }
@Test public void doesNotUseDelegateWhenContentTypeHeaderHasArbitraryCase() { headers.put("Content-TYPE", Arrays.asList(MediaType.TEXT_PLAIN)); requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verifyZeroInteractions(delegate); } }
@Test public void doesNotUseDelegateWhenContentTypeIsTextPlain() { headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.TEXT_PLAIN)); requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verifyZeroInteractions(delegate); }