Search in sources :

Example 1 with NotAcceptableStatusException

use of org.springframework.web.server.NotAcceptableStatusException 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 NotAcceptableStatusException

use of org.springframework.web.server.NotAcceptableStatusException in project spring-framework by spring-projects.

the class ViewResolutionResultHandler method render.

private Mono<? extends Void> render(List<View> views, Map<String, Object> model, ServerWebExchange exchange) {
    List<MediaType> mediaTypes = getMediaTypes(views);
    MediaType bestMediaType = selectMediaType(exchange, () -> mediaTypes);
    if (bestMediaType != null) {
        for (View view : views) {
            for (MediaType mediaType : view.getSupportedMediaTypes()) {
                if (mediaType.isCompatibleWith(bestMediaType)) {
                    return view.render(model, mediaType, exchange);
                }
            }
        }
    }
    throw new NotAcceptableStatusException(mediaTypes);
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) MediaType(org.springframework.http.MediaType)

Example 3 with NotAcceptableStatusException

use of org.springframework.web.server.NotAcceptableStatusException in project spring-integration by spring-projects.

the class WebFluxInboundEndpoint method writeResponseBody.

@SuppressWarnings("unchecked")
private Mono<Void> writeResponseBody(ServerWebExchange exchange, Object body) {
    ResolvableType bodyType = ResolvableType.forInstance(body);
    ReactiveAdapter adapter = this.adapterRegistry.getAdapter(bodyType.resolve(), body);
    Publisher<?> publisher;
    ResolvableType elementType;
    if (adapter != null) {
        publisher = adapter.toPublisher(body);
        ResolvableType genericType = bodyType.getGeneric(0);
        elementType = getElementType(adapter, genericType);
    } else {
        publisher = Mono.justOrEmpty(body);
        elementType = bodyType;
    }
    if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) {
        return Mono.from((Publisher<Void>) publisher);
    }
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(bodyType);
    MediaType bestMediaType = selectMediaType(exchange, () -> producibleMediaTypes);
    if (bestMediaType != null) {
        for (HttpMessageWriter<?> writer : this.codecConfigurer.getWriters()) {
            if (writer.canWrite(bodyType, bestMediaType)) {
                return ((HttpMessageWriter<Object>) writer).write(publisher, elementType, bestMediaType, exchange.getResponse(), Collections.emptyMap());
            }
        }
    } else {
        if (producibleMediaTypes.isEmpty()) {
            return Mono.error(new IllegalStateException("No HttpMessageWriters for response type: " + bodyType));
        }
    }
    return Mono.error(new NotAcceptableStatusException(producibleMediaTypes));
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 4 with NotAcceptableStatusException

use of org.springframework.web.server.NotAcceptableStatusException 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 5 with NotAcceptableStatusException

use of org.springframework.web.server.NotAcceptableStatusException in project spring-security by spring-projects.

the class MediaTypeServerWebExchangeMatcher method matches.

@Override
public Mono<MatchResult> matches(ServerWebExchange exchange) {
    List<MediaType> httpRequestMediaTypes;
    try {
        httpRequestMediaTypes = resolveMediaTypes(exchange);
    } catch (NotAcceptableStatusException ex) {
        this.logger.debug("Failed to parse MediaTypes, returning false", ex);
        return MatchResult.notMatch();
    }
    this.logger.debug(LogMessage.format("httpRequestMediaTypes=%s", httpRequestMediaTypes));
    for (MediaType httpRequestMediaType : httpRequestMediaTypes) {
        this.logger.debug(LogMessage.format("Processing %s", httpRequestMediaType));
        if (shouldIgnore(httpRequestMediaType)) {
            this.logger.debug("Ignoring");
            continue;
        }
        if (this.useEquals) {
            boolean isEqualTo = this.matchingMediaTypes.contains(httpRequestMediaType);
            this.logger.debug("isEqualTo " + isEqualTo);
            return isEqualTo ? MatchResult.match() : MatchResult.notMatch();
        }
        for (MediaType matchingMediaType : this.matchingMediaTypes) {
            boolean isCompatibleWith = matchingMediaType.isCompatibleWith(httpRequestMediaType);
            this.logger.debug(LogMessage.format("%s .isCompatibleWith %s = %s", matchingMediaType, httpRequestMediaType, isCompatibleWith));
            if (isCompatibleWith) {
                return MatchResult.match();
            }
        }
    }
    this.logger.debug("Did not match any media types");
    return MatchResult.notMatch();
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) MediaType(org.springframework.http.MediaType)

Aggregations

NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)12 MediaType (org.springframework.http.MediaType)11 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)4 ReactiveAdapter (org.springframework.core.ReactiveAdapter)2 ResolvableType (org.springframework.core.ResolvableType)2 HttpMethod (org.springframework.http.HttpMethod)2 HttpStatus (org.springframework.http.HttpStatus)2 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 MethodNotAllowedException (org.springframework.web.server.MethodNotAllowedException)2 ServerWebInputException (org.springframework.web.server.ServerWebInputException)2 UnsupportedMediaTypeStatusException (org.springframework.web.server.UnsupportedMediaTypeStatusException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)1 Mono (reactor.core.publisher.Mono)1