Search in sources :

Example 1 with InvalidMediaTypeException

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

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
	 * Iterate all RequestMappingInfos once again, look if any match by URL at
	 * least and raise exceptions accordingly.
	 * @throws MethodNotAllowedException for matches by URL but not by HTTP method
	 * @throws UnsupportedMediaTypeStatusException if there are matches by URL
	 * and HTTP method but not by consumable media types
	 * @throws NotAcceptableStatusException if there are matches by URL and HTTP
	 * method but not by producible media types
	 * @throws ServerWebInputException if there are matches by URL and HTTP
	 * method but not by query parameter conditions
	 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String lookupPath, ServerWebExchange exchange) throws Exception {
    PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
    if (helper.isEmpty()) {
        return null;
    }
    ServerHttpRequest request = exchange.getRequest();
    if (helper.hasMethodsMismatch()) {
        HttpMethod httpMethod = request.getMethod();
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS.matches(httpMethod.name())) {
            HttpOptionsHandler handler = new HttpOptionsHandler(methods);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new MethodNotAllowedException(httpMethod.name(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType;
        try {
            contentType = request.getHeaders().getContentType();
        } catch (InvalidMediaTypeException ex) {
            throw new UnsupportedMediaTypeStatusException(ex.getMessage());
        }
        throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes));
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new NotAcceptableStatusException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        throw new ServerWebInputException("Unsatisfied query parameter conditions: " + helper.getParamConditions() + ", actual parameters: " + request.getQueryParams());
    }
    return null;
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ServerWebInputException(org.springframework.web.server.ServerWebInputException) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 2 with InvalidMediaTypeException

use of org.springframework.http.InvalidMediaTypeException in project ma-modules-public by infiniteautomation.

the class FileStoreRestV2Controller method getFile.

protected ResponseEntity<FileSystemResource> getFile(File file, boolean download, HttpServletRequest request, HttpServletResponse response) throws HttpMediaTypeNotAcceptableException {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set(HttpHeaders.CONTENT_DISPOSITION, download ? "attachment" : "inline");
    MediaType mediaType = null;
    Set<MediaType> mediaTypes = Sets.newHashSet(MediaType.APPLICATION_OCTET_STREAM);
    request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    // ResourceHttpMessageConverter uses ActivationMediaTypeFactory.getMediaType(resource) but this is not visible
    String mimeType = request.getServletContext().getMimeType(file.getName());
    if (StringUtils.hasText(mimeType)) {
        try {
            mediaType = MediaType.parseMediaType(mimeType);
        } catch (InvalidMediaTypeException e) {
        // Shouldn't happen - ServletContext.getMimeType() should return valid mime types
        }
    }
    // to whatever the Accept header was
    if (mediaType == null) {
        mediaTypes.add(MediaType.ALL);
        responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    } else {
        mediaTypes.add(mediaType);
        responseHeaders.setContentType(mediaType);
    }
    // this doesn't work as a header from ResponseEntity wont be set if it is already set in the response
    // responseHeaders.setCacheControl(cacheControlHeader);
    response.setHeader(HttpHeaders.CACHE_CONTROL, cacheControlHeader);
    return new ResponseEntity<>(new FileSystemResource(file), responseHeaders, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) MediaType(org.springframework.http.MediaType) FileSystemResource(org.springframework.core.io.FileSystemResource) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 3 with InvalidMediaTypeException

use of org.springframework.http.InvalidMediaTypeException in project spring-security by spring-projects.

the class MediaTypeServerWebExchangeMatcher method resolveMediaTypes.

private List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
    try {
        List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept();
        MediaType.sortBySpecificityAndQuality(mediaTypes);
        return mediaTypes;
    } catch (InvalidMediaTypeException ex) {
        String value = exchange.getRequest().getHeaders().getFirst("Accept");
        throw new NotAcceptableStatusException("Could not parse 'Accept' header [" + value + "]: " + ex.getMessage());
    }
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 4 with InvalidMediaTypeException

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

the class HeaderContentTypeResolver method resolveMediaTypes.

@Override
public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
    try {
        List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept();
        MimeTypeUtils.sortBySpecificity(mediaTypes);
        return (!CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST);
    } catch (InvalidMediaTypeException ex) {
        String value = exchange.getRequest().getHeaders().getFirst("Accept");
        throw new NotAcceptableStatusException("Could not parse 'Accept' header [" + value + "]: " + ex.getMessage());
    }
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 5 with InvalidMediaTypeException

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

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
 * Iterate all RequestMappingInfo's once again, look if any match by URL at
 * least and raise exceptions according to what doesn't match.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {
    PartialMatchHelper helper = new PartialMatchHelper(infos, request);
    if (helper.isEmpty()) {
        return null;
    }
    if (helper.hasMethodsMismatch()) {
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS.matches(request.getMethod())) {
            Set<MediaType> mediaTypes = helper.getConsumablePatchMediaTypes();
            HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new HttpRequestMethodNotSupportedException(request.getMethod(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType = null;
        if (StringUtils.hasLength(request.getContentType())) {
            try {
                contentType = MediaType.parseMediaType(request.getContentType());
            } catch (InvalidMediaTypeException ex) {
                throw new HttpMediaTypeNotSupportedException(ex.getMessage());
            }
        }
        throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes));
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        List<String[]> conditions = helper.getParamConditions();
        throw new UnsatisfiedServletRequestParameterException(conditions, request.getParameterMap());
    }
    return null;
}
Also used : UnsatisfiedServletRequestParameterException(org.springframework.web.bind.UnsatisfiedServletRequestParameterException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Aggregations

InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)12 MediaType (org.springframework.http.MediaType)12 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)4 HttpMethod (org.springframework.http.HttpMethod)3 HandlerMethod (org.springframework.web.method.HandlerMethod)3 IOException (java.io.IOException)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)2 Nullable (org.springframework.lang.Nullable)2 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)2 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)2 MethodNotAllowedException (org.springframework.web.server.MethodNotAllowedException)2 ServerWebInputException (org.springframework.web.server.ServerWebInputException)2 UnsupportedMediaTypeStatusException (org.springframework.web.server.UnsupportedMediaTypeStatusException)2 RequestContext (com.netflix.zuul.context.RequestContext)1 Charset (java.nio.charset.Charset)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ResolvableType (org.springframework.core.ResolvableType)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1