List of usage examples for org.springframework.http HttpInputMessage getBody
InputStream getBody() throws IOException;
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 {//ww w . ja 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:org.gvnix.web.json.DataBinderMappingJackson2HttpMessageConverter.java
/** * Before call to {@link ObjectMapper#readValue(java.io.InputStream, Class)} * creates a {@link ServletRequestDataBinder} and put it to current Thread * in order to be used by the {@link DataBinderDeserializer}. * <p/>//from ww w. j a va 2 s .c o m * Ref: <a href= * "http://java.dzone.com/articles/java-thread-local-%E2%80%93-how-use">When * to use Thread Local?</a> * * @param javaType * @param inputMessage * @return */ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) { try { Object target = null; String objectName = null; // CRear el DataBinder con un target object en funcion del javaType, // ponerlo en el thread local Class<?> clazz = javaType.getRawClass(); if (Collection.class.isAssignableFrom(clazz)) { Class<?> contentClazz = javaType.getContentType().getRawClass(); target = new DataBinderList<Object>(contentClazz); objectName = "list"; } else if (Map.class.isAssignableFrom(clazz)) { // TODO Class<?> contentClazz = // javaType.getContentType().getRawClass(); target = CollectionFactory.createMap(clazz, 0); objectName = "map"; } else { target = BeanUtils.instantiateClass(clazz); objectName = "bean"; } WebDataBinder binder = new ServletRequestDataBinder(target, objectName); binder.setConversionService(this.conversionService); binder.setAutoGrowNestedPaths(true); binder.setValidator(validator); ThreadLocalUtil.setThreadVariable(BindingResult.MODEL_KEY_PREFIX.concat("JSON_DataBinder"), binder); Object value = getObjectMapper().readValue(inputMessage.getBody(), javaType); return value; } catch (IOException ex) { throw new HttpMessageNotReadableException("Could not read JSON: ".concat(ex.getMessage()), ex); } }
From source file:com.github.hateoas.forms.spring.xhtml.XhtmlResourceMessageConverter.java
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { InputStream is;/*from w w w . j a v a 2 s.c o 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.katsu.springframework.http.converter.json.JsonHttpMessageConverter.java
@Override protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return deserialize(inputMessage.getBody(), clazz); }
From source file:com.qcadoo.view.internal.JsonHttpMessageConverter.java
@Override protected JSONObject readInternal(final Class<? extends JSONObject> clazz, final HttpInputMessage inputMessage) throws IOException { String body = IOUtils.toString(inputMessage.getBody(), CHARSET.name()); try {/* w ww . j a v a2 s . c o m*/ return new JSONObject(body); } catch (JSONException e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } }
From source file:com.qcadoo.view.internal.JsonMapperHttpMessageConverter.java
@Override protected Object readInternal(final Class<?> clazz, final HttpInputMessage inputMessage) throws IOException { String body = IOUtils.toString(inputMessage.getBody(), CHARSET.name()); return mapper.readValue(body, clazz); }
From source file:com.sunney.eweb.config.MappingFastjson2HttpMessageConverter.java
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { @SuppressWarnings("resource") ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i;/* w w w. ja va2s . co m*/ while ((i = inputMessage.getBody().read()) != -1) { baos.write(i); } return JSON.parseArray(baos.toString(), clazz); }
From source file:org.molgenis.util.GsonHttpMessageConverter.java
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Reader json = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())); try {/* w w w.j a v a 2s . c o m*/ Type typeOfT = getType(); if (LOG.isTraceEnabled()) { String jsonStr = IOUtils.toString(json); LOG.trace("Json request:\n" + jsonStr); if (typeOfT != null) { return this.gson.fromJson(jsonStr, typeOfT); } return this.gson.fromJson(jsonStr, clazz); } else { if (typeOfT != null) { return this.gson.fromJson(json, typeOfT); } return this.gson.fromJson(json, clazz); } } catch (JsonSyntaxException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (JsonIOException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (JsonParseException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } finally { IOUtils.closeQuietly(json); } }
From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java
/** * Return the {@link HttpTunnelPayload} for the given message or {@code null} if there * is no payload.//w w w .ja va 2 s . co m * @param message the HTTP message * @return the payload or {@code null} * @throws IOException in case of I/O errors */ public static HttpTunnelPayload get(HttpInputMessage message) throws IOException { long length = message.getHeaders().getContentLength(); if (length <= 0) { return null; } String seqHeader = message.getHeaders().getFirst(SEQ_HEADER); Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header"); ReadableByteChannel body = Channels.newChannel(message.getBody()); ByteBuffer payload = ByteBuffer.allocate((int) length); while (payload.hasRemaining()) { body.read(payload); } body.close(); payload.flip(); return new HttpTunnelPayload(Long.valueOf(seqHeader), payload); }
From source file:org.springframework.data.web.ProjectingJackson2HttpMessageConverter.java
@Override public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return projectionFactory.createProjection(ResolvableType.forType(type).getRawClass(), inputMessage.getBody()); }