List of usage examples for org.springframework.http.converter HttpMessageConverter read
T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
From source file:newcontroller.handler.impl.DefaultRequest.java
@Override public <T> T body(Class<T> clazz) { MediaType mediaType = MediaType.parseMediaType(this.request.getContentType()); HttpMessageConverter converter = HttpMessageConvertersHelper.findConverter(this.converters, clazz, mediaType);/* w ww . j a va 2 s . c om*/ try { return clazz.cast(converter.read(clazz, new ServletServerHttpRequest(this.request))); } catch (IOException e) { throw new UncheckedIOException(e); // TODO } }
From source file:org.springframework.cloud.aws.messaging.endpoint.NotificationMessageHandlerMethodArgumentResolver.java
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object doResolveArgumentFromNotificationMessage(JsonNode content, HttpInputMessage request,
Class<?> parameterType) {
if (!"Notification".equals(content.get("Type").asText())) {
throw new IllegalArgumentException(
"@NotificationMessage annotated parameters are only allowed for method that receive a notification message.");
}// w ww . jav a2s . co m
MediaType mediaType = getMediaType(content);
String messageContent = content.findPath("Message").asText();
for (HttpMessageConverter<?> converter : this.messageConverter) {
if (converter.canRead(parameterType, mediaType)) {
try {
return converter.read((Class) parameterType,
new ByteArrayHttpInputMessage(messageContent, mediaType, request));
} catch (Exception e) {
throw new HttpMessageNotReadableException(
"Error converting notification message with payload:" + messageContent, e);
}
}
}
throw new HttpMessageNotReadableException(
"Error converting notification message with payload:" + messageContent);
}
From source file:org.cloudfoundry.identity.uaa.error.ExceptionReportHttpMessageConverter.java
@Override protected ExceptionReport readInternal(Class<? extends ExceptionReport> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { for (HttpMessageConverter<?> converter : messageConverters) { for (MediaType mediaType : converter.getSupportedMediaTypes()) { if (converter.canRead(Map.class, mediaType)) { @SuppressWarnings({ "rawtypes", "unchecked" }) HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter; @SuppressWarnings("unchecked") Map<String, String> map = messageConverter.read(Map.class, inputMessage); return new ExceptionReport(getException(map)); }/*from w w w . ja va 2 s.c o m*/ } } return null; }
From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java
private Object read(IncomingRequest request, HttpMessageConverter<Object> converter, RootResourceInformation information) { try {//from www . j a va2s.c o m return converter.read(information.getDomainType(), request.getServerHttpRequest()); } catch (IOException o_O) { throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, information.getDomainType()), o_O); } }
From source file:com.expedia.seiso.web.resolver.PEResourceResolver.java
@SuppressWarnings({ "unchecked", "rawtypes" })
private Item toItem(Class<?> itemClass, HttpServletRequest request) throws IOException {
val wrappedRequest = resolverUtils.wrapRequest(request);
val contentType = wrappedRequest.getHeaders().getContentType();
for (HttpMessageConverter messageConverter : messageConverters) {
// This is how we process application/json separately from application/hal+json.
if (messageConverter.canRead(itemClass, contentType)) {
return (Item) messageConverter.read(itemClass, wrappedRequest);
}/*from www. j a v a 2 s . c o m*/
}
throw new RuntimeException(
"No converter for itemClass=" + itemClass.getName() + ", contentType=" + contentType.getType());
}
From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType) throws Exception { MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType())); String paramName = methodParam.getParameterName(); if (paramName != null) { builder.append(' '); builder.append(paramName);/*from w ww . j a va 2 s. c om*/ } throw new HttpMediaTypeNotSupportedException( "Cannot extract parameter (" + builder.toString() + "): no Content-Type found"); } List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); if (this.messageConverters != null) { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); if (messageConverter.canRead(paramType, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return messageConverter.read(paramType, inputMessage); } } } throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes); }
From source file:org.springframework.data.keyvalue.riak.core.AbstractRiakTemplate.java
@SuppressWarnings({ "unchecked" })
protected <T> RiakValue<T> extractValue(final ResponseEntity<?> response, Class<?> origType,
Class<T> requiredType) throws IOException {
if (response.hasBody()) {
RiakMetaData meta = extractMetaData(response.getHeaders());
Object o = response.getBody();
if (!origType.equals(requiredType)) {
if (conversionService.canConvert(origType, requiredType)) {
o = conversionService.convert(o, requiredType);
} else {
if (o instanceof byte[] || o instanceof String) {
// Peek inside, see if it's a string of something we recognize
String s = (o instanceof byte[] ? new String((byte[]) o) : (String) o);
if (s.charAt(0) == '{' || s.charAt(0) == '[') {
// Looks like it might be a JSON string. Use the JSON converter
for (HttpMessageConverter conv : getRestTemplate().getMessageConverters()) {
if (conv instanceof MappingJacksonHttpMessageConverter) {
o = conv.read(requiredType, new HttpInputMessage() {
public InputStream getBody() throws IOException {
Object body = response.getBody();
return new ByteArrayInputStream((body instanceof byte[] ? (byte[]) body
: ((String) body).getBytes()));
}/*from w ww . j a v a2 s.c o m*/
public HttpHeaders getHeaders() {
return response.getHeaders();
}
});
break;
}
}
}
} else {
throw new DataStoreOperationException(
"Cannot convert object of type " + origType + " to type " + requiredType);
}
}
}
return new RiakValue<T>((T) o, meta);
}
return null;
}
From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage,
Class<?> paramType) throws Exception {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
String paramName = methodParam.getParameterName();
if (paramName != null) {
builder.append(' ');
builder.append(paramName);/*from w w w.j a va2 s . com*/
}
throw new HttpMediaTypeNotSupportedException(
"Cannot extract parameter (" + builder.toString() + "): no Content-Type found");
}
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
if (this.messageConverters != null) {
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
if (messageConverter.canRead(paramType, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType + "\" using ["
+ messageConverter + "]");
}
return messageConverter.read((Class) paramType, inputMessage);
}
}
}
throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
}
From source file:org.springframework.web.client.HttpMessageConverterExtractor.java
@Override
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
public T extractData(ClientHttpResponse response) throws IOException {
MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
return null;
}/*from ww w. j av a 2 s. c om*/
MediaType contentType = getContentType(responseWrapper);
try {
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
if (messageConverter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter;
if (genericMessageConverter.canRead(this.responseType, null, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + this.responseType + "] as \"" + contentType + "\" using ["
+ messageConverter + "]");
}
return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
}
}
if (this.responseClass != null) {
if (messageConverter.canRead(this.responseClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + this.responseClass.getName() + "] as \"" + contentType
+ "\" using [" + messageConverter + "]");
}
return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
}
}
}
} catch (IOException | HttpMessageNotReadableException ex) {
throw new RestClientException("Error while extracting response for type [" + this.responseType
+ "] and content type [" + contentType + "]", ex);
}
throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found "
+ "for response type [" + this.responseType + "] and content type [" + contentType + "]");
}
From source file:org.thingsboard.server.controller.AbstractControllerTest.java
@SuppressWarnings("unchecked") protected <T> T readResponse(ResultActions result, Class<T> responseClass) throws Exception { byte[] content = result.andReturn().getResponse().getContentAsByteArray(); MockHttpInputMessage mockHttpInputMessage = new MockHttpInputMessage(content); HttpMessageConverter converter = responseClass.equals(String.class) ? stringHttpMessageConverter : mappingJackson2HttpMessageConverter; return (T) converter.read(responseClass, mockHttpInputMessage); }