Example #1
0
  /**
   * Handles a call. The default behavior is to initialize the Restlet by setting the current
   * context using the {@link Context#setCurrent(Context)} method and by attempting to start it,
   * unless it was already started. If an exception is thrown during the start action, then the
   * response status is set to {@link Status#SERVER_ERROR_INTERNAL}.
   *
   * <p>Subclasses overriding this method should make sure that they call super.handle(request,
   * response) before adding their own logic.
   *
   * @param request The request to handle.
   * @param response The response to update.
   */
  public void handle(Request request, Response response) {
    // Associate the response to the current thread
    Response.setCurrent(response);

    // Associate the context to the current thread
    if (getContext() != null) {
      Context.setCurrent(getContext());
    }

    // Check if the Restlet was started
    if (isStopped()) {
      try {
        start();
      } catch (Exception e) {
        // Occurred while starting the Restlet
        getContext().getLogger().log(Level.WARNING, UNABLE_TO_START, e);
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
      }

      if (!isStarted()) {
        // No exception raised but the Restlet somehow couldn't be
        // started
        getContext().getLogger().log(Level.WARNING, UNABLE_TO_START);
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
      }
    }
  }
  /** Starts the filter and the attached routes. */
  @Override
  public synchronized void start() throws Exception {
    if (isStopped()) {
      super.start();

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

      if (getDefaultRoute() != null) {
        getDefaultRoute().start();
      }
    }
  }