KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > jetty > HTTPRedirectHandler


1 package com.sslexplorer.server.jetty;
2
3 import java.io.IOException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 import org.mortbay.http.HttpContext;
7 import org.mortbay.http.HttpException;
8 import org.mortbay.http.HttpHandler;
9 import org.mortbay.http.HttpRequest;
10 import org.mortbay.http.HttpResponse;
11
12 import com.sslexplorer.boot.ContextHolder;
13 import com.sslexplorer.boot.RequestHandler;
14 import com.sslexplorer.boot.RequestHandlerException;
15
16
17 /**
18  * <p>Title: </p>
19  *
20  * <p>Description: </p>
21  *
22  * <p>Copyright: Copyright (c) 2004</p>
23  *
24  * <p>Company: </p>
25  *
26  * @author not attributable
27  */

28 public class HTTPRedirectHandler implements HttpHandler {
29
30     HttpContext context;
31     
32     static ArrayList JavaDoc<RequestHandler> handlers = new ArrayList JavaDoc<RequestHandler>();
33     
34     
35     public static void registerHandler(RequestHandler handler) {
36         handlers.add(handler);
37     }
38     
39     public static void unregisterHandler(RequestHandler handler) {
40         handlers.remove(handler);
41     }
42     /**
43      * Constructor
44      */

45     public HTTPRedirectHandler() {
46     }
47
48     /* (non-Javadoc)
49      * @see org.mortbay.http.HttpHandler#initialize(org.mortbay.http.HttpContext)
50      */

51     public void initialize(HttpContext context) {
52         this.context = context;
53     }
54
55     /* (non-Javadoc)
56      * @see org.mortbay.util.LifeCycle#isStarted()
57      */

58     public boolean isStarted() {
59         return true;
60     }
61
62     /* (non-Javadoc)
63      * @see org.mortbay.util.LifeCycle#stop()
64      */

65     public void stop() {
66
67     }
68
69     /* (non-Javadoc)
70      * @see org.mortbay.util.LifeCycle#start()
71      */

72     public void start() {
73
74     }
75     
76     /* (non-Javadoc)
77      * @see org.mortbay.http.HttpHandler#getName()
78      */

79     public String JavaDoc getName() {
80         return "SECURE";
81     }
82     
83     /* (non-Javadoc)
84      * @see org.mortbay.http.HttpHandler#getHttpContext()
85      */

86     public HttpContext getHttpContext() {
87         return context;
88     }
89     
90     /* (non-Javadoc)
91      * @see org.mortbay.http.HttpHandler#handle(java.lang.String, java.lang.String, org.mortbay.http.HttpRequest, org.mortbay.http.HttpResponse)
92      */

93     public void handle(String JavaDoc pathInContext,
94                        String JavaDoc str,
95                            HttpRequest request,
96                            HttpResponse response) throws IOException JavaDoc {
97         handle(pathInContext, request, response);
98     }
99     /**
100      *
101      * @param pathInContext String
102      * @param request RequestHandlerRequest
103      * @param response RequestHandlerResponse
104      * @throws IOException
105      * @todo Implement this com.sslexplorer.boot.RequestHandler method
106      */

107     public void handle(String JavaDoc pathInContext,
108                        HttpRequest request,
109                        HttpResponse response) throws IOException JavaDoc {
110
111         for(RequestHandler handler : handlers) {
112             try {
113                 request.setCharacterEncoding(System.getProperty("sslexplorer.encoding", "UTF-8"), false);
114                  if (handler.handle(pathInContext, "", new RequestAdapter(request), new ResponseAdapter(response))) {
115                     request.setHandled(true);
116                     return;
117                 }
118             } catch (RequestHandlerException e) {
119                 throw new HttpException(e.getCode(), e.getMessage());
120             }
121             
122         }
123         
124         if(!request.isConfidential()) {
125             int sslPort = ContextHolder.getContext().getPort();
126             response.sendRedirect("https://" + request.getHost() + (sslPort > 0 && sslPort!=443 ? ":" + sslPort : "") + request.getEncodedPath());
127             request.setHandled(true);
128         }
129     }
130 }
131
Popular Tags