KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server.jetty;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.mortbay.http.HttpException;
30 import org.mortbay.http.HttpRequest;
31 import org.mortbay.http.HttpResponse;
32 import org.mortbay.http.handler.AbstractHttpHandler;
33
34 import com.sslexplorer.boot.RequestHandler;
35 import com.sslexplorer.boot.RequestHandlerException;
36
37 /**
38  * Implementation of Jetty's
39  * {@link org.mortbay.http.handler.AbstractHttpHandler} that adapts requests
40  * to SSL-Explorer's own registered {@link com.sslexplorer.boot.RequestHandler}
41  * implementations.
42  *
43  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
44  * @see com.sslexplorer.boot.RequestHandler
45  */

46 public class RequestHandlerAdapter extends AbstractHttpHandler {
47
48     // Package private statics
49

50     static Log log = LogFactory.getLog(RequestHandlerAdapter.class);
51
52     // Private statics
53

54     private static final long serialVersionUID = 4682392114545977296L;
55
56     // Private instance variables
57

58     private List JavaDoc requestHandlers = new ArrayList JavaDoc();
59
60     /**
61      * <p>
62      * Add a new {@link RequestHandler}. Every time a request is received that
63      * is not serviced by the main webapp, each registered handler will be
64      * invoked until one deals with the request.
65      *
66      * <p>
67      * This shouldn't be called directly, but through
68      * {@link CustomHttpContext#registerRequestHandler(RequestHandler)}
69      *
70      * @param requestHandler handler to add
71      */

72     public void registerRequestHandler(RequestHandler requestHandler) {
73         if (log.isInfoEnabled())
74             log.info("Registering request handler " + requestHandler.getClass().getName());
75         requestHandlers.add(requestHandler);
76     }
77
78     /**
79      * <p>
80      * Remove a {@link RequestHandler} so that is no longer received unhandled
81      * requests. See {@link #registerRequestHandler(RequestHandler)}.
82      *
83      * <p>
84      * This shouldn't be called directly, but through
85      * {@link CustomHttpContext#deregisterRequestHandler(RequestHandler)}
86      *
87      * @param requestHandler handler to remove
88      */

89     public void deregisterRequestHandler(RequestHandler requestHandler) {
90         if (log.isInfoEnabled())
91             log.info("De-registering request handler " + requestHandler.getClass().getName());
92         requestHandlers.remove(requestHandler);
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.mortbay.http.HttpHandler#handle(java.lang.String,
99      * java.lang.String, org.mortbay.http.HttpRequest,
100      * org.mortbay.http.HttpResponse)
101      */

102     public void handle(String JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request, HttpResponse response) throws HttpException,
103                     IOException JavaDoc {
104         if (log.isDebugEnabled())
105             log.debug("Request for " + pathInContext);
106         for (Iterator JavaDoc i = requestHandlers.iterator(); i.hasNext();) {
107             try {
108                 request.setCharacterEncoding(System.getProperty("sslexplorer.encoding", "UTF-8"), false);
109                 RequestHandler handler = (RequestHandler) i.next();
110                 if (handler.handle(pathInContext, pathParams, new RequestAdapter(request), new ResponseAdapter(response))) {
111                     request.setHandled(true);
112                     break;
113                 }
114             } catch (RequestHandlerException e) {
115                 log.error("Failed to handle request. Status code " + e.getCode());
116                 throw new HttpException(e.getCode(), e.getMessage());
117             }
118         }
119     }
120 }
121
Popular Tags