View Javadoc
1   /*
2    *  This file is part of the Wayback archival access software
3    *   (http://archive-access.sourceforge.net/projects/wayback/).
4    *
5    *  Licensed to the Internet Archive (IA) by one or more individual 
6    *  contributors. 
7    *
8    *  The IA licenses this file to You under the Apache License, Version 2.0
9    *  (the "License"); you may not use this file except in compliance with
10   *  the License.  You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *  Unless required by applicable law or agreed to in writing, software
15   *  distributed under the License is distributed on an "AS IS" BASIS,
16   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   *  See the License for the specific language governing permissions and
18   *  limitations under the License.
19   */
20  package org.archive.wayback.util.webapp;
21  
22  import java.io.IOException;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.springframework.beans.factory.BeanNameAware;
30  
31  /**
32   * A generic handler of HttpServletRequests. very similar to an HttpServlet, but
33   * the handleRequest() method returns a boolean indicating if the RequestHandler
34   * returned data to the user.
35   * 
36   * This interface further defines methods to facilitate automatic registration
37   * when loaded as a Spring configuration, and maintains a reference to the
38   * ServletContext under which it accepts incoming requests.
39   * 
40   * @author brad
41   *
42   */
43  public interface RequestHandler extends BeanNameAware {
44  
45  	/**
46  	 * Possibly handle an incoming HttpServletRequest, much like a normal
47  	 * HttpServlet, but includes a return value.
48  	 * @param httpRequest the incoming HttpServletRequest
49  	 * @param httpResponse the HttpServletResponse to return data to the client.
50  	 * @return true if the RequestHandler returned a response to the client,
51  	 * false otherwise
52  	 * @throws ServletException for usual reasons.
53  	 * @throws IOException for usual reasons.
54  	 */
55  	public boolean handleRequest(HttpServletRequest httpRequest,
56  			HttpServletResponse httpResponse) 
57  	throws ServletException, IOException;
58  
59  	/**
60  	 * @return the "name" property of the bean from the SpringConfiguration
61  	 */
62  	public String getBeanName();
63  
64  	/**
65  	 * Called before registerPortListener(), to enable the registration process
66  	 * and subsequent handleRequest() calls to access the ServletContext, via
67  	 * the getServletContext() method.
68  	 * @param servletContext the ServletContext where the RequestHandler is
69  	 * registered.
70  	 */
71  	public void setServletContext(ServletContext servletContext);
72  
73  	/**
74  	 * @return the ServletContext where the RequestHandler is registered.
75  	 */
76  	public ServletContext getServletContext();
77  
78  	/**
79  	 * Called at webapp context initialization, to allow the RequestHandler to
80  	 * register itself with the RequestMapper, which will delegate request 
81  	 * handling to the appropriate RequestHandler.
82  	 * @param requestMapper the RequestMapper on which this RequestHandler 
83  	 * should register itself, including to register for notification of context
84  	 * shutdown.
85  	 */
86  	public void registerPortListener(RequestMapper requestMapper);
87  
88  	/**
89  	 * @param httpRequest the HttpServletRequest being handled
90  	 * @return the portion of the original incoming request that falls within
91  	 * this RequestHandler, not including any query information
92  	 */
93  	public String translateRequestPath(HttpServletRequest httpRequest);
94  
95  	/**
96  	 * @param httpRequest the HttpServletRequest being handled
97  	 * @return the portion of the original incoming request that falls within
98  	 * this RequestHandler, including any query information
99  	 */
100 	public String translateRequestPathQuery(HttpServletRequest httpRequest);
101 }