Search in sources :

Example 11 with NotSupportedException

use of jakarta.ws.rs.NotSupportedException in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method constructorStringResponseThrowableTest.

/*
   * @testName: constructorStringResponseThrowableTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:1094; JAXRS:JAVADOC:12;
   * 
   * @test_Strategy: Construct a new unsupported media type exception.
   * getResponse
   */
public void constructorStringResponseThrowableTest() throws Fault {
    Throwable[] throwables = new Throwable[] { new RuntimeException(), new IOException(), new Error(), new Throwable() };
    for (Throwable t : throwables) {
        NotSupportedException e = new NotSupportedException(MESSAGE, buildResponse(STATUS), t);
        assertResponse(e, HOST);
        assertCause(e, t);
        assertMessage(e);
    }
}
Also used : IOException(java.io.IOException) NotSupportedException(jakarta.ws.rs.NotSupportedException)

Example 12 with NotSupportedException

use of jakarta.ws.rs.NotSupportedException in project mycore by MyCoRe-Org.

the class MCRErrorResponse method toException.

public WebApplicationException toException() {
    WebApplicationException e;
    Response.Status s = Response.Status.fromStatusCode(status);
    final Response response = Response.status(s).entity(this).build();
    // s maybe null
    switch(Response.Status.Family.familyOf(status)) {
        case CLIENT_ERROR:
            // Response.Status.OK is to trigger "default" case
            switch(s != null ? s : Response.Status.OK) {
                case BAD_REQUEST:
                    e = new BadRequestException(getMessage(), response, getCause());
                    break;
                case FORBIDDEN:
                    e = new ForbiddenException(getMessage(), response, getCause());
                    break;
                case NOT_ACCEPTABLE:
                    e = new NotAcceptableException(getMessage(), response, getCause());
                    break;
                case METHOD_NOT_ALLOWED:
                    e = new NotAllowedException(getMessage(), response, getCause());
                    break;
                case UNAUTHORIZED:
                    e = new NotAuthorizedException(getMessage(), response, getCause());
                    break;
                case NOT_FOUND:
                    e = new NotFoundException(getMessage(), response, getCause());
                    break;
                case UNSUPPORTED_MEDIA_TYPE:
                    e = new NotSupportedException(getMessage(), response, getCause());
                    break;
                default:
                    e = new ClientErrorException(getMessage(), response, getCause());
            }
            break;
        case SERVER_ERROR:
            // Response.Status.OK is to trigger "default" case
            switch(s != null ? s : Response.Status.OK) {
                case INTERNAL_SERVER_ERROR:
                    e = new InternalServerErrorException(getMessage(), response, getCause());
                    break;
                default:
                    e = new ServerErrorException(getMessage(), response, getCause());
            }
            break;
        default:
            e = new WebApplicationException(getMessage(), getCause(), response);
    }
    LogManager.getLogger().error(this::getLogMessage, e);
    return e;
}
Also used : ForbiddenException(jakarta.ws.rs.ForbiddenException) WebApplicationException(jakarta.ws.rs.WebApplicationException) NotAllowedException(jakarta.ws.rs.NotAllowedException) NotFoundException(jakarta.ws.rs.NotFoundException) NotAuthorizedException(jakarta.ws.rs.NotAuthorizedException) Response(jakarta.ws.rs.core.Response) NotAcceptableException(jakarta.ws.rs.NotAcceptableException) BadRequestException(jakarta.ws.rs.BadRequestException) ClientErrorException(jakarta.ws.rs.ClientErrorException) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) ServerErrorException(jakarta.ws.rs.ServerErrorException) NotSupportedException(jakarta.ws.rs.NotSupportedException)

Example 13 with NotSupportedException

use of jakarta.ws.rs.NotSupportedException in project resteasy by resteasy.

the class WebApplicationExceptionWrapper method wrap.

/**
 * If the {@code resteasy.original.webapplicationexception.behavior} is set to {@code true} or the request is
 * determined to not be a server side request, then the {@link WebApplicationException} passed in will be returned.
 * If the property is not set to {@code true} and this is a server side request then the exception is wrapped and
 * the response is {@linkplain #sanitize(Response) sanitized}.
 *
 * @param e the exception to possibly wrapped
 *
 * @return the wrapped exception or the original exception if the exception has already been wrapped the the
 * wrapping feature is turned off
 */
