Search in sources :

Example 86 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class RecordReaderHttpMessageConverter method read.

@Override
public RecordReader read(final Class<? extends RecordReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    try {
        final HttpHeaders headers = inputMessage.getHeaders();
        final MediaType mediaType = headers.getContentType();
        Charset charset = mediaType.getCharSet();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        final InputStream body = inputMessage.getBody();
        final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
        final RecordReaderFactory readerFactory = IoFactory.factoryByMediaType(RecordReaderFactory.class, mediaTypeString);
        if (readerFactory == null) {
            throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
        } else {
            final Reader<Record> reader = readerFactory.newRecordReader(new InputStreamResource("recordInput", body));
            GeometryFactory factory = this.geometryFactory;
            final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
            final String srid = requestAttributes.getParameter("srid");
            if (srid != null && srid.trim().length() > 0) {
                factory = GeometryFactory.floating3d(Integer.parseInt(srid));
            }
            reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
            return (RecordReader) reader;
        }
    } catch (final IOException e) {
        throw new HttpMessageNotReadableException("Error reading data", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) RecordReader(com.revolsys.record.io.RecordReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) RecordReaderFactory(com.revolsys.record.io.RecordReaderFactory) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 87 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class MediaTypeUtil method isHtmlPage.

public static boolean isHtmlPage() {
    final HttpServletRequest request = HttpServletUtils.getRequest();
    final String format = request.getParameter("format");
    if (Property.hasValue(format)) {
        return false;
    }
    final String requestUri = URL_PATH_HELPER.getRequestUri(request);
    final String filename = WebUtils.extractFullFilenameFromUrlPath(requestUri);
    final String extension = StringUtils.getFilenameExtension(filename);
    if (Property.hasValue(extension)) {
        return false;
    }
    final String acceptHeader = request.getHeader(ACCEPT_HEADER);
    if (Property.hasValue(acceptHeader)) {
        for (final MediaType mediaType : MediaType.parseMediaTypes(acceptHeader)) {
            if (mediaType.includes(MediaType.TEXT_HTML)) {
                return true;
            }
        }
        return false;
    } else {
        return true;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(org.springframework.http.MediaType)

Example 88 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class MediaTypeUtil method getAcceptedFileNameExtension.

public static String getAcceptedFileNameExtension() {
    final HttpServletRequest request = HttpServletUtils.getRequest();
    final List<MediaType> mediaTypes = getAcceptedMediaTypes(request);
    for (final MediaType mediaType : mediaTypes) {
        final String extension = mediaTypeToExtensionMap.get(mediaType);
        if (Property.hasValue(extension)) {
            return extension;
        }
    }
    return "html";
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(org.springframework.http.MediaType)

Example 89 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class WebAnnotationMethodHandlerAdapter method handleResponseBody.

private View handleResponseBody(final Object returnValue, final ServletWebRequest webRequest) throws ServletException, IOException {
    final HttpServletRequest request = webRequest.getRequest();
    String jsonp = request.getParameter("jsonp");
    if (jsonp == null) {
        jsonp = request.getParameter("callback");
    }
    request.setAttribute(IoConstants.JSONP_PROPERTY, jsonp);
    List<MediaType> acceptedMediaTypes = MediaTypeUtil.getAcceptedMediaTypes(request, WebAnnotationMethodHandlerAdapter.this.mediaTypes, WebAnnotationMethodHandlerAdapter.this.mediaTypeOrder, WebAnnotationMethodHandlerAdapter.this.urlPathHelper, WebAnnotationMethodHandlerAdapter.this.parameterName, WebAnnotationMethodHandlerAdapter.this.defaultMediaType);
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }
    final Class<?> returnValueType = returnValue.getClass();
    final Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<>();
    if (WebAnnotationMethodHandlerAdapter.this.messageConverters != null) {
        for (final MediaType acceptedMediaType : acceptedMediaTypes) {
            for (final HttpMessageConverter<?> messageConverter : WebAnnotationMethodHandlerAdapter.this.messageConverters) {
                allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
                if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                    final MediaType mediaType = getMediaType(messageConverter.getSupportedMediaTypes(), acceptedMediaType);
                    return new HttpMessageConverterView(messageConverter, mediaType, returnValue);
                }
            }
        }
    }
    throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(allSupportedMediaTypes));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MediaType(org.springframework.http.MediaType)

Example 90 with MediaType

use of org.springframework.http.MediaType in project rest-assured by rest-assured.

the class MockMvcRequestSenderImpl method setContentTypeToApplicationFormUrlEncoded.

private void setContentTypeToApplicationFormUrlEncoded(MockHttpServletRequestBuilder request) {
    String contentType = APPLICATION_FORM_URLENCODED_VALUE;
    EncoderConfig encoderConfig = config.getEncoderConfig();
    if (encoderConfig.shouldAppendDefaultContentCharsetToContentTypeIfUndefined()) {
        contentType += "; charset=";
        if (encoderConfig.hasDefaultCharsetForContentType(contentType)) {
            contentType += encoderConfig.defaultCharsetForContentType(contentType);
        } else {
            contentType += encoderConfig.defaultContentCharset();
        }
    }
    MediaType mediaType = MediaType.parseMediaType(contentType);
    request.contentType(mediaType);
    List<Header> newHeaders = new ArrayList<Header>(headers.asList());
    newHeaders.add(new Header(CONTENT_TYPE, mediaType.toString()));
    headers = new Headers(newHeaders);
}
Also used : EncoderConfig(io.restassured.config.EncoderConfig) MediaType(org.springframework.http.MediaType)

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