Exemple #1
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;
   }
 }