org.glassfish.jersey.server.model.ResourceMethod类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(96)

本文整理了Java中org.glassfish.jersey.server.model.ResourceMethod类的一些代码示例,展示了ResourceMethod类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResourceMethod类的具体详情如下:
包路径:org.glassfish.jersey.server.model.ResourceMethod
类名称:ResourceMethod

ResourceMethod介绍

[英]Model of a method available on a resource. Covers resource method, sub-resource method and sub-resource locator.
[中]资源上可用方法的模型。包括资源方法、子资源方法和子资源定位器。

代码示例

代码示例来源:origin: jersey/jersey

/**
 * Create a builder from an existing resource method model.
 *
 * @param parent         parent resource model builder.
 * @param originalMethod existing resource method model to create the builder from.
 */
/* package */ Builder(final Resource.Builder parent, ResourceMethod originalMethod) {
  this.parent = parent;
  this.consumedTypes = new LinkedHashSet<>(originalMethod.getConsumedTypes());
  this.producedTypes = new LinkedHashSet<>(originalMethod.getProducedTypes());
  this.suspended = originalMethod.isSuspendDeclared();
  this.suspendTimeout = originalMethod.getSuspendTimeout();
  this.suspendTimeoutUnit = originalMethod.getSuspendTimeoutUnit();
  this.handlerParameters = new LinkedHashSet<>(originalMethod.getInvocable().getHandler().getParameters());
  this.nameBindings = originalMethod.getNameBindings();
  this.httpMethod = originalMethod.getHttpMethod();
  this.managedAsync = originalMethod.isManagedAsyncDeclared();
  Invocable invocable = originalMethod.getInvocable();
  this.handlingMethod = invocable.getHandlingMethod();
  this.encodedParams = false;
  this.routingResponseType = invocable.getRoutingResponseType();
  this.extended = originalMethod.isExtended();
  Method handlerMethod = invocable.getDefinitionMethod();
  MethodHandler handler = invocable.getHandler();
  if (handler.isClassBased()) {
    handledBy(handler.getHandlerClass(), handlerMethod);
  } else {
    handledBy(handler.getHandlerInstance(), handlerMethod);
  }
}

代码示例来源:origin: jersey/jersey

/**
 * Get the method unique string ID. The ID is constructed from method attributes separated
 * by pipe '|'. The attributes are used in the following order:
 * method-produces|method-consumes|http-method|method-path|method-java-name
 * <p>
 *     If any of the attributes is not defined, "null" is used for such an attribute.
 * <p/>
 *
 * @param method Resource method.
 * @return String constructed from resource method parameters.
 */
public static String getMethodUniqueId(final ResourceMethod method) {
  final String path = method.getParent() != null ? createPath(method.getParent()) : "null";
  return method.getProducedTypes().toString() + "|"
      + method.getConsumedTypes().toString() + "|"
      + method.getHttpMethod() + "|"
      + path + "|"
      + method.getInvocable().getHandlingMethod().getName();
}

代码示例来源:origin: jersey/jersey

@Override
public void visitResourceMethod(final ResourceMethod resourceMethod) {
  if (resourceMethod.isExtended()) {
    return;
  }
  if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) {
    if (resourceMethod.getInvocable() != null) {
      final Invocable i = resourceMethod.getInvocable();
      final Type type = i.getResponseType();
      final StringBuilder template = getTemplate();
      mappings.put((Class) type, getMapping(template));
      // Process sub resources ?
      Resource.Builder builder = Resource
          .builder(i.getRawResponseType());
      if (builder == null) {
        // for example in the case the return type of the sub resource locator is Object
        builder = Resource.builder().path(resourceMethod.getParent().getPath());
      }
      final Resource subResource = builder.build();
      visitChildResource(subResource);
    }
  }
  processComponents(resourceMethod);
}

代码示例来源:origin: jersey/jersey

