/** * 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; }
private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return this.defaultCharset; } } }
private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return getDefaultCharset(); } }
protected Long getContentLength(String s, MediaType contentType) { if ((contentType != null) && (contentType.getCharSet() != null)) { Charset charset = contentType.getCharSet(); try { return Long.valueOf(s.getBytes(charset.name()).length); } catch (UnsupportedEncodingException ex) { throw new InternalError(ex.getMessage()); } } return 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; }
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; }
/** * 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</code>) */ 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; }
/** * 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</code>) */ 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; }
/** * 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; }
private Charset getCharset(HttpHeaders headers) { if (headers != null && headers.getContentType() != null && headers.getContentType().getCharSet() != null) { return headers.getContentType().getCharSet(); } return DEFAULT_CHARSET; }
private Charset getCharset(HttpHeaders headers) { if (headers != null && headers.getContentType() != null && headers.getContentType().getCharSet() != null) { return headers.getContentType().getCharSet(); } return DEFAULT_CHARSET; }
public MultiValueMap<String, String> read(Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset; String body = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); String[] pairs = StringUtils.tokenizeToStringArray(body, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(URLDecoder.decode(pair, charset.name()), null); } else { String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } return result; }
@SuppressWarnings("rawtypes") protected String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType.getCharSet() != null) ? contentType .getCharSet() : DEFAULT_CHARSET; return FileCopyUtils.copyToString(new InputStreamReader(inputMessage .getBody(), charset)); }
protected Charset getCharset(ClientHttpResponse response) { HttpHeaders headers = response.getHeaders(); MediaType contentType = headers.getContentType(); return contentType != null ? contentType.getCharSet() : null; }
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } MediaType contentType = outputMessage.getHeaders().getContentType(); Charset charset = (contentType.getCharSet() != null) ? contentType .getCharSet() : DEFAULT_CHARSET; FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); }
@Override public JSOG read(Class<? extends JSOG> clazz, HttpInputMessage input) throws IOException, HttpMessageNotReadableException { HttpHeaders headers = input.getHeaders(); MediaType contentType = headers.getContentType(); Charset encoding = contentType.getCharSet(); if (encoding == null) { encoding = this.encoding; } // Read in the JSON String json = IOUtils.toString(input.getBody(), encoding.name()); // Parse the JSON and return a JSOG. try { return JSOG.parse(json); } catch (IOException e) { throw new HttpMessageNotReadableException("Unable to parse JSON.", e); } }
@Override protected SyndFeed readInternal(Class<? extends SyndFeed> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { SyndFeedInput feedInput = new SyndFeedInput(); MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset; if (contentType != null && contentType.getCharSet() != null) { charset = contentType.getCharSet(); } else { charset = DEFAULT_CHARSET; } try { Reader reader = new InputStreamReader(inputMessage.getBody(), charset); return feedInput.build(reader); } catch (FeedException ex) { throw new HttpMessageNotReadableException("Could not read SyndFeed: " + ex.getMessage(), ex); } }
@Override @SuppressWarnings("unchecked") protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { WireFeedInput feedInput = new WireFeedInput(); MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset; if (contentType != null && contentType.getCharSet() != null) { charset = contentType.getCharSet(); } else { charset = DEFAULT_CHARSET; } try { Reader reader = new InputStreamReader(inputMessage.getBody(), charset); return (T) feedInput.build(reader); } catch (FeedException ex) { throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex); } }
@Override public void write(JSOG jsog, MediaType type, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException { // If the outputContentType doesn't specify a charset, we need to set it Charset encoding = outputContentType.getCharSet(); if (encoding != null) { output.getHeaders().setContentType(outputContentType); } else { encoding = this.encoding; output.getHeaders().setContentType(new MediaType( outputContentType.getType(), outputContentType.getSubtype(), encoding)); } // Transform the JSOG to a byte array encoded in the proper encoding byte[] text = jsog.toString().getBytes(encoding); // Set the content length output.getHeaders().setContentLength(text.length); IOUtils.write(text, output.getBody()); } }
protected void writeMessage(final Message message, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { MediaType contentType = outputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; if (isHtml(contentType)) { String data = HtmlFormat.printToString(message); FileCopyUtils.copy(data.getBytes(charset), outputMessage.getBody()); } else if (isJson(contentType)) { String data = JsonFormat.printToString(message); FileCopyUtils.copy(data.getBytes(charset), outputMessage.getBody()); } else if (isText(contentType)) { String data = TextFormat.printToString(message); FileCopyUtils.copy(data.getBytes(charset), outputMessage.getBody()); } else if (isXml(contentType)) { String data = XmlFormat.printToString(message); FileCopyUtils.copy(data.getBytes(charset), outputMessage.getBody()); } else { FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody()); } }