/** Tests resource reference getting/setting. */
 public void testResourceRef() throws Exception {
   final Request request = getRequest();
   String uri = "http://www.restlet.org/";
   Reference reference = getReference(uri);
   request.setResourceRef(uri);
   assertEquals(reference, request.getResourceRef());
   uri = "http://www.restlet.org/something";
   reference = getReference(uri);
   request.setResourceRef(reference);
   assertEquals(reference, request.getResourceRef());
 }
 /**
  * Creates an uniform call.
  *
  * @param jdbcURI The database's JDBC URI (ex: jdbc:mysql://[hostname]/[database]).
  * @param request The request to send (valid XML request).
  */
 public static Request create(String jdbcURI, Representation request) {
   Request result = new Request();
   result.getClientInfo().setAgent(Engine.VERSION_HEADER);
   result.setMethod(Method.POST);
   result.setResourceRef(jdbcURI);
   result.setEntity(request);
   return result;
 }
Example #3
0
 @Override
 public void handle(Request request, Response response) {
   // Update URI of the incoming request
   request.setResourceRef("http://localhost:8182/");
   // Create a basic HTTP client
   Client client = new Client(Protocol.HTTP);
   // Let the client handle the request and response
   client.handle(request, response);
 }
  private Request createRequest(Reference reference, Representation representation) {
    if (reference != null) {
      Request request = new Request();
      request.setResourceRef(reference);
      if (representation != null) {
        request.setEntity(representation);
      }

      return request;
    }

    throw new RepositoryException("Reference object is null!");
  }
  /**
   * 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);
          }
        }
      }
    }
  }
 /** Tests context's base reference getting/setting. */
 public void testBaseRef() throws Exception {
   final Request request = getRequest();
   final String resourceRefURI = "http://www.restlet.org/path/to/resource";
   final Reference resourceRef = getReference(resourceRefURI);
   request.setResourceRef(resourceRefURI);
   assertEquals(resourceRef, request.getResourceRef());
   String uri = "http://www.restlet.org/path";
   Reference reference = getReference(uri);
   request.getResourceRef().setBaseRef(uri);
   assertEquals(uri, request.getResourceRef().getBaseRef().toString());
   assertEquals(reference, request.getResourceRef().getBaseRef());
   uri = "http://www.restlet.org/path/to";
   reference = getReference(uri);
   request.getResourceRef().setBaseRef(uri);
   assertEquals(uri, request.getResourceRef().getBaseRef().toString());
   assertEquals(reference, request.getResourceRef().getBaseRef());
 }
  public List<Breadcrumb> getBreadcrumbList(Resource resource) {

    List<Breadcrumb> breadcrumbs = new ArrayList<Breadcrumb>();
    breadcrumbs.add(new Breadcrumb("/", null, "<i class='icon-home'></i>"));

    Reference reference = resource.getReference();
    SkysailApplication application = (SkysailApplication) resource.getApplication();
    RouteList routes = application.getRoutes();

    List<String> segments = getCleanedSegments(reference);

    Reference rootRef = resource.getRootRef();
    if (!(resource.getApplication() instanceof DefaultSkysailApplication)) {
      breadcrumbs.add(
          new Breadcrumb(
              segments.size() <= 1 ? null : rootRef.toString(),
              null,
              resource.getApplication().getName()));
    }

    String path = "";
    Route match = null;
    for (int i = 1; i < segments.size(); i++) {
      path = path + "/" + segments.get(i);

      Request request = resource.getRequest();
      request.setResourceRef(new Reference(path));
      Route best = routes.getBest(request, resource.getResponse(), 0.5f);
      if (best != match) {
        match = best;
        if (i < segments.size() - 1) {
          breadcrumbs.add(
              new Breadcrumb(
                  "/" + resource.getApplication().getName() + path, null, segments.get(i)));
        } else {
          breadcrumbs.add(new Breadcrumb(null, null, segments.get(i)));
        }
      }
    }
    return breadcrumbs;
  }
Example #8
0
  /**
   * Handle the call and follow redirection for safe methods.
   *
   * @param request The request to send.
   * @param response The response to update.
   * @param references The references that caused a redirection to prevent infinite loops.
   * @param retryAttempt The number of remaining attempts.
   * @param next The next handler handling the call.
   */
  private void handle(
      Request request,
      Response response,
      List<Reference> references,
      int retryAttempt,
      Uniform next) {
    if (next != null) {
      // Actually handle the call
      next.handle(request, response);

      // Check for redirections
      if (isFollowingRedirects()
          && response.getStatus().isRedirection()
          && (response.getLocationRef() != null)) {
        boolean doRedirection = false;

        if (request.getMethod().isSafe()) {
          doRedirection = true;
        } else {
          if (Status.REDIRECTION_SEE_OTHER.equals(response.getStatus())) {
            // The user agent is redirected using the GET method
            request.setMethod(Method.GET);
            request.setEntity(null);
            doRedirection = true;
          } else if (Status.REDIRECTION_USE_PROXY.equals(response.getStatus())) {
            doRedirection = true;
          }
        }

        if (doRedirection) {
          Reference newTargetRef = response.getLocationRef();

          if ((references != null) && references.contains(newTargetRef)) {
            getLogger().warning("Infinite redirection loop detected with URI: " + newTargetRef);
          } else if (request.getEntity() != null && !request.isEntityAvailable()) {
            getLogger()
                .warning(
                    "Unable to follow the redirection because the request entity isn't available anymore.");
          } else {
            if (references == null) {
              references = new ArrayList<Reference>();
            }

            // Add to the list of redirection reference
            // to prevent infinite loops
            references.add(request.getResourceRef());
            request.setResourceRef(newTargetRef);
            handle(request, response, references, 0, next);
          }
        }
      } else if (isRetryOnError()
          && response.getStatus().isRecoverableError()
          && request.getMethod().isIdempotent()
          && (retryAttempt < getRetryAttempts())
          && ((request.getEntity() == null) || request.getEntity().isAvailable())) {
        getLogger()
            .log(
                Level.INFO,
                "A recoverable error was detected ("
                    + response.getStatus().getCode()
                    + "), attempting again in "
                    + getRetryDelay()
                    + " ms.");

        // Wait before attempting again
        if (getRetryDelay() > 0) {
          try {
            Thread.sleep(getRetryDelay());
          } catch (InterruptedException e) {
            getLogger().log(Level.FINE, "Retry delay sleep was interrupted", e);
          }
        }

        // Retry the call
        handle(request, response, references, ++retryAttempt, next);
      }
    }
  }