示例#1
0
  /**
   * Detaches the target from this router. All routes routing to this target Restlet are removed
   * from the list of routes and the default route is set to null.
   *
   * @param targetClass The target class to detach.
   */
  public void detach(Class<?> targetClass) {
    for (int i = getRoutes().size() - 1; i >= 0; i--) {
      Restlet target = getRoutes().get(i).getNext();

      if (target != null && Finder.class.isAssignableFrom(target.getClass())) {
        Finder finder = (Finder) target;

        if (finder.getTargetClass().equals(targetClass)) {
          getRoutes().remove(i);
        }
      }
    }

    if (getDefaultRoute() != null) {
      Restlet target = getDefaultRoute().getNext();

      if (target != null && Finder.class.isAssignableFrom(target.getClass())) {
        Finder finder = (Finder) target;

        if (finder.getTargetClass().equals(targetClass)) {
          setDefaultRoute(null);
        }
      }
    }
  }
  protected void setUp() throws Exception {
    super.setUp();
    Finder finder = new Finder();
    finder.setTargetClass(MyResource06.class);

    this.clientResource = new ClientResource("http://local");
    this.clientResource.setNext(finder);
  }
  /**
   * Creates a new finder instance based on the "targetClass" property. If none is define, the
   * {@link Application#createFinder(Class)} method is invoked if available, otherwise the {@link
   * org.restlet.resource.Finder#createFinder(Class, Class, Context, Logger)} method is called with
   * the {@link org.restlet.resource.Finder} class as parameter.
   *
   * @param resourceClass The target {@link org.restlet.resource.ServerResource} class to find.
   * @return The new finder instance.
   * @see org.restlet.resource.Finder#createFinder(Class, Class, Context, Logger)
   */
  public org.restlet.resource.Finder createFinder(
      Class<? extends org.restlet.resource.ServerResource> resourceClass) {
    org.restlet.resource.Finder result = null;

    if (getFinderClass() != null) {
      result =
          org.restlet.resource.Finder.createFinder(
              resourceClass, getFinderClass(), getContext(), getLogger());
    } else if ((getApplication() != null) && (getApplication() != this)) {
      result = getApplication().createFinder(resourceClass);
    } else {
      result =
          org.restlet.resource.Finder.createFinder(
              resourceClass, org.restlet.resource.Finder.class, getContext(), getLogger());
    }

    return result;
  }
示例#4
0
  private void generateSwaggerForFinder(JSONObject pathObject, String routePath, Finder finder)
      throws JSONException {
    Class<? extends ServerResource> targetClass = finder.getTargetClass();
    for (Method method : targetClass.getDeclaredMethods()) {
      String httpVerb = null;
      Annotation annotationInstance = method.getAnnotation(HttpVerb.class);
      if (annotationInstance != null) {
        httpVerb = ((HttpVerb) annotationInstance).value().toLowerCase();
      }

      HashSet<String> methodPaths = new HashSet<String>();
      annotationInstance = method.getAnnotation(Paths.class);
      if (annotationInstance != null) {
        methodPaths.addAll(Arrays.asList(((Paths) annotationInstance).value()));
      }

      if (httpVerb != null && methodPaths.contains(routePath) && !routePath.endsWith("/")) {
        JSONObject operation = new JSONObject();
        pathObject.put(httpVerb, operation);

        annotationInstance = method.getAnnotation(Summary.class);
        if (annotationInstance != null) {
          operation.put(
              Summary.class.getSimpleName().toLowerCase(), ((Summary) annotationInstance).value());
        }

        annotationInstance = method.getAnnotation(Description.class);
        if (annotationInstance != null) {
          operation.put(
              Description.class.getSimpleName().toLowerCase(),
              ((Description) annotationInstance).value());
        }

        annotationInstance = method.getAnnotation(Tags.class);
        if (annotationInstance != null) {
          operation.put(
              Tags.class.getSimpleName().toLowerCase(), ((Tags) annotationInstance).value());
        }

        operation.put("operationId", method.getName());

        ArrayList<JSONObject> parameters = new ArrayList<JSONObject>();

        for (Annotation[] annotations : method.getParameterAnnotations()) {
          if (annotations.length != 0) {
            JSONObject parameter = new JSONObject();
            for (Annotation annotation : annotations) {
              if (annotation instanceof Parameter) {
                Parameter parameterAnnotation = (Parameter) annotation;
                parameter.put("name", parameterAnnotation.name());
                parameter.put("in", parameterAnnotation.in());
                if (parameterAnnotation.description() != null) {
                  parameter.put("description", parameterAnnotation.description());
                }
                parameter.put("type", "string");
                parameter.put("required", parameterAnnotation.required());
              }
            }

            if (parameter.keys().hasNext()) {
              parameters.add(parameter);
            }
          }
        }

        operation.put("parameters", parameters.toArray(new JSONObject[parameters.size()]));
      }
    }
  }