Java Code Examples for org.springframework.http.converter.HttpMessageNotReadableException

The following are top voted examples for showing how to use org.springframework.http.converter.HttpMessageNotReadableException. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: cf-java-client-sap   File: LoggregatorHttpMessageConverter.java   Source Code and License 7 votes vote down vote up
@Override
protected ApplicationLogs readInternal(Class<? extends ApplicationLogs> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    String boundary = getMessageBoundary(inputMessage);

    Multipart multipart = new Multipart(inputMessage.getBody(), boundary);

    ApplicationLogs logs = new ApplicationLogs();

    Multipart.Part part;
    while ((part = multipart.nextPart()) != null) {
        ApplicationLog log = messageParser.parseMessage(part.getContent());
        logs.add(log);
    }

    return logs;
}
 
Example 2
Project: solo-spring   File: HttpPutBodyFilter.java   Source Code and License 6 votes vote down vote up
@Override
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 = StreamUtils.copyToString(inputMessage.getBody(), charset);

	MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>();
	result.add("body", body);
	return result;
}
 
Example 3
Project: teamcity-hashicorp-vault-plugin   File: AbstractJackson2HttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
	try {
		if (inputMessage instanceof MappingJacksonInputMessage) {
			Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
			if (deserializationView != null) {
				return this.objectMapper.readerWithView(deserializationView).withType(javaType).
						readValue(inputMessage.getBody());
			}
		}
		return this.objectMapper.readValue(inputMessage.getBody(), javaType);
	}
	catch (IOException ex) {
		throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
	}
}
 
Example 4
Project: lams   File: AbstractWireFeedHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@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);
	}
}
 
Example 5
Project: lams   File: MarshallingHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
	Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
	try {
		Object result = this.unmarshaller.unmarshal(source);
		if (!clazz.isInstance(result)) {
			throw new TypeMismatchException(result, clazz);
		}
		return result;
	}
	catch (UnmarshallingFailureException ex) {
		throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);
	}
}
 
Example 6
Project: lams   File: SourceHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class.equals(clazz)) {
		return (T) readDOMSource(body);
	}
	else if (SAXSource.class.equals(clazz)) {
		return (T) readSAXSource(body);
	}
	else if (StAXSource.class.equals(clazz)) {
		return (T) readStAXSource(body);
	}
	else if (StreamSource.class.equals(clazz) || Source.class.equals(clazz)) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageConversionException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
	}
}
 
Example 7
Project: lams   File: SourceHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
private Source readStAXSource(InputStream body) {
	try {
		XMLInputFactory inputFactory = XMLInputFactory.newFactory();
		inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
		if (!isProcessExternalEntities()) {
			inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
		}
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
		return new StAXSource(streamReader);
	}
	catch (XMLStreamException ex) {
		throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
	}
}
 
Example 8
Project: OpenLRW   File: XAPIExceptionHandlerAdvice.java   Source Code and License 6 votes vote down vote up
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public XAPIErrorInfo handleHttpMessageNotReadableException(final HttpServletRequest request, HttpMessageNotReadableException e) {
    if (e.getCause() instanceof UnrecognizedPropertyException) {
        return this.handleUnrecognizedPropertyException(request, (UnrecognizedPropertyException)e.getCause());
    } else {
        XAPIErrorInfo result;
        if (e.getCause() instanceof JsonProcessingException) {
            final JsonProcessingException jpe = (JsonProcessingException)e.getCause();
            result = new XAPIErrorInfo(HttpStatus.BAD_REQUEST, request, jpe.getOriginalMessage());
        } else {
            result = new XAPIErrorInfo(HttpStatus.BAD_REQUEST, request, e);
        }
        this.logException(e);
        this.logError(result);
        return result;
    }
}
 
Example 9
Project: FastBootWeixin   File: WxMediaResourceMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
    protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {

        WxMediaResource wxMediaResource = new WxMediaResource(inputMessage);
        if (wxMediaResource.isUrlMedia() && !clazz.isAssignableFrom(WxMediaResource.class)) {
            throw new WxApiException("不支持的返回类型,接口返回了url");
        }
        if (InputStreamResource.class == clazz) {
            return new InputStreamResource(wxMediaResource.getInputStream());
        } else if (clazz.isAssignableFrom(WxMediaResource.class)) {
            return wxMediaResource;
        } else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
            return new ByteArrayResource(wxMediaResource.getBody());
        } else if (clazz.isAssignableFrom(FileSystemResource.class)) {
            return new FileSystemResource(wxMediaResource.getFile());
        }
