/**
   * Returns the target reference to redirect to by automatically resolving URI template variables
   * found using the {@link Template} class using the request and response as data models.
   *
   * @param request The request to handle.
   * @param response The response to update.
   * @return The target reference to redirect to.
   */
  protected Reference getTargetRef(Request request, Response response) {
    // Create the template
    Template rt = new Template(this.targetTemplate);
    rt.setLogger(getLogger());

    // Return the formatted target URI
    if (new Reference(this.targetTemplate).isRelative()) {
      // Be sure to keep the resource's base reference.
      return new Reference(request.getResourceRef(), rt.format(request, response));
    }

    return new Reference(rt.format(request, response));
  }
  /**
   * 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);
          }
        }
      }
    }
  }