Search in sources :

Example 1 with NotAllowedException

use of javax.ws.rs.NotAllowedException in project jersey by jersey.

the class MethodSelectingRouter method getMethodRouter.

private List<Router> getMethodRouter(final RequestProcessingContext context) {
    final ContainerRequest request = context.request();
    final List<ConsumesProducesAcceptor> acceptors = consumesProducesAcceptors.get(request.getMethod());
    if (acceptors == null) {
        throw new NotAllowedException(Response.status(Status.METHOD_NOT_ALLOWED).allow(consumesProducesAcceptors.keySet()).build());
    }
    final List<ConsumesProducesAcceptor> satisfyingAcceptors = new LinkedList<>();
    final Set<ResourceMethod> differentInvokableMethods = Collections.newSetFromMap(new IdentityHashMap<>());
    for (ConsumesProducesAcceptor cpi : acceptors) {
        if (cpi.isConsumable(request)) {
            satisfyingAcceptors.add(cpi);
            differentInvokableMethods.add(cpi.methodRouting.method);
        }
    }
    if (satisfyingAcceptors.isEmpty()) {
        throw new NotSupportedException();
    }
    final List<AcceptableMediaType> acceptableMediaTypes = request.getQualifiedAcceptableMediaTypes();
    final MediaType requestContentType = request.getMediaType();
    final MediaType effectiveContentType = requestContentType == null ? MediaType.WILDCARD_TYPE : requestContentType;
    final MethodSelector methodSelector = selectMethod(acceptableMediaTypes, satisfyingAcceptors, effectiveContentType, differentInvokableMethods.size() == 1);
    if (methodSelector.selected != null) {
        final RequestSpecificConsumesProducesAcceptor selected = methodSelector.selected;
        if (methodSelector.sameFitnessAcceptors != null) {
            reportMethodSelectionAmbiguity(acceptableMediaTypes, methodSelector.selected, methodSelector.sameFitnessAcceptors);
        }
        context.push(new Function<ContainerResponse, ContainerResponse>() {

            @Override
            public ContainerResponse apply(final ContainerResponse responseContext) {
                // - either there is an entity, or we are responding to a HEAD request
                if (responseContext.getMediaType() == null && ((responseContext.hasEntity() || HttpMethod.HEAD.equals(request.getMethod())))) {
                    MediaType effectiveResponseType = determineResponseMediaType(responseContext.getEntityClass(), responseContext.getEntityType(), methodSelector.selected, acceptableMediaTypes);
                    if (MediaTypes.isWildcard(effectiveResponseType)) {
                        if (effectiveResponseType.isWildcardType() || "application".equalsIgnoreCase(effectiveResponseType.getType())) {
                            effectiveResponseType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
                        } else {
                            throw new NotAcceptableException();
                        }
                    }
                    responseContext.setMediaType(effectiveResponseType);
                }
                return responseContext;
            }
        });
        return selected.methodRouting.routers;
    }
    throw new NotAcceptableException();
}
Also used : NotAllowedException(javax.ws.rs.NotAllowedException) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) LinkedList(java.util.LinkedList) NotAcceptableException(javax.ws.rs.NotAcceptableException) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) NotSupportedException(javax.ws.rs.NotSupportedException) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 2 with NotAllowedException

use of javax.ws.rs.NotAllowedException in project jersey by jersey.

the class JerseyInvocation method convertToException.

