@Override
 protected void writeInternal(String t, HttpOutputMessage outputMessage)
     throws IOException, HttpMessageNotWritableException {
   MediaType contentType = outputMessage.getHeaders().getContentType();
   Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
   FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), charset));
 }
 @Override
 protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage)
     throws IOException, HttpMessageNotReadableException {
   MediaType contentType = inputMessage.getHeaders().getContentType();
   Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
   return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
 }
 private JsonEncoding getEncoding(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 Long getContentLength(String s, MediaType contentType) {
   if (contentType != null && contentType.getCharSet() != null) {
     Charset charset = contentType.getCharSet();
     try {
       return (long) s.getBytes(charset.name()).length;
     } catch (UnsupportedEncodingException ex) {
       throw new InternalError(ex.getMessage());
     }
   } else {
     return null;
   }
 }
 @Override
 public void write(JSONEntity entity, MediaType contentType, HttpOutputMessage outputMessage)
     throws IOException, HttpMessageNotWritableException {
   // First write the entity to a JSON string
   try {
     HttpHeaders headers = outputMessage.getHeaders();
     if (headers.getContentType() == null) {
       if (contentType == null
           || contentType.isWildcardType()
           || contentType.isWildcardSubtype()) {
         contentType = MediaType.APPLICATION_JSON;
       }
       if (contentType != null) {
         headers.setContentType(contentType);
       }
     }
     String jsonString = EntityFactory.createJSONStringForEntity(entity);
     long length =
         JSONEntityHttpMessageConverter.writeToStream(
             jsonString, outputMessage.getBody(), contentType.getCharSet());
     if (headers.getContentLength() == -1) {
       headers.setContentLength(length);
     }
   } catch (JSONObjectAdapterException e) {
     throw new HttpMessageNotWritableException(e.getMessage());
   }
 }
 @SuppressWarnings("unchecked")
 public void doWithRequest(ClientHttpRequest request) throws IOException {
   if (responseType != null) {
     List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
     for (HttpMessageConverter<?> messageConverter : getMessageConverters()) {
       if (messageConverter.canRead(responseType, null)) {
         List<MediaType> supportedMediaTypes = messageConverter.getSupportedMediaTypes();
         for (MediaType supportedMediaType : supportedMediaTypes) {
           if (supportedMediaType.getCharSet() != null) {
             supportedMediaType =
                 new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype());
           }
           allSupportedMediaTypes.add(supportedMediaType);
         }
       }
     }
     if (!allSupportedMediaTypes.isEmpty()) {
       MediaType.sortBySpecificity(allSupportedMediaTypes);
       if (logger.isDebugEnabled()) {
         logger.debug("Setting request Accept header to " + allSupportedMediaTypes);
       }
       request.getHeaders().setAccept(allSupportedMediaTypes);
     }
   }
 }
 private String determineEncoding(String contentTypeHeader, String defaultEncoding) {
   if (!StringUtils.hasText(contentTypeHeader)) {
     return defaultEncoding;
   }
   MediaType contentType = MediaType.parseMediaType(contentTypeHeader);
   Charset charset = contentType.getCharSet();
   return (charset != null ? charset.name() : defaultEncoding);
 }
 @Test
 public void parseQuotedCharset() {
   String s = "application/xml;charset=\"utf-8\"";
   MediaType mediaType = MediaType.parseMediaType(s);
   assertEquals("Invalid type", "application", mediaType.getType());
   assertEquals("Invalid subtype", "xml", mediaType.getSubtype());
   assertEquals("Invalid charset", Charset.forName("UTF-8"), mediaType.getCharSet());
 }
 @Test
 public void parseCharset() throws Exception {
   String s = "text/html; charset=iso-8859-1";
   MediaType mediaType = MediaType.parseMediaType(s);
   assertEquals("Invalid type", "text", mediaType.getType());
   assertEquals("Invalid subtype", "html", mediaType.getSubtype());
   assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mediaType.getCharSet());
 }
  @SuppressWarnings("resource")
  public void writeJsonResponse(
      HttpServletResponse response,
      Object responseObject,
      Class<?> jsonView,
      boolean streamResponse,
      boolean isMultipart)
      throws IOException, JsonGenerationException, JsonMappingException {

    ObjectMapper objectMapper = configurationService.getJsonHandler().getMapper();

    if (isMultipart) {
      response.setContentType(RouterController.TEXT_HTML.toString());
      response.setCharacterEncoding(RouterController.TEXT_HTML.getCharSet().name());

      ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
      bos.write("<html><body><textarea>".getBytes(ExtDirectSpringUtil.UTF8_CHARSET));

      String responseJson;
      if (jsonView == null) {
        responseJson = objectMapper.writeValueAsString(responseObject);
      } else {
        responseJson = objectMapper.writerWithView(jsonView).writeValueAsString(responseObject);
      }

      responseJson = responseJson.replace("&quot;", "\\&quot;");
      bos.write(responseJson.getBytes(ExtDirectSpringUtil.UTF8_CHARSET));
      bos.write("</textarea></body></html>".getBytes(ExtDirectSpringUtil.UTF8_CHARSET));

      response.setContentLength(bos.size());
      FileCopyUtils.copy(bos.toByteArray(), response.getOutputStream());
    } else {

      response.setContentType(APPLICATION_JSON.toString());
      response.setCharacterEncoding(APPLICATION_JSON.getCharSet().name());

      ServletOutputStream outputStream = response.getOutputStream();

      if (!streamResponse) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        JsonGenerator jsonGenerator =
            objectMapper.getFactory().createJsonGenerator(bos, JsonEncoding.UTF8);

        if (jsonView == null) {
          objectMapper.writeValue(jsonGenerator, responseObject);
        } else {
          objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject);
        }

        response.setContentLength(bos.size());
        outputStream.write(bos.toByteArray());
        jsonGenerator.close();
      } else {
        JsonGenerator jsonGenerator =
            objectMapper.getFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
        if (jsonView == null) {
          objectMapper.writeValue(jsonGenerator, responseObject);
        } else {
          objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject);
        }
        jsonGenerator.close();
      }

      outputStream.flush();
    }
  }
  public SSEWriter(HttpServletResponse response) {
    this.response = response;

    response.setContentType(EVENT_STREAM.toString());
    response.setCharacterEncoding(EVENT_STREAM.getCharSet().name());
  }
 private Charset getCharset(ClientHttpResponse response) {
   HttpHeaders headers = response.getHeaders();
   MediaType contentType = headers.getContentType();
   return contentType != null ? contentType.getCharSet() : null;
 }