private List<MediaType> getEffectiveInputTypes(final ResourceMethod resourceMethod) {
  if (!resourceMethod.getConsumedTypes().isEmpty()) {
    return resourceMethod.getConsumedTypes();
  }
  List<MediaType> result = new LinkedList<>();
  if (workers != null) {
    for (Parameter p : resourceMethod.getInvocable().getParameters()) {
      if (p.getSource() == Parameter.Source.ENTITY) {
        result.addAll(workers.getMessageBodyReaderMediaTypes(
            p.getRawType(), p.getType(), p.getDeclaredAnnotations()));
      }
    }
  }
  return result.isEmpty() ? StarTypeList : result;
}

代码示例来源:origin: jersey/jersey

private List<MediaType> getEffectiveOutputTypes(final ResourceMethod resourceMethod) {
  if (!resourceMethod.getProducedTypes().isEmpty()) {
    return resourceMethod.getProducedTypes();
  }
  List<MediaType> result = new LinkedList<>();
  if (workers != null) {
    final Invocable invocable = resourceMethod.getInvocable();
    result.addAll(workers.getMessageBodyWriterMediaTypes(
        invocable.getRawResponseType(),
        invocable.getResponseType(),
        invocable.getHandlingMethod().getAnnotations()));
  }
  return result.isEmpty() ? StarTypeList : result;
}

代码示例来源:origin: jersey/jersey