static WebApplicationException wrap(final WebApplicationException e) {
    final Configuration config = ConfigurationFactory.getInstance().getConfiguration();
    final boolean originalBehavior = config.getOptionalValue("resteasy.original.webapplicationexception.behavior", boolean.class).orElse(false);
    final boolean serverSide = ResteasyDeployment.onServer();
    if (originalBehavior || !serverSide) {
        return e;
    }
    if (e instanceof WebApplicationExceptionWrapper) {
        return e;
    }
    if (e instanceof BadRequestException) {
        return new ResteasyBadRequestException((BadRequestException) e);
    }
    if (e instanceof NotAuthorizedException) {
        return new ResteasyNotAuthorizedException((NotAuthorizedException) e);
    }
    if (e instanceof ForbiddenException) {
        return new ResteasyForbiddenException((ForbiddenException) e);
    }
    if (e instanceof NotFoundException) {
        return new ResteasyNotFoundException((NotFoundException) e);
    }
    if (e instanceof NotAllowedException) {
        return new ResteasyNotAllowedException((NotAllowedException) e);
    }
    if (e instanceof NotAcceptableException) {
        return new ResteasyNotAcceptableException((NotAcceptableException) e);
    }
    if (e instanceof NotSupportedException) {
        return new ResteasyNotSupportedException((NotSupportedException) e);
    }
    if (e instanceof InternalServerErrorException) {
        return new ResteasyInternalServerErrorException((InternalServerErrorException) e);
    }
    if (e instanceof ServiceUnavailableException) {
        return new ResteasyServiceUnavailableException((ServiceUnavailableException) e);
    }
    if (e instanceof ClientErrorException) {
        return new ResteasyClientErrorException((ClientErrorException) e);
    }
    if (e instanceof ServerErrorException) {
        return new ResteasyServerErrorException((ServerErrorException) e);
    }
    if (e instanceof RedirectionException) {
        return new ResteasyRedirectionException((RedirectionException) e);
    }
    return new ResteasyWebApplicationException(e);
}
Also used : Configuration(org.jboss.resteasy.spi.config.Configuration) NotAllowedException(jakarta.ws.rs.NotAllowedException) NotFoundException(jakarta.ws.rs.NotFoundException) NotAuthorizedException(jakarta.ws.rs.NotAuthorizedException) ServiceUnavailableException(jakarta.ws.rs.ServiceUnavailableException) RedirectionException(jakarta.ws.rs.RedirectionException) ForbiddenException(jakarta.ws.rs.ForbiddenException) NotAcceptableException(jakarta.ws.rs.NotAcceptableException) BadRequestException(jakarta.ws.rs.BadRequestException) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) ClientErrorException(jakarta.ws.rs.ClientErrorException) ServerErrorException(jakarta.ws.rs.ServerErrorException) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) NotSupportedException(jakarta.ws.rs.NotSupportedException)

Example 14 with NotSupportedException

use of jakarta.ws.rs.NotSupportedException in project resteasy by resteasy.

the class SegmentNode method match.

