List of usage examples for org.springframework.http HttpInputMessage getHeaders
HttpHeaders getHeaders();
From source file:com.yang.oa.commons.exception.RestExceptionHandler.java
@SuppressWarnings("unchecked") private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); }/* www .ja v a 2 s. c o m*/ MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> bodyType = body.getClass(); List<HttpMessageConverter<?>> converters = this.allMessageConverters; if (converters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : converters) { if (messageConverter.canWrite(bodyType, acceptedMediaType)) { messageConverter.write(body, acceptedMediaType, outputMessage); //return empty model and view to short circuit the iteration and to let //Spring know that we've rendered the view ourselves: return new ModelAndView(); } } } } return null; }
From source file:com.novation.eligibility.rest.spring.web.servlet.handler.RestExceptionHandler.java
@SuppressWarnings("unchecked") private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); }/*w w w . j a va2s. c om*/ MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> bodyType = body.getClass(); List<HttpMessageConverter<?>> converters = this.allMessageConverters; if (converters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : converters) { if (messageConverter.canWrite(bodyType, acceptedMediaType)) { messageConverter.write(body, acceptedMediaType, outputMessage); //return empty model and view to short circuit the iteration and to let //Spring know that we've rendered the view ourselves: return new ModelAndView(); } } } } if (logger.isWarnEnabled()) { logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and " + acceptedMediaTypes); } return null; }
From source file:com.oolong.platform.web.error.RestExceptionHandler.java
@SuppressWarnings("unchecked") private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); }/*ww w. j a v a2 s .co m*/ MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> bodyType = body.getClass(); List<HttpMessageConverter<?>> converters = this.allMessageConverters; if (converters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : converters) { if (messageConverter.canWrite(bodyType, acceptedMediaType)) { messageConverter.write(body, acceptedMediaType, outputMessage); // return empty model and view to short circuit the // iteration and to let // Spring know that we've rendered the view ourselves: return new ModelAndView(); } } } } if (logger.isWarnEnabled()) { logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and " + acceptedMediaTypes); } return null; }
From source file:org.jlot.web.api.error.RestExceptionHandler.java
@SuppressWarnings({ "unchecked", "rawtypes", "resource" }) private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest) throws ServletException, IOException { HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); }/*from w w w . j a va 2 s.com*/ MediaType.sortByQualityValue(acceptedMediaTypes); HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); Class<?> bodyType = body.getClass(); List<HttpMessageConverter<?>> converters = this.allMessageConverters; if (converters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : converters) { if (messageConverter.canWrite(bodyType, acceptedMediaType)) { messageConverter.write(body, acceptedMediaType, outputMessage); // return empty model and view to short circuit the // iteration and to let // Spring know that we've rendered the view ourselves: return new ModelAndView(); } } } } if (logger.isWarnEnabled()) { logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and " + acceptedMediaTypes); } return null; }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testRead() throws Exception { // Setup// www . j a va2s . co m JSOG expected = new JSOG("foobar"); MediaType contentType = JSON_CONTENT_TYPE; HttpHeaders headers = createMock(HttpHeaders.class); expect(headers.getContentType()).andReturn(contentType); HttpInputMessage message = createMock(HttpInputMessage.class); expect(message.getHeaders()).andReturn(headers); expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1"))); // Execute replay(headers, message); JSOG actual = instance.read(JSOG.class, message); // Verify verify(message); assertEquals(expected, actual); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testReadObject() throws Exception { // Setup/*from w w w.ja va 2 s.c o m*/ JSOG expected = JSOG.object("foo", "bar"); MediaType contentType = JSON_CONTENT_TYPE; HttpHeaders headers = createMock(HttpHeaders.class); expect(headers.getContentType()).andReturn(contentType); HttpInputMessage message = createMock(HttpInputMessage.class); expect(message.getHeaders()).andReturn(headers); expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1"))); // Execute replay(headers, message); JSOG actual = instance.read(JSOG.class, message); // Verify verify(message); assertEquals(expected, actual); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testReadArray() throws Exception { // Setup/*from w w w .j ava2s .c om*/ JSOG expected = JSOG.array("foo", "bar"); MediaType contentType = JSON_CONTENT_TYPE; HttpHeaders headers = createMock(HttpHeaders.class); expect(headers.getContentType()).andReturn(contentType); HttpInputMessage message = createMock(HttpInputMessage.class); expect(message.getHeaders()).andReturn(headers); expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1"))); // Execute replay(headers, message); JSOG actual = instance.read(JSOG.class, message); // Verify verify(message); assertEquals(expected, actual); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test(expected = HttpMessageNotReadableException.class) public void testReadBad() throws Exception { // Setup//from w w w .j a va2 s.com MediaType contentType = JSON_CONTENT_TYPE; HttpHeaders headers = createMock(HttpHeaders.class); expect(headers.getContentType()).andReturn(contentType); HttpInputMessage message = createMock(HttpInputMessage.class); expect(message.getHeaders()).andReturn(headers); expect(message.getBody()).andReturn(new ByteArrayInputStream("[".toString().getBytes("ISO-8859-1"))); // Execute replay(headers, message); try { instance.read(JSOG.class, message); } finally { // Verify verify(message); } }
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 .ja v a 2 s .c o 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.j a v a2s . c om 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); }