Exemplo n.º 1
0
 /**
  * Display the Frame message and some attributes of our associated Resource. This method is called
  * if the associated resource has been registered with <strong>registerOtherResource</strong> or
  * if it's not a usual resource (FileResource, DirectoryResource)
  *
  * @param request The request to handle.
  * @return A Reply instance.
  * @exception ProtocolException if processing the request failed
  * @exception ResourceException if an internal error occurs
  */
 protected Reply getOtherResource(Request request)
     throws ProtocolException, ResourceException { // get our associated Resource
   FramedResource res = getResource();
   // Create the HTML generator, and set titles:
   HtmlGenerator g = new HtmlGenerator("FancyFrame");
   g.append("<h1>FancyFrame output</h1>");
   // emit the message
   g.append("<p>", getMessage(), "</p>");
   // display information about our Resource
   g.append("<h2> Resource associated : </h2>");
   g.append("<ul><li>Identifier : ", res.getIdentifier());
   g.append("<li>Last Modified Time : ", new Date(res.getLastModified()).toString(), "</ul>");
   // now emit the reply
   Reply reply = createDefaultReply(request, HTTP.OK);
   reply.setStream(g);
   return reply;
 }
Exemplo n.º 2
0
  /**
   * The HEAD method, may emit a redirect
   *
   * @param request The request to handle.
   * @exception ProtocolException If processsing the request failed.
   * @exception ResourceException If the resource got a fatal error.
   */
  public Reply head(Request request) throws ProtocolException, ResourceException {
    String methods[] = getMethods();

    if (methods != null) {
      String reqmeth = request.getMethod();
      boolean affected = false;
      for (int i = 0; i < methods.length; i++) {
        if (reqmeth.equals(methods[i])) {
          affected = true;
          break;
        }
      }
      if (!affected) {
        return super.head(request);
      }
    }
    // now we can modify it :)
    Reply reply = getRedirectReply(request);
    reply.setStream((InputStream) null);
    return reply;
  }
Exemplo n.º 3
0
  /**
   * We check that the date is in the right values otherwise, send a NOT_AVAILABLE and fills the
   * right Retry-After header
   *
   * @return a Reply if blocked, null otherwise
   */
  public synchronized ReplyInterface ingoingFilter(RequestInterface req) {
    Request request = (Request) req;
    Reply reply = null;
    int n_year, n_month, n_week, n_day, n_time;
    int a, b, n;
    Calendar cal = Calendar.getInstance();

    n_year = cal.get(Calendar.YEAR);
    n_month = cal.get(Calendar.MONTH);
    n_week = cal.get(Calendar.DAY_OF_WEEK);
    n_day = cal.get(Calendar.DAY_OF_MONTH);
    n_time =
        cal.get(Calendar.HOUR_OF_DAY) * 3600
            + cal.get(Calendar.MINUTE) * 60
            + cal.get(Calendar.SECOND);

    if (getDayRepeat()) { // check it if it is repeated every day
      if ((n_time < a_time) || (n_time > b_time)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        if (n_time < a_time) reply.setRetryAfter(a_time - n_time);
        else reply.setRetryAfter(84600 + n_time - a_time);
      }
    } else if (getWeekRepeat()) { // check it if it is repeated every week
      a = a_time + a_week * 86400;
      b = b_time + b_week * 86400;
      n = n_time + n_week * 86400;
      if ((n < a) || (n > b)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        if (n < a) reply.setRetryAfter(a - n);
        else reply.setRetryAfter(84600 * 7 + n - a);
      }
    } else if (getMonthRepeat()) { // check it if it's repeated every month
      a = a_time + a_day * 86400;
      b = b_time + b_day * 86400;
      n = n_time + n_day * 86400;
      if ((n < a) || (n > b)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        if (n < a) reply.setRetryAfter(a - n);
        else {
          cal.setTime(new Date(getLong(ATTR_DATE_START, -1)));
          cal.set(Calendar.YEAR, n_year);
          cal.set(Calendar.MONTH, n_month);
          cal.roll(Calendar.MONTH, true);
          reply.setRetryAfter(cal.getTime().getTime());
        }
      }
    } else if (getYearRepeat()) { // check it if it's repeated every year
      Calendar c_a = Calendar.getInstance();
      Calendar c_b = Calendar.getInstance();
      c_a.setTime(new Date(getLong(ATTR_DATE_START, -1)));
      c_b.setTime(new Date(getLong(ATTR_DATE_END, -1)));
      c_a.set(Calendar.YEAR, n_year);
      c_b.set(Calendar.YEAR, n_year);
      if (cal.before(c_a)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        reply.setRetryAfter(c_a.getTime().getTime());
      } else if (cal.after(c_b)) {
        c_a.roll(Calendar.YEAR, true);
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        reply.setRetryAfter(c_a.getTime().getTime());
      }
    } else {
        /* no repeat */
      Calendar c_a = Calendar.getInstance();
      Calendar c_b = Calendar.getInstance();
      c_a.setTime(new Date(getLong(ATTR_DATE_START, -1)));
      c_b.setTime(new Date(getLong(ATTR_DATE_END, -1)));
      if (cal.before(c_a)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
        reply.setRetryAfter(c_a.getTime().getTime());
      } else if (cal.after(c_b)) {
        reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
      }
    }
    if (reply != null) {
      HtmlGenerator g = new HtmlGenerator("Service Unavailable");
      g.append("You may retry after the delay or the date given");
      reply.setStream(g);
    }
    return reply;
  }