public MatchCache match(List<Match> matches, String httpMethod, HttpRequest request) {
    MediaType contentType = request.getHttpHeaders().getMediaType();
    List<MediaType> requestAccepts = request.getHttpHeaders().getAcceptableMediaTypes();
    List<WeightedMediaType> weightedAccepts = new ArrayList<WeightedMediaType>();
    for (MediaType accept : requestAccepts) weightedAccepts.add(WeightedMediaType.parse(accept));
    List<Match> list = new ArrayList<Match>();
    boolean methodMatch = false;
    boolean consumeMatch = false;
    // make a list of all compatible ResourceMethods
    for (Match match : matches) {
        ResourceMethodInvoker invoker = (ResourceMethodInvoker) match.expression.getInvoker();
        if (invoker.getHttpMethods().contains(httpMethod.toUpperCase())) {
            methodMatch = true;
            if (invoker.doesConsume(contentType)) {
                consumeMatch = true;
                if (invoker.doesProduce(weightedAccepts)) {
                    list.add(match);
                }
            }
        }
    }
    if (list.size() == 0) {
        if (!methodMatch) {
            HashSet<String> allowed = new HashSet<String>();
            for (Match match : matches) {
                allowed.addAll(((ResourceMethodInvoker) match.expression.getInvoker()).getHttpMethods());
            }
            if (httpMethod.equalsIgnoreCase("HEAD") && allowed.contains("GET")) {
                return match(matches, "GET", request);
            }
            if (allowed.contains("GET"))
                allowed.add("HEAD");
            allowed.add("OPTIONS");
            StringBuilder allowHeaders = new StringBuilder("");
            boolean first = true;
            for (String allow : allowed) {
                if (first)
                    first = false;
                else
                    allowHeaders.append(", ");
                allowHeaders.append(allow);
            }
            String allowHeaderValue = allowHeaders.toString();
            if (httpMethod.equals("OPTIONS")) {
                ResponseBuilder resBuilder = Response.ok(allowHeaderValue.toString(), MediaType.TEXT_PLAIN_TYPE).header(HttpHeaderNames.ALLOW, allowHeaderValue.toString());
                if (allowed.contains("PATCH")) {
                    Set<MediaType> patchAccepts = new HashSet<MediaType>(8);
                    for (Match match : matches) {
                        if (((ResourceMethodInvoker) match.expression.getInvoker()).getHttpMethods().contains("PATCH")) {
                            patchAccepts.addAll(Arrays.asList(((ResourceMethodInvoker) match.expression.getInvoker()).getConsumes()));
                        }
                    }
                    StringBuilder acceptPatch = new StringBuilder("");
                    first = true;
                    for (MediaType mediaType : patchAccepts) {
                        if (first)
                            first = false;
                        else
                            acceptPatch.append(", ");
                        acceptPatch.append(mediaType.toString());
                    }
                    resBuilder.header(HttpHeaderNames.ACCEPT_PATCH, acceptPatch.toString());
                }
                throw new DefaultOptionsMethodException(Messages.MESSAGES.noResourceMethodFoundForOptions(), resBuilder.build());
            } else {
                Response res = Response.status(HttpResponseCodes.SC_METHOD_NOT_ALLOWED).header(HttpHeaderNames.ALLOW, allowHeaderValue).build();
                throw new NotAllowedException(Messages.MESSAGES.noResourceMethodFoundForHttpMethod(httpMethod), res);
            }
        } else if (!consumeMatch) {
            throw new NotSupportedException(Messages.MESSAGES.cannotConsumeContentType());
        }
        throw new NotAcceptableException(Messages.MESSAGES.noMatchForAcceptHeader());
    }
    // if (list.size() == 1) return list.get(0); //don't do this optimization as we need to set chosen accept
    List<SortEntry> sortList = new ArrayList<SortEntry>();
    for (Match match : list) {
        ResourceMethodInvoker invoker = (ResourceMethodInvoker) match.expression.getInvoker();
        if (contentType == null)
            contentType = MediaType.WILDCARD_TYPE;
        MediaType[] consumes = invoker.getConsumes();
        if (consumes.length == 0) {
            consumes = WILDCARD_ARRAY;
        }
        MediaType[] produces = invoker.getProduces();
        if (produces.length == 0) {
            produces = WILDCARD_ARRAY;
        }
        List<SortFactor> consumeCombo = new ArrayList<SortFactor>();
        for (MediaType consume : consumes) {
            consumeCombo.add(createSortFactor(contentType, consume));
        }
        for (MediaType produce : produces) {
            List<MediaType> acceptableMediaTypes = requestAccepts;
            if (acceptableMediaTypes.size() == 0) {
                acceptableMediaTypes = DEFAULT_ACCEPTS;
            }
            for (MediaType accept : acceptableMediaTypes) {
                if (accept.isCompatible(produce)) {
                    SortFactor sortFactor = createSortFactor(accept, produce);
                    for (SortFactor consume : consumeCombo) {
                        sortList.add(new SortEntry(match, consume, sortFactor, produce));
                    }
                }
            }
        }
    }
    Collections.sort(sortList);
    SortEntry sortEntry = sortList.get(0);
    String[] mm = matchingMethods(sortList);
    if (mm != null) {
        boolean isFailFast = ConfigurationFactory.getInstance().getConfiguration().getOptionalValue(ResteasyContextParameters.RESTEASY_FAIL_FAST_ON_MULTIPLE_RESOURCES_MATCHING, boolean.class).orElse(false);
        if (isFailFast) {
            throw new RuntimeException(Messages.MESSAGES.multipleMethodsMatchFailFast(requestToString(request), mm));
        } else {
            LogMessages.LOGGER.multipleMethodsMatch(requestToString(request), mm);
        }
    }
    MediaType acceptType = sortEntry.getAcceptType();
    request.setAttribute(RESTEASY_CHOSEN_ACCEPT, acceptType);
    MatchCache ctx = new MatchCache();
    ctx.chosen = acceptType;
    ctx.match = sortEntry.match;
    ctx.invoker = sortEntry.match.expression.invoker;
    return ctx;
}
Also used : NotAllowedException(jakarta.ws.rs.NotAllowedException) ArrayList(java.util.ArrayList) MediaType(jakarta.ws.rs.core.MediaType) WeightedMediaType(org.jboss.resteasy.util.WeightedMediaType) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) WeightedMediaType(org.jboss.resteasy.util.WeightedMediaType) DefaultOptionsMethodException(org.jboss.resteasy.spi.DefaultOptionsMethodException) ResourceMethodInvoker(org.jboss.resteasy.core.ResourceMethodInvoker) Response(jakarta.ws.rs.core.Response) NotAcceptableException(jakarta.ws.rs.NotAcceptableException) NotSupportedException(jakarta.ws.rs.NotSupportedException)

Aggregations

NotSupportedException (jakarta.ws.rs.NotSupportedException)14 IOException (java.io.IOException)4 BadRequestException (jakarta.ws.rs.BadRequestException)3 NotAcceptableException (jakarta.ws.rs.NotAcceptableException)3 NotAllowedException (jakarta.ws.rs.NotAllowedException)3 Path (jakarta.ws.rs.Path)3 Timed (com.codahale.metrics.annotation.Timed)2 ClientErrorException (jakarta.ws.rs.ClientErrorException)2 ForbiddenException (jakarta.ws.rs.ForbiddenException)2 GET (jakarta.ws.rs.GET)2 InternalServerErrorException (jakarta.ws.rs.InternalServerErrorException)2 NotAuthorizedException (jakarta.ws.rs.NotAuthorizedException)2 NotFoundException (jakarta.ws.rs.NotFoundException)2 Produces (jakarta.ws.rs.Produces)2 ServerErrorException (jakarta.ws.rs.ServerErrorException)2 Response (jakarta.ws.rs.core.Response)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 HugeConfig (com.baidu.hugegraph.config.HugeConfig)1 TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)1