/**
  * Determine the JSON encoding to use for the given content type.
  *
  * @param contentType the media type as requested by the caller
  * @return the JSON encoding to use (never {@code null})
  */
 protected JsonEncoding getJsonEncoding(MediaType contentType) {
   if (contentType != null && contentType.getCharset() != null) {
     Charset charset = contentType.getCharset();
     for (JsonEncoding encoding : JsonEncoding.values()) {
       if (charset.name().equals(encoding.getJavaName())) {
         return encoding;
       }
     }
   }
   return JsonEncoding.UTF8;
 }
 @Override
 protected void applyHeaders() {
   for (Map.Entry<String, List<String>> entry : getHeaders().entrySet()) {
     String headerName = entry.getKey();
     for (String headerValue : entry.getValue()) {
       this.response.addHeader(headerName, headerValue);
     }
   }
   MediaType contentType = getHeaders().getContentType();
   if (this.response.getContentType() == null && contentType != null) {
     this.response.setContentType(contentType.toString());
   }
   Charset charset = (contentType != null ? contentType.getCharset() : null);
   if (this.response.getCharacterEncoding() == null && charset != null) {
     this.response.setCharacterEncoding(charset.name());
   }
 }
 @Override
 protected HttpHeaders initHeaders() {
   HttpHeaders headers = new HttpHeaders();
   for (Enumeration<?> names = getServletRequest().getHeaderNames(); names.hasMoreElements(); ) {
     String name = (String) names.nextElement();
     for (Enumeration<?> values = getServletRequest().getHeaders(name);
         values.hasMoreElements(); ) {
       headers.add(name, (String) values.nextElement());
     }
   }
   MediaType contentType = headers.getContentType();
   if (contentType == null) {
     String requestContentType = getServletRequest().getContentType();
     if (StringUtils.hasLength(requestContentType)) {
       contentType = MediaType.parseMediaType(requestContentType);
       headers.setContentType(contentType);
     }
   }
   if (contentType != null && contentType.getCharset() == null) {
     String encoding = getServletRequest().getCharacterEncoding();
     if (StringUtils.hasLength(encoding)) {
       Charset charset = Charset.forName(encoding);
       Map<String, String> params = new LinkedCaseInsensitiveMap<>();
       params.putAll(contentType.getParameters());
       params.put("charset", charset.toString());
       headers.setContentType(
           new MediaType(contentType.getType(), contentType.getSubtype(), params));
     }
   }
   if (headers.getContentLength() == -1) {
     int contentLength = getServletRequest().getContentLength();
     if (contentLength != -1) {
       headers.setContentLength(contentLength);
     }
   }
   return headers;
 }
Ejemplo n.º 4
0
 private Optional<Charset> getCharset(MediaType mediaType) {
   return (mediaType != null ? Optional.ofNullable(mediaType.getCharset()) : Optional.empty());
 }
Ejemplo n.º 5
0
 private Charset getCharset(ClientHttpResponse response) {
   HttpHeaders headers = response.getHeaders();
   MediaType contentType = headers.getContentType();
   return contentType != null ? contentType.getCharset() : null;
 }