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();
}
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);
}
}
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);
}
}
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.*"));
}
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));
}
Aggregations