/** Test of createResource method, of class IntrospectionModeller. */
  @Test
  public void testCreateResource() {
    Class<?> resourceClass = HelloWorldResource.class;
    Resource result = Resource.builder(resourceClass).build();
    final List<ResourceMethod> resourceMethods = result.getResourceMethods();
    assertEquals(
        "Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());

    ResourceMethod resourceMethod;
    resourceMethod = find(resourceMethods, "postA");
    assertEquals(
        "Unexpected number of produced media types in the resource method model",
        3,
        resourceMethod.getProducedTypes().size());
    assertEquals(
        "Unexpected number of consumed media types in the resource method model",
        2,
        resourceMethod.getConsumedTypes().size());

    resourceMethod = find(resourceMethods, "postB");
    assertEquals(
        "Unexpected number of inherited produced media types in the resource method model",
        2,
        resourceMethod.getProducedTypes().size());
    assertEquals(
        "Unexpected number of inherited consumed media types in the resource method model",
        3,
        resourceMethod.getConsumedTypes().size());
  }
  @Override
  public ContainerResponse apply(final ContainerRequest requestContext) {
    final Object resource = routingContextFactory.get().peekMatchedResource();

    final ProcessingContext processingCtx = invocationContextFactory.get();
    if (method.isSuspendDeclared()) {
      processingCtx.setSuspendTimeout(method.getSuspendTimeout(), method.getSuspendTimeoutUnit());
    }
    requestContext.setProperty(ReaderInterceptorExecutor.INTERCEPTORS, getReaderInterceptors());
    requestContext.setProperty(WriterInterceptorExecutor.INTERCEPTORS, getWriterInterceptors());
    final Response response = dispatcher.dispatch(resource, requestContext);

    if (method.isSuspendDeclared()) {
      processingCtx.setResponse(resource);
      processingCtx.trySuspend();
    }

    final ContainerResponse responseContext = new ContainerResponse(requestContext, response);
    final Invocable invocable = method.getInvocable();
    responseContext.setEntityAnnotations(invocable.getHandlingMethod().getDeclaredAnnotations());

    if (responseContext.hasEntity()
        && !(responseContext.getEntityType() instanceof ParameterizedType)) {
      Type invocableType = invocable.getResponseType();
      if (invocableType != null
          && Void.TYPE != invocableType
          && Void.class != invocableType
          && invocableType != Response.class) {
        responseContext.setEntityType(invocableType);
      }
    }

    return responseContext;
  }
  private ResourceMethod find(List<ResourceMethod> methods, String javaMethodName) {
    for (ResourceMethod method : methods) {
      if (method.getInvocable().getHandlingMethod().getName().equals(javaMethodName)) {
        return method;
      }
    }

    return null;
  }
Пример #4
0
 @Override
 public com.sun.research.ws.wadl.Method createMethod(
     org.glassfish.jersey.server.model.Resource r, final ResourceMethod m) {
   com.sun.research.ws.wadl.Method wadlMethod = new com.sun.research.ws.wadl.Method();
   wadlMethod.setName(m.getHttpMethod());
   // TODO - J2
   wadlMethod.setId(m.getHttpMethod() + m.getPath());
   return wadlMethod;
 }
Пример #5
0
 @Override
 public com.sun.research.ws.wadl.Method createMethod(
     org.glassfish.jersey.server.model.Resource r, final ResourceMethod m) {
   com.sun.research.ws.wadl.Method wadlMethod = new com.sun.research.ws.wadl.Method();
   wadlMethod.setName(m.getHttpMethod());
   wadlMethod.setId(m.getInvocable().getDefinitionMethod().getName());
   if (m.isExtended()) {
     wadlMethod.getAny().add(WadlApplicationContextImpl.extendedElement);
   }
   return wadlMethod;
 }
  private void registerUnitOfWorkAnnotations(ResourceMethod method) {
    UnitOfWork annotation =
        method.getInvocable().getDefinitionMethod().getAnnotation(UnitOfWork.class);

    if (annotation == null) {
      annotation = method.getInvocable().getHandlingMethod().getAnnotation(UnitOfWork.class);
    }

    if (annotation != null) {
      this.methodMap.put(method.getInvocable().getDefinitionMethod(), annotation);
    }
  }
  /**
   * @param resource abstract resource
   * @param resourceMethod abstract resource method
   * @return response
   * @see
   *     org.glassfish.jersey.server.wadl.WadlGenerator#createResponses(org.glassfish.jersey.server.model.Resource,
   *     org.glassfish.jersey.server.model.ResourceMethod)
   */
  public List<Response> createResponses(
      org.glassfish.jersey.server.model.Resource resource,
      final org.glassfish.jersey.server.model.ResourceMethod resourceMethod) {
    final List<Response> responses =
        wadlGeneratorDelegate.createResponses(resource, resourceMethod);
    if (responses != null) {

      for (Response response : responses) {
        for (final Representation representation : response.getRepresentation()) {

          // Process each representation
          nameCallbacks.add(
              new TypeCallbackPair(
                  new GenericType(resourceMethod.getInvocable().getResponseType()),
                  new NameCallbackSetter() {

                    public void setName(QName name) {
                      representation.setElement(name);
                    }
                  }));
        }
      }
    }
    return responses;
  }
 private Object getResource(RoutingContext routingCtx) {
   final Object resource = routingCtx.peekMatchedResource();
   try {
     Method handlingMethod = locatorModel.getInvocable().getHandlingMethod();
     return handlingMethod.invoke(
         resource, ParameterValueHelper.getParameterValues(valueProviders));
   } catch (IllegalAccessException ex) {
     throw new ProcessingException("Resource Java method invocation error.", ex);
   } catch (InvocationTargetException ex) {
     final Throwable cause = ex.getCause();
     if (cause instanceof ProcessingException) {
       throw (ProcessingException) cause;
     }
     // exception cause potentially mappable
     throw new MappableException(cause);
   } catch (UndeclaredThrowableException ex) {
     throw new ProcessingException("Resource Java method invocation error.", ex);
   } catch (ProcessingException ex) {
     throw ex;
   } catch (Exception ex) {
     // exception potentially mappable
     throw new MappableException(ex);
   } catch (Throwable t) {
     throw new ProcessingException(t);
   }
 }
 /**
  * Create a new sub-resource locator router.
  *
  * @param injector HK2 injector.
  * @param services HK2 services.
  * @param runtimeModelBuilder Runtime model builder.
  * @param locatorModel resource locator method model.
  */
 public SubResourceLocatorRouter(
     final Injector injector,
     final Services services,
     final RuntimeModelBuilder runtimeModelBuilder,
     final ResourceMethod locatorModel) {
   this.injector = injector;
   this.runtimeModelBuilder = runtimeModelBuilder;
   this.locatorModel = locatorModel;
   this.valueProviders =
       ParameterValueHelper.createValueProviders(services, locatorModel.getInvocable());
 }
Пример #10
0
  private Router createSingleMethodAcceptor(
      final ResourceMethod resourceMethod, final boolean subResourceMode) {
    Router methodAcceptor = null;
    switch (resourceMethod.getType()) {
      case RESOURCE_METHOD:
      case SUB_RESOURCE_METHOD:
        methodAcceptor = Routers.asTreeAcceptor(createInflector(resourceMethod));
        break;
      case SUB_RESOURCE_LOCATOR:
        methodAcceptor = new SubResourceLocatorRouter(locator, this, resourceMethod);
        break;
    }

    // TODO: solve this via instance-based method handler model?
    if (subResourceMode) {
      return methodAcceptor;
    } else {
      return pushHandlerAcceptorBuilder.build(
          resourceMethod.getInvocable().getHandler(), methodAcceptor);
    }
  }
Пример #11
0
  @Test
  public void resourceTest() {

    Resource.Builder builder = Resource.builder(LightMareBean.class);
    Resource resource = builder.build();
    System.out.println(resource.getName());
    System.out.println(resource);

    List<ResourceMethod> methods = resource.getAllMethods();
    // ResourceMethod.Builder methodBuilder;
    // String name = resource.getName();
    Collection<Class<?>> handlers = resource.getHandlerClasses();
    System.out.println(handlers);
    Class<?> beanClass;
    Method realMethod;
    List<Parameter> parameters;
    for (ResourceMethod method : methods) {
      System.out.println(method);
      realMethod = method.getInvocable().getHandlingMethod();
      realMethod.getParameterTypes();
      MethodHandler handler = method.getInvocable().getHandler();
      List<? extends ResourceModelComponent> components = method.getInvocable().getComponents();
      parameters = method.getInvocable().getParameters();
      MethodHandler methodHandler = method.getInvocable().getHandler();
      System.out.println(methodHandler);
      for (Parameter parameter : parameters) {
        System.out.println(parameter);
        System.out.println(parameter.getRawType());
      }
      System.out.println(components);
      beanClass = handler.getHandlerClass();
      System.out.println(beanClass);
      System.out.println(realMethod);
    }
  }
Пример #12
0
  @Override
  public List<Response> createResponses(
      org.glassfish.jersey.server.model.Resource r, ResourceMethod m) {
    final Response response = new Response();

    for (MediaType mediaType : m.getProducedTypes()) {
      if (!MediaType.WILDCARD_TYPE.equals(mediaType) || !hasEmptyProducibleMediaTypeSet(m)) {
        Representation wadlRepresentation = createResponseRepresentation(r, m, mediaType);
        response.getRepresentation().add(wadlRepresentation);
      }
    }

    List<Response> responses = new ArrayList<Response>();
    responses.add(response);
    return responses;
  }
Пример #13
0
  @Override
  public List<Response> createResponses(
      org.glassfish.jersey.server.model.Resource r, ResourceMethod m) {
    final Response response = new Response();

    // add mediaType="*/*" in case that no mediaType was specified
    if (hasEmptyProducibleMediaTypeSet(m)) {
      Representation wadlRepresentation =
          createResponseRepresentation(r, m, MediaType.WILDCARD_TYPE);
      response.getRepresentation().add(wadlRepresentation);
    } else {
      for (MediaType mediaType : m.getProducedTypes()) {
        Representation wadlRepresentation = createResponseRepresentation(r, m, mediaType);
        response.getRepresentation().add(wadlRepresentation);
      }
    }

    List<Response> responses = new ArrayList<Response>();
    responses.add(response);
    return responses;
  }
  /**
   * @param ar abstract resource
   * @param arm abstract resource method
   * @param mt media type
   * @return respresentation type
   * @see org.glassfish.jersey.server.wadl.WadlGenerator# createRequestRepresentation
   *     (org.glassfish.jersey.server.model.Resource,
   *     org.glassfish.jersey.server.model.ResourceMethod, javax.ws.rs.core.MediaType)
   */
  public Representation createRequestRepresentation(
      org.glassfish.jersey.server.model.Resource ar,
      org.glassfish.jersey.server.model.ResourceMethod arm,
      MediaType mt) {

    final Representation rt = wadlGeneratorDelegate.createRequestRepresentation(ar, arm, mt);

    for (Parameter p : arm.getInvocable().getParameters()) {
      if (p.getSource() == Parameter.Source.ENTITY) {
        nameCallbacks.add(
            new TypeCallbackPair(
                new GenericType(p.getType()),
                new NameCallbackSetter() {
                  @Override
                  public void setName(QName name) {
                    rt.setElement(name);
                  }
                }));
      }
    }

    return rt;
  }
Пример #15
0
  private ResourceMethodInvoker(
      Provider<RoutingContext> routingContextFactory,
      Provider<ProcessingContext> invocationContextFactory,
      ResourceMethodDispatcher.Provider dispatcherProvider,
      ResourceMethodInvocationHandlerProvider invocationHandlerProvider,
      ResourceMethod method,
      MultivaluedMap<Class<? extends Annotation>, ContainerRequestFilter> nameBoundRequestFilters,
      MultivaluedMap<Class<? extends Annotation>, ContainerResponseFilter> nameBoundResponseFilters,
      Collection<ReaderInterceptor> globalReaderInterceptors,
      Collection<WriterInterceptor> globalWriterInterceptors,
      MultivaluedMap<Class<? extends Annotation>, ReaderInterceptor> nameBoundReaderInterceptors,
      MultivaluedMap<Class<? extends Annotation>, WriterInterceptor> nameBoundWriterInterceptors,
      Collection<DynamicBinder> dynamicBinders) {

    this.routingContextFactory = routingContextFactory;
    this.invocationContextFactory = invocationContextFactory;

    this.method = method;
    final Invocable invocable = method.getInvocable();
    this.dispatcher =
        dispatcherProvider.create(invocable, invocationHandlerProvider.create(invocable));

    this.resourceMethod = invocable.getHandlingMethod();
    this.resourceClass = invocable.getHandler().getHandlerClass();

    List<ReaderInterceptor> _readerInterceptors = new LinkedList<ReaderInterceptor>();
    List<WriterInterceptor> _writerInterceptors = new LinkedList<WriterInterceptor>();

    for (DynamicBinder dynamicBinder : dynamicBinders) {
      Object boundProvider = dynamicBinder.getBoundProvider(this);

      // TODO: should be based on the type arg. value rather than instanceof?
      if (boundProvider instanceof WriterInterceptor) {
        _writerInterceptors.add((WriterInterceptor) boundProvider);
      }

      if (boundProvider instanceof ReaderInterceptor) {
        _readerInterceptors.add((ReaderInterceptor) boundProvider);
      }

      if (boundProvider instanceof ContainerRequestFilter) {
        this.requestFilters.add((ContainerRequestFilter) boundProvider);
      }

      if (boundProvider instanceof ContainerResponseFilter) {
        this.responseFilters.add((ContainerResponseFilter) boundProvider);
      }
    }

    _readerInterceptors.addAll(globalReaderInterceptors);
    _writerInterceptors.addAll(globalWriterInterceptors);

    if (resourceMethod != null) {
      addNameBoundFiltersAndInterceptors(
          nameBoundRequestFilters,
          nameBoundResponseFilters,
          nameBoundReaderInterceptors,
          nameBoundWriterInterceptors,
          this.requestFilters,
          this.responseFilters,
          _readerInterceptors,
          _writerInterceptors,
          method);
    }

    Collections.sort(
        _readerInterceptors,
        new PriorityComparator<ReaderInterceptor>(PriorityComparator.Order.ASCENDING));
    Collections.sort(
        _writerInterceptors,
        new PriorityComparator<WriterInterceptor>(PriorityComparator.Order.ASCENDING));

    this.readerInterceptors = Collections.unmodifiableList(_readerInterceptors);
    this.writerInterceptors = Collections.unmodifiableList(_writerInterceptors);
  }
Пример #16
0
 @Override
 public String toString() {
   return method.getInvocable().getHandlingMethod().toString();
 }
Пример #17
0
 private boolean hasEmptyProducibleMediaTypeSet(final ResourceMethod method) {
   return method.getProducedTypes().isEmpty();
 }