Example usage for org.springframework.data.rest.webmvc IncomingRequest IncomingRequest

List of usage examples for org.springframework.data.rest.webmvc IncomingRequest IncomingRequest

Introduction

In this page you can find the example usage for org.springframework.data.rest.webmvc IncomingRequest IncomingRequest.

Prototype

public IncomingRequest(ServerHttpRequest request) 

Source Link

Document

Creates a new IncomingRequest from ServerHttpRequest .

Usage

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    RootResourceInformation resourceInformation = resourceInformationResolver.resolveArgument(parameter,
            mavContainer, webRequest, binderFactory);

    HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    ServletServerHttpRequest request = new ServletServerHttpRequest(nativeRequest);
    IncomingRequest incoming = new IncomingRequest(request);

    Class<?> domainType = resourceInformation.getDomainType();
    MediaType contentType = request.getHeaders().getContentType();

    for (HttpMessageConverter converter : messageConverters) {

        if (!converter.canRead(PersistentEntityResource.class, contentType)) {
            continue;
        }/*from  w ww  . j  av a 2 s .  c o m*/

        Serializable id = idResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        Object objectToUpdate = getObjectToUpdate(id, resourceInformation);

        boolean forUpdate = false;
        Object entityIdentifier = null;
        PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();

        if (objectToUpdate != null) {
            forUpdate = true;
            entityIdentifier = entity.getIdentifierAccessor(objectToUpdate).getIdentifier();
        }

        Object obj = read(resourceInformation, incoming, converter, objectToUpdate);

        if (obj == null) {
            throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, domainType));
        }

        if (entityIdentifier != null) {
            entity.getPropertyAccessor(obj).setProperty(entity.getIdProperty(), entityIdentifier);
        }

        Builder build = PersistentEntityResource.build(obj, entity);
        return forUpdate ? build.build() : build.forCreation();
    }

    throw new HttpMessageNotReadableException(String.format(NO_CONVERTER_FOUND, domainType, contentType));
}