Пример #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);
        }
      }
    }
  }
Пример #2
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()]));
      }
    }
  }