/**
   * 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);
        }
      }
    }
  }
Beispiel #2
0
  /**
   * Sets the next handler such as a Restlet or a Filter.
   *
   * <p>In addition, this method will set the context of the next Restlet if it is null by passing a
   * reference to its own context.
   *
   * @param next The next handler.
   */
  public void setNext(org.restlet.Uniform next) {
    if (next instanceof Restlet) {
      Restlet nextRestlet = (Restlet) next;

      if (nextRestlet.getContext() == null) {
        nextRestlet.setContext(getContext());
      }
    }

    this.next = next;

    // If true, it must be updated after calling this method
    this.nextCreated = false;
  }
  @Override
  public void handle(Request request, Response response) {
    super.handle(request, response);

    if (Method.GET.equals(request.getMethod())) {
      response.setEntity(getSwagger());
    } else {
      response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
    }
  }
  /**
   * Handles a call by invoking the next Restlet if it is available.
   *
   * @param request The request to handle.
   * @param response The response to update.
   */
  @Override
  public void handle(Request request, Response response) {
    super.handle(request, response);
    Restlet next = getNext(request, response);

    if (next != null) {
      doHandle(next, request, response);
    } else {
      response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    }
  }
  /** Stops the filter and the attached routes. */
  @Override
  public synchronized void stop() throws Exception {
    if (isStarted()) {
      if (getDefaultRoute() != null) {
        getDefaultRoute().stop();
      }

      for (Route route : getRoutes()) {
        route.stop();
      }

      super.stop();
    }
  }
  /**
   * Indicates that a Restlet's context has changed.
   *
   * @param restlet The Restlet with a changed context.
   * @param context The new context.
   */
  private static void fireContextChanged(Restlet restlet, Context context) {
    if (context != null) {
      if (context instanceof org.restlet.engine.util.ChildContext) {
        org.restlet.engine.util.ChildContext childContext =
            (org.restlet.engine.util.ChildContext) context;

        if (childContext.getChild() == null) {
          childContext.setChild(restlet);
        }
      } else if (!(restlet instanceof Component)
          && (context instanceof org.restlet.engine.component.ComponentContext)) {
        context
            .getLogger()
            .severe(
                "For security reasons, don't pass the component context to child Restlets anymore. Use the Context#createChildContext() method instead. "
                    + restlet.getClass());
      }
    }
  }
  @Override
  public void handle(Request request, Response response) {
    super.handle(request, response);

    try {
      if (request.getMethod().equals(Method.GET)) {

        try {
          if ("users".equalsIgnoreCase(request.getResourceRef().getLastSegment())) {
            UserListValue users = importer.users();
            StringRepresentation result =
                new StringRepresentation(
                    users.toJSON(),
                    MediaType.APPLICATION_JSON,
                    Language.DEFAULT,
                    CharacterSet.UTF_8);
            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(result);
          } else if ("groups".equalsIgnoreCase(request.getResourceRef().getLastSegment())) {
            GroupListValue groups = importer.groups();
            StringRepresentation result =
                new StringRepresentation(
                    groups.toJSON(),
                    MediaType.APPLICATION_JSON,
                    Language.DEFAULT,
                    CharacterSet.UTF_8);
            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(result);
          }
        } catch (ResourceException e) {
          response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
          response.setEntity(e.getStatus().getDescription(), MediaType.TEXT_PLAIN);
        }

      } else {
        response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
      }
    } finally {
      request.release();
    }
  }
  /**
   * Redirects a given call on the server-side to a next Restlet with a given target reference. In
   * the default implementation, the request HTTP headers, stored in the request's attributes, are
   * removed before dispatching. After dispatching, the response HTTP headers are also removed to
   * prevent conflicts with the main call.
   *
   * @param next The next Restlet to forward the call to.
   * @param targetRef The target reference with URI variables resolved.
   * @param request The request to handle.
   * @param response The response to update.
   */
  protected void serverRedirect(
      Restlet next, Reference targetRef, Request request, Response response) {
    if (next == null) {
      getLogger().warning("No next Restlet provided for server redirection to " + targetRef);
    } else {
      // Save the base URI if it exists as we might need it for
      // redirections
      Reference resourceRef = request.getResourceRef();
      Reference baseRef = resourceRef.getBaseRef();

      // Reset the protocol and let the dispatcher handle the protocol
      request.setProtocol(null);

      // Update the request to cleanly go to the target URI
      request.setResourceRef(targetRef);
      request.getAttributes().remove(HeaderConstants.ATTRIBUTE_HEADERS);
      next.handle(request, response);

      // Allow for response rewriting and clean the headers
      response.setEntity(rewrite(response.getEntity()));
      response.getAttributes().remove(HeaderConstants.ATTRIBUTE_HEADERS);
      request.setResourceRef(resourceRef);

      // In case of redirection, we may have to rewrite the redirect URI
      if (response.getLocationRef() != null) {
        Template rt = new Template(this.targetTemplate);
        rt.setLogger(getLogger());
        int matched = rt.parse(response.getLocationRef().toString(), request);

        if (matched > 0) {
          String remainingPart = (String) request.getAttributes().get("rr");

          if (remainingPart != null) {
            response.setLocationRef(baseRef.toString() + remainingPart);
          }
        }
      }
    }
  }
  public void dispatched(Request request, Response response, Restlet restlet) {
    RequestData data = monitor.current();
    if (data == null) {
      // will happen in cases where the filter is not active
      return;
    }

    if (restlet instanceof Route) {
      restlet = ((Route) restlet).getNext();
    }

    if (restlet instanceof BeanDelegatingRestlet) {
      restlet = ((BeanDelegatingRestlet) restlet).getBean();
    }

    if (restlet != null) {
      if (restlet.getClass().getPackage().getName().startsWith("org.geoserver.catalog.rest")) {
        data.setService("RESTConfig");
      }
    }

    monitor.update();
  }
 /**
  * Effectively handles the call using the selected next {@link Restlet}, typically the selected
  * {@link Route}. By default, it just invokes the next Restlet.
  *
  * @param next The next Restlet to invoke.
  * @param request The request.
  * @param response The response.
  */
 protected void doHandle(Restlet next, Request request, Response response) {
   next.handle(request, response);
 }
 /** Constructor with a parent Restlet. */
 public SpringBeanRouter(Restlet parent) {
   super(parent.getContext());
 }