//		else if (clazz.isAssignableFrom(File.class)) {
//			return wxMediaResource.getFile();
//		}
        throw new WxApiException("不支持的返回类型");
    }
 
Example 10
Project: springboot-scala-withswagger   File: ProtostuffHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {

    MediaType contentType = inputMessage.getHeaders().getContentType();
    if (MEDIA_TYPE.isCompatibleWith(contentType)) {
        final Schema<?> schema = getSchema(clazz);
        final Object value = schema.newMessage();

        try (final InputStream stream = inputMessage.getBody()) {
            ProtobufIOUtil.mergeFrom(stream, value, (Schema<Object>) schema);
            return value;
        }
    }

    throw new HttpMessageNotReadableException(
            "Unrecognized HTTP media type " + inputMessage.getHeaders().getContentType().getType() + ".");
}
 
Example 11
Project: hsweb-framework   File: FastJsonGenericHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (; ; ) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }
    byte[] bytes = baos.toByteArray();
    return readByBytes(type, bytes);
}
 
Example 12
Project: hsweb-framework   File: FastJsonHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException,
        HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (; ; ) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }
    byte[] bytes = baos.toByteArray();
    return readByBytes(clazz, bytes);
}
 
Example 13
Project: spring4-understanding   File: RequestResponseBodyMethodProcessor.java   Source Code and License 6 votes vote down vote up
@Override
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam,
		Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

	HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
	ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

	Object arg = readWithMessageConverters(inputMessage, methodParam, paramType);
	if (arg == null) {
		if (methodParam.getParameterAnnotation(RequestBody.class).required()) {
			throw new HttpMessageNotReadableException("Required request body is missing: " +
					methodParam.getMethod().toGenericString());
		}
	}
	return arg;
}
 
Example 14
Project: spring4-understanding   File: AbstractJackson2HttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
	try {
		if (inputMessage instanceof MappingJacksonInputMessage) {
			Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
			if (deserializationView != null) {
				return this.objectMapper.readerWithView(deserializationView).withType(javaType).
						readValue(inputMessage.getBody());
			}
		}
		return this.objectMapper.readValue(inputMessage.getBody(), javaType);
	}
	catch (IOException ex) {
		throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
	}
}
 
Example 15
Project: spring4-understanding   File: AbstractWireFeedHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@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 =
			(contentType != null && contentType.getCharSet() != null? contentType.getCharSet() : 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);
	}
}
 
Example 16
Project: spring4-understanding   File: SourceHttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class == clazz) {
		return (T) readDOMSource(body);
	}
	else if (SAXSource.class == clazz) {
		return (T) readSAXSource(body);
	}
	else if (StAXSource.class == clazz) {
		return (T) readStAXSource(body);
	}
	else if (StreamSource.class == clazz || Source.class == clazz) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageConversionException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
	}
}
 
Example 17
Project: spring4-understanding   File: Jaxb2RootElementHttpMessageConverterTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<rootElement><external>&lol9;</external></rootElement>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	this.thrown.expect(HttpMessageNotReadableException.class);
	this.thrown.expectMessage("DOCTYPE");
	this.converter.read(RootElement.class, inputMessage);
}
 
Example 18
Project: spring4-understanding   File: MarshallingHttpMessageConverterTests.java   Source Code and License 6 votes vote down vote up
@Test
public void readWithMarshallingFailureException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
	UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex);

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
	converter.setUnmarshaller(unmarshaller);

	try {
		converter.read(Object.class, inputMessage);
		fail("HttpMessageNotReadableException should be thrown");
	}
	catch (HttpMessageNotReadableException e) {
		assertTrue("Invalid exception hierarchy", e.getCause() == ex);
	}
}
 
Example 19
Project: spring4-understanding   File: SourceHttpMessageConverterTests.java   Source Code and License 6 votes vote down vote up
@Test
public void readDomSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));

	this.thrown.expect(HttpMessageNotReadableException.class);
	this.thrown.expectMessage("DOCTYPE");

	this.converter.read(DOMSource.class, inputMessage);
}
 
