Search in sources :

Example 6 with Parameter

use of org.glassfish.jersey.server.model.Parameter in project jersey by jersey.

the class WadlBuilder method generateRequest.

private Request generateRequest(org.glassfish.jersey.server.model.Resource parentResource, final org.glassfish.jersey.server.model.ResourceMethod resourceMethod, Map<String, Param> wadlResourceParams) {
    try {
        final List<Parameter> requestParams = new LinkedList<>(resourceMethod.getInvocable().getParameters());
        // Adding handler instance parameters to the list of potential request parameters.
        requestParams.addAll(resourceMethod.getInvocable().getHandler().getParameters());
        if (requestParams.isEmpty()) {
            return null;
        }
        Request wadlRequest = _wadlGenerator.createRequest(parentResource, resourceMethod);
        processRequestParameters(parentResource, resourceMethod, wadlResourceParams, requestParams, wadlRequest);
        if (wadlRequest.getRepresentation().size() + wadlRequest.getParam().size() == 0) {
            return null;
        } else {
            return wadlRequest;
        }
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_REQUEST(resourceMethod, parentResource), e);
    }
}
Also used : Request(com.sun.research.ws.wadl.Request) Parameter(org.glassfish.jersey.server.model.Parameter) LinkedList(java.util.LinkedList) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 7 with Parameter

use of org.glassfish.jersey.server.model.Parameter in project jersey by jersey.

the class WadlBuilder method generateResource.

