Search in sources :

Example 76 with MediaType

use of org.springframework.http.MediaType in project spring-cloud-function by spring-cloud.

the class FluxReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    Object adaptFrom = returnValue;
    if (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> value = (ResponseEntity<?>) returnValue;
        adaptFrom = value.getBody();
        HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
        response.setStatus(value.getStatusCodeValue());
        HttpHeaders headers = value.getHeaders();
        for (String name : headers.keySet()) {
            List<String> list = headers.get(name);
            for (String header : list) {
                response.addHeader(name, header);
            }
        }
    }
    Publisher<?> flux = (Publisher<?>) adaptFrom;
    Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
    Class<?> type = inspector.getOutputType(handler);
    if (isOutputSingle(webRequest, handler, type)) {
        Object result = Flux.from(flux).blockFirst();
        if (result instanceof Message) {
            Message<?> message = (Message<?>) result;
            result = message.getPayload();
            addHeaders(webRequest, message);
        }
        single.handleReturnValue(result, singleReturnType, mavContainer, webRequest);
        return;
    }
    MediaType mediaType = null;
    if (isPlainText(webRequest) && (CharSequence.class.isAssignableFrom(type) || Void.class.isAssignableFrom(type))) {
        mediaType = MediaType.TEXT_PLAIN;
    } else {
        mediaType = findMediaType(webRequest);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Handling return value " + type + " with media type: " + mediaType);
    }
    ServletServerHttpRequest request = new ServletServerHttpRequest(webRequest.getNativeRequest(HttpServletRequest.class));
    delegate.handleReturnValue(getEmitter(timeout, flux, mediaType, request.getHeaders()), returnType, mavContainer, webRequest);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) Message(org.springframework.messaging.Message) HttpServletResponse(javax.servlet.http.HttpServletResponse) Publisher(org.reactivestreams.Publisher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) MediaType(org.springframework.http.MediaType)

Example 77 with MediaType

use of org.springframework.http.MediaType in project irida by phac-nml.

the class SequenceFileMessageConverterTest method canConvertFastqToString.

@Test
public void canConvertFastqToString() {
    Class<String> clazz = String.class;
    MediaType mediaType = new MediaType("application", "fastq");
    assertFalse(converter.canRead(clazz, mediaType));
}
Also used : MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 78 with MediaType

use of org.springframework.http.MediaType in project ma-core-public by infiniteautomation.

the class JsonUsernamePasswordAuthenticationFilter method requiresAuthentication.

@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
    String contentType = request.getContentType();
    if (contentType == null || contentType.isEmpty())
        return false;
    boolean pathMatches = super.requiresAuthentication(request, response);
    if (!pathMatches) {
        return false;
    }
    MediaType requestMediaType;
    try {
        requestMediaType = MediaType.valueOf(contentType);
    } catch (Exception e) {
        return false;
    }
    return requestMediaType.isCompatibleWith(MediaType.APPLICATION_JSON);
}
Also used : MediaType(org.springframework.http.MediaType) ServletException(javax.servlet.ServletException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) IOException(java.io.IOException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 79 with MediaType

use of org.springframework.http.MediaType in project ma-core-public by infiniteautomation.

the class ExceptionCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        CsvSchema schema;
        if (object instanceof AbstractRestV2Exception) {
            schema = this.restExceptionSchema;
            object = new CsvRestException((AbstractRestV2Exception) object);
        } else {
            schema = this.exceptionSchema;
        }
        writePrefix(generator, object);
        ObjectWriter objectWriter = this.objectMapper.writer().with(schema);
        objectWriter.writeValue(generator, object);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 80 with MediaType

use of org.springframework.http.MediaType in project TranslateIt-api by zunpiau.

the class WebConfig method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ArrayList<MediaType> mediaTypes = new ArrayList<>(2);
    mediaTypes.add(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
    mediaTypes.add(new MediaType(MediaType.TEXT_HTML, StandardCharsets.UTF_8));
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
    stringHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    jackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    converters.add(stringHttpMessageConverter);
    converters.add(jackson2HttpMessageConverter);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Aggregations

MediaType (org.springframework.http.MediaType)477 Test (org.junit.jupiter.api.Test)177 HttpHeaders (org.springframework.http.HttpHeaders)97 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)58 Charset (java.nio.charset.Charset)42 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)34 List (java.util.List)33 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)33 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)33 HashMap (java.util.HashMap)30 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)30 IOException (java.io.IOException)26 Resource (org.springframework.core.io.Resource)23 ResolvableType (org.springframework.core.ResolvableType)22 HttpEntity (org.springframework.http.HttpEntity)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)20 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)20 Nullable (org.springframework.lang.Nullable)18 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18