List of usage examples for org.springframework.http MediaType getCharset
@Nullable
public Charset getCharset()
From source file:com.github.jmnarloch.spring.cloud.feign.VndErrorDecoder.java
/** * Retrieves the response charset./*from w ww . ja va 2s .c o m*/ * @param headers the http headers * @return the response charset */ private Charset getCharset(HttpHeaders headers) { final MediaType contentType = headers.getContentType(); return contentType != null ? contentType.getCharSet() : null; }
From source file:com.p5solutions.core.json.JsonHttpMessageConverter.java
/** * Write internal./*from ww w . jav a2 s. c o m*/ * * @param t * the t * @param outputMessage * the output message * @throws IOException * Signals that an I/O exception has occurred. * @throws HttpMessageNotWritableException * the http message not writable exception * @see org.springframework.http.converter.AbstractHttpMessageConverter#writeInternal * (java.lang.Object, org.springframework.http.HttpOutputMessage) */ @Override protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { HttpHeaders headers = outputMessage.getHeaders(); MediaType mediaType = headers.getContentType(); Charset charset = mediaType.getCharSet(); OutputStream output = outputMessage.getBody(); serializer.serialize(t, output, charset); }
From source file:com.p5solutions.core.json.JsonHttpMessageConverter.java
/** * Deserialize.//from w ww . j ava 2s . c o m * * @param clazz * the clazz * @param bindObject * the bind object * @param inputMessage * the input message * @return the object * @throws IOException * Signals that an I/O exception has occurred. */ @SuppressWarnings("unchecked") protected Object deserialize(Class<?> clazz, Object bindObject, WebRequest webRequest, WebDataBinder binder, HttpInputMessage inputMessage) throws IOException { /// HttpHeaders headers = inputMessage.getHeaders(); MediaType mediaType = headers.getContentType(); Charset charset = mediaType.getCharSet(); InputStream input = inputMessage.getBody(); Object value = deserializer.deserialize((Class<Object>) clazz, bindObject, webRequest, binder, input, charset); return value; }
From source file:com.fiadot.springjsoncrypt.json.CryptMappingJacson2HttpMessageConverter.java
/** * 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}) *///from w ww . ja v a 2 s . com 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; }
From source file:com.httpMessageConvert.FormHttpMessageConverter.java
public Object read(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset; String body = StreamUtils.copyToString(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 {//from w w w .j a v a 2 s . co m String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } Map<String, String> map = result.toSingleValueMap(); String json = JSONObject.toJSONString(map); JavaType javaType = getJavaType(clazz, null); ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(json, javaType); }
From source file:com.github.hateoas.forms.spring.xhtml.XhtmlResourceMessageConverter.java
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { InputStream is;/* w w w. ja v a2 s . co m*/ if (inputMessage instanceof ServletServerHttpRequest) { // this is necessary to support HiddenHttpMethodFilter // thanks to https://www.w3.org/html/wg/tracker/issues/195 // but see http://dev.w3.org/html5/decision-policy/html5-2014-plan.html#issues // and http://cameronjones.github.io/form-http-extensions/index.html // and http://www.w3.org/TR/form-http-extensions/ // TODO recognize this more safely or make the filter mandatory MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset; ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) inputMessage; HttpServletRequest servletRequest = servletServerHttpRequest.getServletRequest(); is = getBodyFromServletRequestParameters(servletRequest, charset.displayName(Locale.US)); } else { is = inputMessage.getBody(); } return readRequestBody(clazz, is, charset); }
From source file:com.httpMessageConvert.FormHttpMessageConverter.java
private void writeForm(MultiValueMap<String, String> form, MediaType contentType, HttpOutputMessage outputMessage) throws IOException { Charset charset;/* www .j a v a 2s . co m*/ if (contentType != null) { outputMessage.getHeaders().setContentType(contentType); charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset; } else { outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); charset = this.charset; } StringBuilder builder = new StringBuilder(); for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) { String name = nameIterator.next(); for (Iterator<String> valueIterator = form.get(name).iterator(); valueIterator.hasNext();) { String value = valueIterator.next(); builder.append(URLEncoder.encode(name, charset.name())); if (value != null) { builder.append('='); builder.append(URLEncoder.encode(value, charset.name())); if (valueIterator.hasNext()) { builder.append('&'); } } } if (nameIterator.hasNext()) { builder.append('&'); } } byte[] bytes = builder.toString().getBytes(charset.name()); outputMessage.getHeaders().setContentLength(bytes.length); StreamUtils.copy(bytes, outputMessage.getBody()); }
From source file:org.alfresco.rest.framework.webscripts.ResourceWebScriptPut.java
/** * Returns the basic content info from the request. * @param req WebScriptRequest//from w w w. j a va 2 s . c o m * @return BasicContentInfo */ private BasicContentInfo getContentInfo(WebScriptRequest req) { String encoding = "UTF-8"; String contentType = MimetypeMap.MIMETYPE_BINARY; if (StringUtils.isNotEmpty(req.getContentType())) { MediaType media = MediaType.parseMediaType(req.getContentType()); contentType = media.getType() + '/' + media.getSubtype(); if (media.getCharSet() != null) { encoding = media.getCharSet().toString(); } } return new ContentInfoImpl(contentType, encoding, -1, Locale.getDefault()); }
From source file:org.springframework.cloud.dataflow.shell.command.HttpCommands.java
@CliCommand(value = { POST_HTTPSOURCE }, help = "POST data to http endpoint") public String postHttp(@CliOption(mandatory = false, key = { "", "target" }, help = "the location to post to", unspecifiedDefaultValue = "http://localhost:9000") String target, @CliOption(mandatory = false, key = "data", help = "the text payload to post. exclusive with file. embedded double quotes are not supported if next to a space character") String data, @CliOption(mandatory = false, key = "file", help = "filename to read data from. exclusive with data") File file, @CliOption(mandatory = false, key = "contentType", help = "the content-type to use. file is also read using the specified charset", unspecifiedDefaultValue = DEFAULT_MEDIA_TYPE) MediaType mediaType) throws IOException { Assert.isTrue(file != null || data != null, "One of 'file' or 'data' must be set"); Assert.isTrue(file == null || data == null, "Only one of 'file' or 'data' must be set"); if (mediaType.getCharSet() == null) { mediaType = new MediaType(mediaType, Collections.singletonMap("charset", Charset.defaultCharset().toString())); }// ww w . java2 s. com if (file != null) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), mediaType.getCharSet()); data = FileCopyUtils.copyToString(isr); } final StringBuilder buffer = new StringBuilder(); URI requestURI = URI.create(target); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(mediaType); final HttpEntity<String> request = new HttpEntity<String>(data, headers); try { outputRequest("POST", requestURI, mediaType, data, buffer); ResponseEntity<String> response = createRestTemplate(buffer).postForEntity(requestURI, request, String.class); outputResponse(response, buffer); if (!response.getStatusCode().is2xxSuccessful()) { buffer.append(OsUtils.LINE_SEPARATOR) .append(String.format("Error sending data '%s' to '%s'", data, target)); } return buffer.toString(); } catch (ResourceAccessException e) { return String.format(buffer.toString() + "Failed to access http endpoint %s", target); } catch (Exception e) { return String.format(buffer.toString() + "Failed to send data to http endpoint %s", target); } }
From source file:org.springframework.http.codec.EncoderHttpMessageWriter.java
private static MediaType addDefaultCharset(MediaType main, @Nullable MediaType defaultType) { if (main.getCharset() == null && defaultType != null && defaultType.getCharset() != null) { return new MediaType(main, defaultType.getCharset()); }//from w w w. j ava 2 s. c o m return main; }