Exemplo n.º 4
0
 /**
  * build the redirect reply based on the request and the current configuration
  *
  * @param request The request to handle.
  * @exception ProtocolException If processsing the request failed.
  * @return a Reply
  */
 private Reply getRedirectReply(Request request) throws ProtocolException {
   String location = getLocation();
   if (location == null) {
     Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
     error.setContent(
         "The target RelocateResource doesn't define the"
             + " relocation location. The server is "
             + " misconfigured.");
     throw new HTTPException(error);
   } else {
     Reply reply = null;
     URL loc = null;
     if (checkUse302()) {
       reply = request.makeReply(HTTP.FOUND);
     } else {
       if (checkPermanentRedirect()) {
         reply = request.makeReply(HTTP.MOVED_PERMANENTLY);
       } else {
         reply = request.makeReply(HTTP.TEMPORARY_REDIRECT);
       }
     }
     try {
       httpd server = (httpd) getServer();
       String host = request.getHost();
       if (host == null) loc = new URL(server.getURL(), location);
       else {
         int ic = host.indexOf(':');
         if (ic < 0) {
           loc =
               new URL(
                   new URL(server.getURL().getProtocol(), host, server.getURL().getFile()),
                   location);
         } else {
           loc =
               new URL(
                   new URL(
                       server.getURL().getProtocol(),
                       host.substring(0, ic),
                       Integer.parseInt(host.substring(ic + 1)),
                       server.getURL().getFile()),
                   location);
         }
       }
       if (checkHandlePathInfo()) {
         String pathinfo = (String) request.getState(PATH_INFO);
         // Given the way pathinfo is computed, it starts with a /
         try {
           if (pathinfo != null) {
             loc = new URL(loc.toExternalForm() + pathinfo);
           }
         } catch (MalformedURLException ex) {
           resource
               .getServer()
               .errlog(
                   resource,
                   "This resource handle Pathinfo "
                       + "but the request has an invalid "
                       + "PATH_INFO state.");
         }
         if (request.hasQueryString()) {
           try {
             loc = new URL(loc.toExternalForm() + "?" + request.getQueryString());
           } catch (MalformedURLException ex) {
             resource
                 .getServer()
                 .errlog(
                     resource,
                     "This resource handle "
                         + "Pathinfo but the "
                         + "request has an "
                         + "invalid "
                         + "PATH_INFO state.");
           }
         }
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
     reply.setLocation(loc);
     HtmlGenerator g = new HtmlGenerator("Moved");
     g.append(
         "<P>This resources has moved, click on the link if your"
             + " browser doesn't support automatic redirection<BR>"
             + "<A HREF=\""
             + loc.toExternalForm()
             + "\">"
             + loc.toExternalForm()
             + "</A>");
     reply.setStream(g);
     return reply;
   }
 }