Search in sources :

Example 1 with HttpInputMessage

use of cn.taketoday.http.HttpInputMessage in project today-framework by TAKETODAY.

the class RequestResponseBodyAdviceChainTests method requestBodyAdvice.

@SuppressWarnings("unchecked")
@Test
public void requestBodyAdvice() throws IOException {
    RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
    ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
    List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
    RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
    HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest());
    given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true);
    given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped);
    assertThat(chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType)).isSameAs(wrapped);
    String modified = "body++";
    given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified);
    assertThat(chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType)).isEqualTo(modified);
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestBodyAdvice(cn.taketoday.web.handler.method.RequestBodyAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with HttpInputMessage

use of cn.taketoday.http.HttpInputMessage in project today-framework by TAKETODAY.

the class ProtobufHttpMessageConverter method readInternal.

@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    MediaType contentType = inputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = PROTOBUF;
    }
    Charset charset = contentType.getCharset();
    if (charset == null) {
        charset = DEFAULT_CHARSET;
    }
    Message.Builder builder = getMessageBuilder(clazz);
    if (PROTOBUF.isCompatibleWith(contentType)) {
        builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
    } else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
        InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
        TextFormat.merge(reader, this.extensionRegistry, builder);
    } else if (this.protobufFormatSupport != null) {
        this.protobufFormatSupport.merge(inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
    }
    return builder.build();
}
Also used : HttpOutputMessage(cn.taketoday.http.HttpOutputMessage) HttpInputMessage(cn.taketoday.http.HttpInputMessage) Message(com.google.protobuf.Message) InputStreamReader(java.io.InputStreamReader) MediaType(cn.taketoday.http.MediaType) Charset(java.nio.charset.Charset)

Example 3 with HttpInputMessage

use of cn.taketoday.http.HttpInputMessage in project today-infrastructure by TAKETODAY.

the class ProtobufHttpMessageConverter method readInternal.

@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    MediaType contentType = inputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = PROTOBUF;
    }
    Charset charset = contentType.getCharset();
    if (charset == null) {
        charset = DEFAULT_CHARSET;
    }
    Message.Builder builder = getMessageBuilder(clazz);
    if (PROTOBUF.isCompatibleWith(contentType)) {
        builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
    } else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
        InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
        TextFormat.merge(reader, this.extensionRegistry, builder);
    } else if (this.protobufFormatSupport != null) {
        this.protobufFormatSupport.merge(inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
    }
    return builder.build();
}
Also used : HttpOutputMessage(cn.taketoday.http.HttpOutputMessage) HttpInputMessage(cn.taketoday.http.HttpInputMessage) Message(com.google.protobuf.Message) InputStreamReader(java.io.InputStreamReader) MediaType(cn.taketoday.http.MediaType) Charset(java.nio.charset.Charset)

Example 4 with HttpInputMessage

use of cn.taketoday.http.HttpInputMessage in project today-framework by TAKETODAY.

the class AbstractMessageConverterParameterResolver method readWithMessageConverters.

/**
 * Create the method argument value of the expected parameter type by
 * reading from the given request.
 *
 * @param context the current request context
 * @param parameter the method parameter descriptor (may be {@code null})
 * @param targetType the target type, not necessarily the same as the method
 * parameter type, e.g. for {@code HttpEntity<String>}.
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@Nullable
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(RequestContext context, MethodParameter parameter, Type targetType) throws // 
IOException, // 
HttpMediaTypeNotSupportedException, // 
HttpMessageNotReadableException {
    MediaType contentType;
    boolean noContentType = false;
    try {
        contentType = context.requestHeaders().getContentType();
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotSupportedException(ex.getMessage());
    }
    if (contentType == null) {
        noContentType = true;
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }
    Class<?> contextClass = parameter.getContainingClass();
    Class<T> targetClass = targetType instanceof Class ? (Class<T>) targetType : null;
    if (targetClass == null) {
        ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
        targetClass = (Class<T>) resolvableType.resolve();
    }
    Object body = NO_VALUE;
    EmptyBodyCheckingHttpInputMessage message = null;
    try {
        message = new EmptyBodyCheckingHttpInputMessage(context);
        RequestResponseBodyAdviceChain adviceChain = getAdvice();
        for (HttpMessageConverter<?> converter : this.messageConverters) {
            Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
            GenericHttpMessageConverter<?> genericConverter = converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null;
            if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) : targetClass != null && converter.canRead(targetClass, contentType)) {
                if (message.hasBody()) {
                    HttpInputMessage msgToUse = adviceChain.beforeBodyRead(message, parameter, targetType, converterType);
                    body = genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse);
                    body = adviceChain.afterBodyRead(body, msgToUse, parameter, targetType, converterType);
                } else {
                    body = adviceChain.handleEmptyBody(null, message, parameter, targetType, converterType);
                }
                break;
            }
        }
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("I/O error while reading input message", ex, context);
    } finally {
        if (message != null && message.hasBody()) {
            closeStreamIfNecessary(message.getBody());
        }
    }
    if (body == NO_VALUE) {
        HttpMethod httpMethod = context.getMethod();
        if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) || (noContentType && !message.hasBody())) {
            return null;
        }
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(targetClass != null ? targetClass : Object.class));
    }
    MediaType selectedContentType = contentType;
    Object theBody = body;
    LogFormatUtils.traceDebug(log, traceOn -> {
        String formatted = LogFormatUtils.formatValue(theBody, !traceOn);
        return "Read \"" + selectedContentType + "\" to [" + formatted + "]";
    });
    return body;
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) IOException(java.io.IOException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) MediaType(cn.taketoday.http.MediaType) ResolvableType(cn.taketoday.core.ResolvableType) HttpMethod(cn.taketoday.http.HttpMethod) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) Nullable(cn.taketoday.lang.Nullable)

Aggregations

HttpInputMessage (cn.taketoday.http.HttpInputMessage)4 MediaType (cn.taketoday.http.MediaType)3 HttpOutputMessage (cn.taketoday.http.HttpOutputMessage)2 Message (com.google.protobuf.Message)2 InputStreamReader (java.io.InputStreamReader)2 Charset (java.nio.charset.Charset)2 ResolvableType (cn.taketoday.core.ResolvableType)1 HttpMethod (cn.taketoday.http.HttpMethod)1 InvalidMediaTypeException (cn.taketoday.http.InvalidMediaTypeException)1 GenericHttpMessageConverter (cn.taketoday.http.converter.GenericHttpMessageConverter)1 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 ServletServerHttpRequest (cn.taketoday.http.server.ServletServerHttpRequest)1 Nullable (cn.taketoday.lang.Nullable)1 HttpMediaTypeNotSupportedException (cn.taketoday.web.HttpMediaTypeNotSupportedException)1 RequestBodyAdvice (cn.taketoday.web.handler.method.RequestBodyAdvice)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1 IOException (java.io.IOException)1 Test (org.junit.jupiter.api.Test)1