private ProcessingException convertToException(final Response response) {
    try {
        // Buffer and close entity input stream (if any) to prevent
        // leaking connections (see JERSEY-2157).
        response.bufferEntity();
        final WebApplicationException webAppException;
        final int statusCode = response.getStatus();
        final Response.Status status = Response.Status.fromStatusCode(statusCode);
        if (status == null) {
            final Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
            webAppException = createExceptionForFamily(response, statusFamily);
        } else {
            switch(status) {
                case BAD_REQUEST:
                    webAppException = new BadRequestException(response);
                    break;
                case UNAUTHORIZED:
                    webAppException = new NotAuthorizedException(response);
                    break;
                case FORBIDDEN:
                    webAppException = new ForbiddenException(response);
                    break;
                case NOT_FOUND:
                    webAppException = new NotFoundException(response);
                    break;
                case METHOD_NOT_ALLOWED:
                    webAppException = new NotAllowedException(response);
                    break;
                case NOT_ACCEPTABLE:
                    webAppException = new NotAcceptableException(response);
                    break;
                case UNSUPPORTED_MEDIA_TYPE:
                    webAppException = new NotSupportedException(response);
                    break;
                case INTERNAL_SERVER_ERROR:
                    webAppException = new InternalServerErrorException(response);
                    break;
                case SERVICE_UNAVAILABLE:
                    webAppException = new ServiceUnavailableException(response);
                    break;
                default:
                    final Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
                    webAppException = createExceptionForFamily(response, statusFamily);
            }
        }
        return new ResponseProcessingException(response, webAppException);
    } catch (final Throwable t) {
        return new ResponseProcessingException(response, LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
    }
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) WebApplicationException(javax.ws.rs.WebApplicationException) NotAllowedException(javax.ws.rs.NotAllowedException) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Response(javax.ws.rs.core.Response) NotAcceptableException(javax.ws.rs.NotAcceptableException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) NotSupportedException(javax.ws.rs.NotSupportedException)

Example 3 with NotAllowedException

use of javax.ws.rs.NotAllowedException in project atlasdb by palantir.

the class RsErrorDecoder method decode.

@Override
public RuntimeException decode(String methodKey, feign.Response feignResponse) {
    try {
        Response response = convertResponseToRs(feignResponse);
        int statusCode = response.getStatus();
        Response.Status status = Response.Status.fromStatusCode(statusCode);
        if (status == null) {
            Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
            return createExceptionForFamily(response, statusFamily);
        } else {
            switch(status) {
                case BAD_REQUEST:
                    return new BadRequestException(response);
                case UNAUTHORIZED:
                    return new NotAuthorizedException(response);
                case FORBIDDEN:
                    return new ForbiddenException(response);
                case NOT_FOUND:
                    return new NotFoundException(response);
                case METHOD_NOT_ALLOWED:
                    return new NotAllowedException(response);
                case NOT_ACCEPTABLE:
                    return new NotAcceptableException(response);
                case UNSUPPORTED_MEDIA_TYPE:
                    return new NotSupportedException(response);
                case INTERNAL_SERVER_ERROR:
                    return new InternalServerErrorException(response);
                case SERVICE_UNAVAILABLE:
                    return new ServiceUnavailableException(response);
                default:
                    Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
                    return createExceptionForFamily(response, statusFamily);
            }
        }
    } catch (Throwable t) {
        return new RuntimeException("Failed to convert response to exception", t);
    }
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) NotAllowedException(javax.ws.rs.NotAllowedException) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Response(javax.ws.rs.core.Response) NotAcceptableException(javax.ws.rs.NotAcceptableException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotSupportedException(javax.ws.rs.NotSupportedException)

Example 4 with NotAllowedException

use of javax.ws.rs.NotAllowedException in project candlepin by candlepin.

the class NotAllowedExceptionMapperTest method handleMethodNotAllowed.

@Test
public void handleMethodNotAllowed() {
    NotAllowedException mnae = new NotAllowedException("Not Allowed");
    NotAllowedExceptionMapper mnaem = injector.getInstance(NotAllowedExceptionMapper.class);
    Response r = mnaem.toResponse(mnae);
    assertEquals(405, r.getStatus());
    verifyMessage(r, rtmsg(".+ Not Allowed.*"));
}
Also used : Response(javax.ws.rs.core.Response) NotAllowedException(javax.ws.rs.NotAllowedException) Test(org.junit.Test)

Example 5 with NotAllowedException

use of javax.ws.rs.NotAllowedException in project com-liferay-apio-architect by liferay.

the class ExceptionSupplierUtilTest method testNotAllowedReturnsValidException.

@Test
public void testNotAllowedReturnsValidException() {
    NotAllowedException notAllowedException = notAllowed(POST, "a", "b", "c").get();
    String expected = POST.name() + " method is not allowed for path a/b/c";
    assertThat(notAllowedException.getMessage(), is(expected));
}
Also used : NotAllowedException(javax.ws.rs.NotAllowedException) Test(org.junit.Test)

Aggregations

NotAllowedException (javax.ws.rs.NotAllowedException)9 Response (javax.ws.rs.core.Response)5 NotFoundException (javax.ws.rs.NotFoundException)4 BadRequestException (javax.ws.rs.BadRequestException)3 NotAcceptableException (javax.ws.rs.NotAcceptableException)3 NotSupportedException (javax.ws.rs.NotSupportedException)3 Test (org.junit.Test)3 ForbiddenException (javax.ws.rs.ForbiddenException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)2 ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 Headers (io.undertow.util.Headers)1 InputStream (java.io.InputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 StandardOpenOption (java.nio.file.StandardOpenOption)1