try {
  if (!detailedWadl && resource.isExtended()) {
    return null;
  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) {
        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);
  for (org.glassfish.jersey.server.model.ResourceMethod method : resource.getResourceMethods()) {
    if (!detailedWadl && method.isExtended()) {
      continue;
  throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_PATH(resource, path), e);

代码示例来源:origin: jersey/jersey

private void checkMethod(ResourceMethod method) {
  checkValueProviders(method);
  final Invocable invocable = method.getInvocable();
  if ("GET".equals(method.getHttpMethod())) {
    final long eventSinkCount = invocable.getParameters()
        .stream()
        .filter(parameter -> SseEventSink.class.equals(parameter.getRawType()))
        .count();
      Errors.warning(method, LocalizationMessages.MULTIPLE_EVENT_SINK_INJECTION(invocable.getHandlingMethod()));
    if (void.class == invocable.getHandlingMethod().getReturnType() && !method.isSuspendDeclared() && !isSse) {
      Errors.hint(method, LocalizationMessages.GET_RETURNS_VOID(invocable.getHandlingMethod()));
    for (Parameter p : invocable.getParameters()) {
      if (p.isAnnotationPresent(FormParam.class)) {
        Errors.fatal(method, LocalizationMessages.GET_CONSUMES_FORM_PARAM(invocable.getHandlingMethod()));
        break;

代码示例来源:origin: jersey/jersey

private Object getResource(final RequestProcessingContext context) {
  final Object resource = context.routingContext().peekMatchedResource();
  final Method handlingMethod = locatorModel.getInvocable().getHandlingMethod();
  final Object[] parameterValues = ParameterValueHelper.getParameterValues(valueProviders, context.request());
      return handlingMethod.invoke(resource, parameterValues);
    } catch (IllegalAccessException | IllegalArgumentException | UndeclaredThrowableException ex) {
      throw new ProcessingException(LocalizationMessages.ERROR_RESOURCE_JAVA_METHOD_INVOCATION(), ex);
    } catch (final InvocationTargetException ex) {
      final Throwable cause = ex.getCause();
      throw new ProcessingException(t);

代码示例来源:origin: jersey/jersey

final Class<?> handlerClass = resourceMethod.getInvocable().getHandler().getHandlerClass();
final Class<?>[] paramTypes = resourceMethod.getInvocable().getHandlingMethod().getParameterTypes();
this.name = resourceMethod.getInvocable().getHandlingMethod().getName();
StringBuilder params = new StringBuilder();
for (Class<?> type : paramTypes) {
  path = "N/A";
} else {
  path = resourceMethod.getParent().getParent() == null ? "" : resourceMethod.getParent().getPath();
String beanName = resourceMethod.getHttpMethod() + "->";
if (uriResource) {
  beanName += handlerClass.getSimpleName()

代码示例来源:origin: jersey/jersey

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

代码示例来源:origin: jersey/jersey

private List<Response> generateResponses(org.glassfish.jersey.server.model.Resource r, final ResourceMethod m) {
    try {
      if (m.getInvocable().getRawResponseType() == void.class) {
        return null;
      }
      return _wadlGenerator.createResponses(r, m);
    } catch (Exception e) {
      throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESPONSE(m, r), e);
    }
  }
}

代码示例来源:origin: com.carecon.fabric3/fabric3-binding-rs-jersey

private void createMethod(Resource.Builder resourceBuilder,
             ResourceMethod template,
             F3ResourceHandler handler,
             String[] consumeTypes,
             String[] produceTypes) {
  ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(template.getHttpMethod());
  methodBuilder.consumes(template.getConsumedTypes());
  methodBuilder.consumes(consumeTypes);
  methodBuilder.produces(template.getProducedTypes());
  methodBuilder.produces(produceTypes);
  methodBuilder.handledBy(handler, template.getInvocable().getHandlingMethod());
  if (template.isSuspendDeclared()) {
    methodBuilder.suspended(template.getSuspendTimeout(), template.getSuspendTimeoutUnit());
  }
  if (template.isManagedAsyncDeclared()) {
    methodBuilder.managedAsync();
  }
}

代码示例来源:origin: jersey/jersey

private Resource processResource(Resource resource) {
  Resource.Builder resourceBuilder = Resource.builder(resource.getPath());
  for (ResourceMethod resourceMethod : resource.getResourceMethods()) {
    ResourceMethod.Builder builder = resourceBuilder.addMethod(resourceMethod);
    if (resourceMethod.getInvocable().getHandlingMethod().isAnnotationPresent(Template.class)) {
      builder.routingResponseType(Viewable.class);
    }
  }
  if (resource.getResourceLocator() != null) {
    resourceBuilder.addMethod(resource.getResourceLocator());
  }
  for (Resource child : resource.getChildResources()) {
    resourceBuilder.addChildResource(processResource(child));
  }
  return resourceBuilder.build();
}

代码示例来源:origin: jersey/jersey

final ResponseDocType responseDoc = resourceDoc.getResponse(m.getInvocable().getDefinitionMethod().getDeclaringClass(),
    m.getInvocable().getDefinitionMethod());
List<Response> responses = new ArrayList<Response>();
if (responseDoc != null && responseDoc.hasRepresentations()) {
      final Param param = new Param();
      param.setName(wadlParamType.getName());
      param.setStyle(ParamStyle.fromValue(wadlParamType.getStyle()));
      param.setType(wadlParamType.getType());
      addDoc(param.getDoc(), wadlParamType.getDoc());

代码示例来源:origin: dropwizard/dropwizard

private List<EndpointLogLine> logMethodLines(Resource resource, String contextPath) {
  final List<EndpointLogLine> methodLines = new ArrayList<>();
  for (ResourceMethod method : resource.getAllMethods()) {
    if ("OPTIONS".equalsIgnoreCase(method.getHttpMethod())) {
      continue;
    final String path = cleanUpPath(contextPath + Strings.nullToEmpty(resource.getPath()));
    final Class<?> handler = method.getInvocable().getHandler().getHandlerClass();
    switch (method.getType()) {
      case RESOURCE_METHOD:
        methodLines.add(new EndpointLogLine(method.getHttpMethod(), path, handler));
        break;
      case SUB_RESOURCE_LOCATOR:
        final ResolvedType responseType = TYPE_RESOLVER
            .resolve(method.getInvocable().getResponseType());
        final Class<?> erasedType = !responseType.getTypeBindings().isEmpty() ?
            responseType.getTypeBindings().getBoundType(0).getErasedType() :
            responseType.getErasedType();
        final Resource res = Resource.from(erasedType);
        if (res == null) {
          methodLines.add(new EndpointLogLine(method.getHttpMethod(), path, handler));
        } else {
          methodLines.addAll(logResourceLines(res, path));

代码示例来源:origin: jersey/jersey

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

代码示例来源:origin: jersey/jersey

final Object resource = processingContext.routingContext().peekMatchedResource();
if (method.isSuspendDeclared() || method.isManagedAsyncDeclared() || method.isSse()) {
  if (!processingContext.asyncContext().suspend()) {
    throw new ProcessingException(LocalizationMessages.ERROR_SUSPENDING_ASYNC_REQUEST());
if (method.isManagedAsyncDeclared()) {
  processingContext.asyncContext().invokeManaged(() -> {
    final Response response = invoke(processingContext, resource);
    if (method.isSuspendDeclared()) {
  if (method.isSse()) {
    return null;
        throw new ProcessingException(LocalizationMessages.ERROR_SUSPENDING_ASYNC_REQUEST());

代码示例来源:origin: jersey/jersey

/**
 * @param r Jersey resource component.
 * @param m resource method.
 * @param p method parameter.
 * @return the enhanced {@link Param}.
 * @see org.glassfish.jersey.server.wadl.WadlGenerator#createParam(org.glassfish.jersey.server.model.Resource,
 * org.glassfish.jersey.server.model.ResourceMethod, org.glassfish.jersey.server.model.Parameter)
 */
public Param createParam(final org.glassfish.jersey.server.model.Resource r,
             final org.glassfish.jersey.server.model.ResourceMethod m, final Parameter p) {
  final Param result = delegate.createParam(r, m, p);
  if (result != null) {
    final ParamDocType paramDoc = resourceDoc.getParamDoc(m.getInvocable().getDefinitionMethod().getDeclaringClass(),
        m.getInvocable().getDefinitionMethod(), p);
    if (paramDoc != null && !isEmpty(paramDoc.getCommentText())) {
      final Doc doc = new Doc();
      doc.getContent().add(paramDoc.getCommentText());
      result.getDoc().add(doc);
    }
  }
  return result;
}

代码示例来源:origin: jersey/jersey

private void checkParameters(ResourceMethod method) {
  final Invocable invocable = method.getInvocable();
  final Method handlingMethod = invocable.getHandlingMethod();
  int paramCount = 0;
  int nonAnnotatedParameters = 0;
  for (Parameter p : invocable.getParameters()) {
    validateParameter(p, handlingMethod, handlingMethod.toGenericString(), Integer.toString(++paramCount), false);
    if (method.getType() == ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR
        && Parameter.Source.ENTITY == p.getSource()) {
      Errors.fatal(method, LocalizationMessages.SUBRES_LOC_HAS_ENTITY_PARAM(invocable.getHandlingMethod()));
    } else if (p.getAnnotations().length == 0) {
      nonAnnotatedParameters++;
      if (nonAnnotatedParameters > 1) {
        Errors.fatal(method, LocalizationMessages.AMBIGUOUS_NON_ANNOTATED_PARAMETER(invocable.getHandlingMethod(),
            invocable.getHandlingMethod().getDeclaringClass()));
      }
    }
  }
}

代码示例来源:origin: Graylog2/graylog2-server

private void checkResources(List<Resource> resources) {
    for (ResourceMethod method : resource.getResourceMethods()) {
      final Method m = method.getInvocable().getDefinitionMethod();
          LOG.warn("REST endpoint not included in audit trail: {}", String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)));
          LOG.debug("Missing @AuditEvent or @NoAuditEvent annotation: {}#{}", m.getDeclaringClass().getCanonicalName(), m.getName());
        } else {
                  String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)), annotation.type());
              LOG.debug("Make sure the audit event types are registered in a class that implements PluginAuditEventTypes: {}#{}",
                  m.getDeclaringClass().getCanonicalName(), m.getName());
                  String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)));
    checkResources(resource.getChildResources());

相关文章