Example 20
Project: spring4-understanding   File: MappingJackson2XmlHttpMessageConverterTests.java   Source Code and License 6 votes vote down vote up
@Test
public void readWithXmlBomb() throws IOException {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String body = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<MyBean>&lol9;</MyBean>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));

	this.thrown.expect(HttpMessageNotReadableException.class);
	this.converter.read(MyBean.class, inputMessage);
}
 
Example 21
Project: spring4-understanding   File: Jaxb2CollectionHttpMessageConverterTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<list><rootElement><external>&lol9;</external></rootElement></list>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	this.thrown.expect(HttpMessageNotReadableException.class);
	this.thrown.expectMessage("\"lol9\"");
	this.converter.read(this.rootElementListType, null, inputMessage);
}
 
Example 22
Project: feign-form   File: SpringManyMultipartFilesReader.java   Source Code and License 6 votes vote down vote up
@Override
protected MultipartFile[] readInternal (Class<? extends MultipartFile[]> clazz, HttpInputMessage inputMessage
) throws IOException {
  val boundaryBytes = getMultiPartBoundary(inputMessage.getHeaders().getContentType());
  MultipartStream multipartStream = new MultipartStream(inputMessage.getBody(), boundaryBytes, bufSize, null);

  val multiparts = new LinkedList<ByteArrayMultipartFile>();
  for (boolean nextPart = multipartStream.skipPreamble(); nextPart; nextPart = multipartStream.readBoundary()) {
    ByteArrayMultipartFile multiPart;
    try {
      multiPart = readMultiPart(multipartStream);
    } catch (Exception e) {
      throw new HttpMessageNotReadableException("Multipart body could not be read.", e);
    }
    multiparts.add(multiPart);
  }
  return multiparts.toArray(new ByteArrayMultipartFile[multiparts.size()]);
}
 
Example 23
Project: backstopper   File: OneOffSpringFrameworkExceptionHandlerListener.java   Source Code and License 6 votes vote down vote up
protected boolean isMissingExpectedContentCase(HttpMessageConversionException ex) {
    if (ex instanceof HttpMessageNotReadableException) {
        // Different versions of Spring Web MVC can have different ways of expressing missing content.

        // More common case
        if (ex.getMessage().startsWith("Required request body is missing")) {
            return true;
        }

        // An older/more unusual case. Unfortunately there's a lot of manual digging that we have to do to determine
        //      that we've reached this case.
        if (ex.getCause() != null && ex.getCause() instanceof JsonMappingException
            && ex.getCause().getMessage() != null && ex.getCause().getMessage()
                                                       .contains("No content to map due to end-of-input")) {
            return true;
        }
    }

    return false;
}
 
Example 24
Project: dpCms   File: Fastjson2HttpMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,  HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (;;) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }
    byte[] bytes = baos.toByteArray();
    return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
}
 
Example 25
Project: eiffel-remrem-generate   File: GsonHttpMessageConverterWithValidate.java   Source Code and License 6 votes vote down vote up
private Object readTypeToken(final TypeToken<?> token, final HttpInputMessage inputMessage) throws IOException {
    final Reader reader = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));

    try {
        final String json = IOUtils.toString(reader);
        reader.close();

        // do the actual validation
        final ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
        mapper.readTree(json);

        return this.gson.fromJson(json, token.getType());
    } catch (JsonParseException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}
 
Example 26
Project: dhis2-core   File: RenderServiceMessageConverter.java   Source Code and License 6 votes vote down vote up
@Override
protected Object readInternal( Class<?> clazz, HttpInputMessage inputMessage ) throws IOException, HttpMessageNotReadableException
{
    MediaType mediaType = inputMessage.getHeaders().getContentType();

    if ( isJson( mediaType ) )
    {
        return renderService.fromJson( inputMessage.getBody(), clazz );
    }
    else if ( isXml( mediaType ) )
    {
        return renderService.fromXml( inputMessage.getBody(), clazz );
    }

    return null;
}
 
