Search in sources :

Example 1 with NotSupportedException

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

the class ParameterValueHelper method getParameterValues.

/**
     * Get the array of parameter values.
     *
     * @param valueProviders a list of value providers.
     * @return array of parameter values provided by the value providers.
     */
public static Object[] getParameterValues(List<ParamValueFactoryWithSource<?>> valueProviders) {
    final Object[] params = new Object[valueProviders.size()];
    try {
        int entityProviderIndex = -1;
        int index = 0;
        for (ParamValueFactoryWithSource<?> paramValProvider : valueProviders) {
            // entity provider has to be called last; see JERSEY-2642
            if (paramValProvider.getSource().equals(Parameter.Source.ENTITY)) {
                entityProviderIndex = index++;
                continue;
            }
            params[index++] = paramValProvider.get();
        }
        if (entityProviderIndex != -1) {
            params[entityProviderIndex] = valueProviders.get(entityProviderIndex).get();
        }
        return params;
    } catch (WebApplicationException e) {
        throw e;
    } catch (MessageBodyProviderNotFoundException e) {
        throw new NotSupportedException(e);
    } catch (ProcessingException e) {
        throw e;
    } catch (RuntimeException e) {
        if (e.getCause() instanceof WebApplicationException) {
            throw (WebApplicationException) e.getCause();
        }
        throw new MappableException("Exception obtaining parameters", e);
    }
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) NotSupportedException(javax.ws.rs.NotSupportedException) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with NotSupportedException

use of javax.ws.rs.NotSupportedException 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 3 with NotSupportedException

use of javax.ws.rs.NotSupportedException 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 4 with NotSupportedException

use of javax.ws.rs.NotSupportedException in project openremote by openremote.

the class TimerProtocol method processLinkedAttributeWrite.

@Override
protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
    AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
    TimerValue timerValue = TimerConfiguration.getValue(attribute).orElse(null);
    if (timerValue == null) {
        LOG.warning("Attribute doesn't have a valid timer value so ignoring write request: " + attribute.getReferenceOrThrow());
        return;
    }
    // Don't remove or alter any running timer just push update back through the system and wait for link/unlink
    // protocol configuration method call
    Optional<String> writeValue = event.getValue().flatMap(Values::getString).flatMap(TextUtil::asNonNullAndNonEmpty);
    switch(timerValue) {
        case ENABLED:
            // check event value is a boolean
            boolean enabled = Values.getBoolean(event.getValue().orElse(null)).orElseThrow(() -> new IllegalStateException("Writing to protocol configuration CONNECTED property requires a boolean value"));
            if (enabled == protocolConfiguration.isEnabled()) {
                LOG.finer("Protocol configuration enabled status is already: " + enabled);
            } else {
                LOG.fine("Updating protocol configuration enabled status");
                updateLinkedProtocolConfiguration(protocolConfiguration, protocolConfig -> protocolConfig.setDisabled(!enabled));
            }
            break;
        case CRON_EXPRESSION:
            // but that is handled gracefully
            if (!writeValue.isPresent()) {
                LOG.warning("Send to actuator value for time trigger must be a non empty string");
                return;
            }
            updateTimerValue(new AttributeState(protocolConfiguration.getReferenceOrThrow(), Values.create(writeValue.get().trim())));
            break;
        case TIME:
            if (!writeValue.isPresent()) {
                LOG.warning("Send to actuator value for time trigger must be a non empty string");
                return;
            }
            CronExpressionParser parser = cronExpressionMap.get(protocolConfiguration.getReferenceOrThrow());
            if (parser == null) {
                LOG.info("Ignoring trigger update because current cron expression is invalid");
                return;
            }
            String[] writeTimeValues = writeValue.get().trim().split(":");
            Integer hours;
            Integer minutes;
            Integer seconds;
            if (writeTimeValues.length != 3 || (hours = CronExpressionParser.parseNumberExpression(writeTimeValues[0])) == null || (minutes = CronExpressionParser.parseNumberExpression(writeTimeValues[1])) == null || (seconds = CronExpressionParser.parseNumberExpression(writeTimeValues[2])) == null) {
                LOG.info("Expected value to be in format HH:MM:SS, actual: " + writeValue);
                return;
            }
            parser.setTime(hours, minutes, seconds);
            updateTimerValue(new AttributeState(protocolConfiguration.getReferenceOrThrow(), Values.create(parser.buildCronExpression())));
            break;
        default:
            throw new NotSupportedException("Unsupported timer value: " + timerValue);
    }
}
Also used : TextUtil(org.openremote.model.util.TextUtil) AssetAttribute(org.openremote.model.asset.AssetAttribute) NotSupportedException(javax.ws.rs.NotSupportedException)

Example 5 with NotSupportedException

use of javax.ws.rs.NotSupportedException 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)

Aggregations

NotSupportedException (javax.ws.rs.NotSupportedException)14 WebApplicationException (javax.ws.rs.WebApplicationException)8 MediaType (javax.ws.rs.core.MediaType)5 Language (com.liferay.apio.architect.language.Language)4 RequestInfo (com.liferay.apio.architect.request.RequestInfo)4 Embedded (com.liferay.apio.architect.response.control.Embedded)4 Fields (com.liferay.apio.architect.response.control.Fields)4 ServerURL (com.liferay.apio.architect.url.ServerURL)4 ProviderManager (com.liferay.apio.architect.wiring.osgi.manager.ProviderManager)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 Annotation (java.lang.annotation.Annotation)4 Type (java.lang.reflect.Type)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 Locale (java.util.Locale)4 Optional (java.util.Optional)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4