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);
}
}
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;
}
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);
}
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;
}
Aggregations