private Resource generateResource(final org.glassfish.jersey.server.model.Resource resource, String path, Set<org.glassfish.jersey.server.model.Resource> visitedResources) {
    try {
        if (!detailedWadl && resource.isExtended()) {
            return null;
        }
        Resource wadlResource = _wadlGenerator.createResource(resource, path);
        // prevent infinite recursion
        if (visitedResources.contains(resource)) {
            return wadlResource;
        } else {
            visitedResources = new HashSet<>(visitedResources);
            visitedResources.add(resource);
        }
        // if the resource contains subresource locator create new resource for this locator and return it instead
        // of this resource
        final ResourceMethod locator = resource.getResourceLocator();
        if (locator != null) {
            try {
                org.glassfish.jersey.server.model.Resource.Builder builder = org.glassfish.jersey.server.model.Resource.builder(locator.getInvocable().getRawResponseType());
                if (builder == null) {
                    // for example in the case the return type of the sub resource locator is Object
                    builder = org.glassfish.jersey.server.model.Resource.builder().path(resource.getPath());
                }
                org.glassfish.jersey.server.model.Resource subResource = builder.build();
                Resource wadlSubResource = generateResource(subResource, resource.getPath(), visitedResources);
                if (wadlSubResource == null) {
                    return null;
                }
                if (locator.isExtended()) {
                    wadlSubResource.getAny().add(WadlApplicationContextImpl.EXTENDED_ELEMENT);
                }
                for (Parameter param : locator.getInvocable().getParameters()) {
                    Param wadlParam = generateParam(resource, locator, param);
                    if (wadlParam != null && wadlParam.getStyle() == ParamStyle.TEMPLATE) {
                        wadlSubResource.getParam().add(wadlParam);
                    }
                }
                return wadlSubResource;
            } catch (RuntimeException e) {
                throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_LOCATOR(locator, resource), e);
            }
        }
        Map<String, Param> wadlResourceParams = new HashMap<>();
        // for each resource method
        for (org.glassfish.jersey.server.model.ResourceMethod method : resource.getResourceMethods()) {
            if (!detailedWadl && method.isExtended()) {
                continue;
            }
            com.sun.research.ws.wadl.Method wadlMethod = generateMethod(resource, wadlResourceParams, method);
            wadlResource.getMethodOrResource().add(wadlMethod);
        }
        // add method parameters that are associated with the resource PATH template
        for (Param wadlParam : wadlResourceParams.values()) {
            wadlResource.getParam().add(wadlParam);
        }
        // for each sub-resource method
        Map<String, Resource> wadlSubResources = new HashMap<>();
        Map<String, Map<String, Param>> wadlSubResourcesParams = new HashMap<>();
        for (org.glassfish.jersey.server.model.Resource childResource : resource.getChildResources()) {
            Resource childWadlResource = generateResource(childResource, childResource.getPath(), visitedResources);
            if (childWadlResource == null) {
                continue;
            }
            wadlResource.getMethodOrResource().add(childWadlResource);
        }
        return wadlResource;
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_PATH(resource, path), e);
    }
}
Also used : HashMap(java.util.HashMap) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ProcessingException(javax.ws.rs.ProcessingException) Resource(com.sun.research.ws.wadl.Resource) ProcessingException(javax.ws.rs.ProcessingException) Param(com.sun.research.ws.wadl.Param) FormParam(javax.ws.rs.FormParam) Parameter(org.glassfish.jersey.server.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 8 with Parameter

use of org.glassfish.jersey.server.model.Parameter in project dropwizard by dropwizard.

the class ConstraintMessage method isRequestEntity.

/**
     * Determines if constraint violation occurred in the request entity. If it did, return a client
     * friendly string representation of where the error occurred (eg. "patient.name")
     */
public static Optional<String> isRequestEntity(ConstraintViolation<?> violation, Invocable invocable) {
    final Path.Node parent = Iterables.get(violation.getPropertyPath(), 1, null);
    if (parent == null) {
        return Optional.empty();
    }
    final List<Parameter> parameters = invocable.getParameters();
    switch(parent.getKind()) {
        case PARAMETER:
            final Parameter param = parameters.get(parent.as(Path.ParameterNode.class).getParameterIndex());
            if (param.getSource().equals(Parameter.Source.UNKNOWN)) {
                return Optional.of(Joiner.on('.').join(Iterables.skip(violation.getPropertyPath(), 2)));
            }
        default:
            break;
    }
    return Optional.empty();
}
Also used : Path(javax.validation.Path) Parameter(org.glassfish.jersey.server.model.Parameter)

Example 9 with Parameter

use of org.glassfish.jersey.server.model.Parameter in project jersey by jersey.

the class JavaResourceMethodDispatcherProvider method create.

@Override
public ResourceMethodDispatcher create(final Invocable resourceMethod, final InvocationHandler invocationHandler, final ConfiguredValidator validator) {
    final List<ParamValueFactoryWithSource<?>> valueProviders = ParameterValueHelper.createValueProviders(injectionManager, resourceMethod);
    final Class<?> returnType = resourceMethod.getHandlingMethod().getReturnType();
    ResourceMethodDispatcher resourceMethodDispatcher = null;
    if (Response.class.isAssignableFrom(returnType)) {
        resourceMethodDispatcher = new ResponseOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
    } else if (returnType != void.class) {
        if (returnType == Object.class || GenericEntity.class.isAssignableFrom(returnType)) {
            resourceMethodDispatcher = new ObjectOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
        } else {
            resourceMethodDispatcher = new TypeOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
        }
    } else {
        // return type is void
        int i = 0;
        for (final Parameter parameter : resourceMethod.getParameters()) {
            if (SseEventSink.class.equals(parameter.getRawType())) {
                resourceMethodDispatcher = new SseEventSinkInvoker(resourceMethod, invocationHandler, valueProviders, validator, i);
                break;
            }
            i++;
        }
        if (resourceMethodDispatcher == null) {
            resourceMethodDispatcher = new VoidOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
        }
    }
    // Inject validator.
    injectionManager.inject(resourceMethodDispatcher);
    return resourceMethodDispatcher;
}
Also used : SseEventSink(javax.ws.rs.sse.SseEventSink) ResourceMethodDispatcher(org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher) Parameter(org.glassfish.jersey.server.model.Parameter) ParamValueFactoryWithSource(org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource)

Example 10 with Parameter

use of org.glassfish.jersey.server.model.Parameter in project jersey by jersey.

the class MethodSelectingRouter method isReadable.

private boolean isReadable(final RequestSpecificConsumesProducesAcceptor candidate) {
    final Invocable invocable = candidate.methodRouting.method.getInvocable();
    final Method handlingMethod = invocable.getHandlingMethod();
    final Parameter entityParam = getEntityParam(invocable);
    if (entityParam == null) {
        return true;
    } else {
        final Class<?> entityType = entityParam.getRawType();
        for (final ReaderModel model : workers.getReaderModelsForType(entityType)) {
            if (model.isReadable(entityType, entityParam.getType(), handlingMethod.getDeclaredAnnotations(), candidate.consumes.combinedType)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Parameter(org.glassfish.jersey.server.model.Parameter) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Aggregations

Parameter (org.glassfish.jersey.server.model.Parameter)10 Path (javax.validation.Path)3 ProcessingException (javax.ws.rs.ProcessingException)3 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)2 ImmutableList (com.google.common.collect.ImmutableList)1 Param (com.sun.research.ws.wadl.Param)1 Request (com.sun.research.ws.wadl.Request)1 Resource (com.sun.research.ws.wadl.Resource)1 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1