Example 27
Project: Juice   File: BaseAppController.java   Source Code and License 5 votes vote down vote up
/**
 * 用于处理HttpMessageNotReadableException
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler({ HttpMessageNotReadableException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public Result exception(HttpMessageNotReadableException e) throws IOException {
    //log.warn("got a Exception",e);

    Integer status = REQUEST_PARAMS_INVALID_ERROR.getStatus();
    String message = REQUEST_PARAMS_INVALID_ERROR.getMessage();

    return handleValue(status, message);
}
 
Example 28
Project: incubator-servicecomb-java-chassis   File: CseHttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
public Object read(Class<? extends Object> clazz,
    HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
  MessageBodyClientHttpResponseWrapper respWrapper = (MessageBodyClientHttpResponseWrapper) inputMessage;
  CseClientHttpResponse resp =
      (CseClientHttpResponse) ReflectionUtils.getField(RESPONSE_FIELD, respWrapper);
  return resp.getResult();
}
 
Example 29
Project: java_springboot   File: MyMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
protected DemoObj readInternal(Class<? extends DemoObj> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
    String temp = StreamUtils.copyToString(httpInputMessage.getBody(),Charset.forName("UTF-8"));
    String[] tempArr = temp.split("-");
    return new DemoObj(new Long(tempArr[0]),tempArr[1]);

}
 
Example 30
Project: WeatherSystem   File: FastJsonMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;
    while ((i = inputMessage.getBody().read()) != -1) {
        baos.write(i);
    }
    return JSON.parseArray(baos.toString(), clazz);
}
 
Example 31
Project: teamcity-hashicorp-vault-plugin   File: AbstractJackson2HttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(clazz, null);
	return readJavaType(javaType, inputMessage);
}
 
Example 32
Project: teamcity-hashicorp-vault-plugin   File: AbstractJackson2HttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(type, contextClass);
	return readJavaType(javaType, inputMessage);
}
 
Example 33
Project: lemon-fileservice   File: ExceptionConfiguration.java   Source Code and License 5 votes vote down vote up
/**
 * 400错误
 */
@ResponseBody
@ExceptionHandler({ HttpMessageNotReadableException.class })	
public ResultResponse httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException ex) {
	logger.error(ResultMessage.F4000.getMessage(), ex);
	return resultResponse.failure(ResultMessage.F4000, ex.getMessage());
}
 
Example 34
Project: Server_Management_Common_eSightApi   File: GsonHttpMessageConverter.java   Source Code and License 5 votes vote down vote up
private Object readTypeToken(TypeToken<?> token, HttpInputMessage inputMessage) throws IOException {
  InputStreamReader json = new InputStreamReader(inputMessage.getBody(), this.getCharset(inputMessage.getHeaders()));
  try {
    return this.gson.fromJson(json, token.getType());
  } catch (JsonParseException var5) {
    throw new HttpMessageNotReadableException("JSON parse error: " + var5.getMessage(), var5);
  }
}
 
Example 35
Project: sns-todo   File: QQTokenHttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
public OAuth2AccessToken read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
    String tokenResult = StreamUtils.copyToString(inputMessage.getBody(), charset);
    log.info("tokenResult:{}", tokenResult);
    String[] results = tokenResult.split("&");
    if (results.length == 3) {
        Map<String, String> tokenParams = new HashMap<>();
        tokenParams.put(ACCESS_TOKEN, results[0].replace("access_token=", ""));
        tokenParams.put(EXPIRES_IN, results[1].replace("expires_in=", ""));
        tokenParams.put(REFRESH_TOKEN, results[2].replace("refresh_token=", ""));
        return DefaultOAuth2AccessToken.valueOf(tokenParams);
    }
    return null;
}
 
Example 36
Project: lams   File: MappingJackson2HttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(clazz, null);
	return readJavaType(javaType, inputMessage);
}
 
Example 37
Project: lams   File: MappingJackson2HttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(type, contextClass);
	return readJavaType(javaType, inputMessage);
}
 
Example 38
Project: lams   File: MappingJackson2HttpMessageConverter.java   Source Code and License 5 votes vote down vote up
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
	try {
		return this.objectMapper.readValue(inputMessage.getBody(), javaType);
	}
	catch (IOException ex) {
		throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
	}
}
 
Example 39
Project: lams   File: MappingJacksonHttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(clazz, null);
	return readJavaType(javaType, inputMessage);
}
 
Example 40
Project: lams   File: MappingJacksonHttpMessageConverter.java   Source Code and License 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	JavaType javaType = getJavaType(type, contextClass);
	return readJavaType(javaType, inputMessage);
}