Search in sources :

Example 16 with HttpInputMessage

use of org.springframework.http.HttpInputMessage in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
    String name = getPartName(parameter, requestPart);
    parameter = parameter.nestedIfOptional();
    Object arg = null;
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            WebDataBinder binder = binderFactory.createBinder(request, arg, name);
            if (arg != null) {
                validateIfApplicable(binder, parameter);
                if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                    throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                }
            }
            mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
        } catch (MissingServletRequestPartException ex) {
            if (isRequired) {
                throw ex;
            }
        } catch (MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingServletRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpInputMessage(org.springframework.http.HttpInputMessage) WebDataBinder(org.springframework.web.bind.WebDataBinder) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) RequestPart(org.springframework.web.bind.annotation.RequestPart) MultipartException(org.springframework.web.multipart.MultipartException) RequestPartServletServerHttpRequest(org.springframework.web.multipart.support.RequestPartServletServerHttpRequest) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Example 17 with HttpInputMessage

use of org.springframework.http.HttpInputMessage in project geode by apache.

the class SerializableObjectHttpMessageConverterJUnitTest method testReadInternal.

@Test
public void testReadInternal() throws IOException {
    final String expectedInputMessageBody = "Expected content of the HTTP input message body!";
    final HttpInputMessage mockInputMessage = mockContext.mock(HttpInputMessage.class, "HttpInputMessage");
    mockContext.checking(new Expectations() {

        {
            oneOf(mockInputMessage).getBody();
            will(returnValue(new ByteArrayInputStream(IOUtils.serializeObject(expectedInputMessageBody))));
        }
    });
    final SerializableObjectHttpMessageConverter converter = new SerializableObjectHttpMessageConverter();
    final Serializable obj = converter.readInternal(String.class, mockInputMessage);
    assertTrue(obj instanceof String);
    assertEquals(expectedInputMessageBody, obj);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) Expectations(org.jmock.Expectations) Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

HttpInputMessage (org.springframework.http.HttpInputMessage)17 ByteArrayInputStream (java.io.ByteArrayInputStream)9 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpOutputMessage (org.springframework.http.HttpOutputMessage)8 Test (org.junit.Test)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)5 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)4 Method (java.lang.reflect.Method)4 MediaType (org.springframework.http.MediaType)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)2 FastJsonpHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4)2 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 FormHttpMessageConverter (org.springframework.http.converter.FormHttpMessageConverter)2 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)1 MappingFastJsonValue (com.alibaba.fastjson.support.spring.MappingFastJsonValue)1 Message (com.google.protobuf.Message)1 InputStream (java.io.InputStream)1