01: /*
02: * Copyright 2003 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package velosurf.util;
18:
19: import java.io.IOException;
20:
21: import javax.servlet.ServletConfig;
22: import javax.servlet.ServletException;
23: import javax.servlet.http.HttpServlet;
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletResponse;
26:
27: /**
28: * A null servlet, provided here for convenience, that will redirect the user to the forbidden uri (if provided
29: * in the init parameter "forbidden-uri") or respond with the FORBIDDEN error code.
30: *
31: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
32: */
33:
34: public class NullServlet extends HttpServlet {
35: /** page showing the 'forbidden' error message */
36: private String forbiddenUri = null;
37:
38: /**
39: * init.
40: * @param config config
41: * @throws ServletException
42: */
43: public void init(ServletConfig config) throws ServletException {
44: super .init(config);
45: forbiddenUri = config.getInitParameter("forbidden-uri");
46: }
47:
48: /**
49: * doGet handler.
50: * @param request request
51: * @param response response
52: * @throws ServletException
53: * @throws IOException
54: */
55: public void doGet(HttpServletRequest request,
56: HttpServletResponse response) throws ServletException,
57: IOException {
58: doRequest(request, response);
59: }
60:
61: /**
62: * doPost handler.
63: * @param request request
64: * @param response response
65: * @throws ServletException
66: * @throws IOException
67: */
68: public void doPost(HttpServletRequest request,
69: HttpServletResponse response) throws ServletException,
70: IOException {
71: doRequest(request, response);
72: }
73:
74: /**
75: * doRequest handler.
76: * @param request request
77: * @param response response
78: * @throws ServletException
79: * @throws IOException
80: */
81: private void doRequest(HttpServletRequest request,
82: HttpServletResponse response) throws ServletException,
83: IOException {
84: Logger
85: .trace("null servlet got hit: "
86: + request.getRequestURI());
87: if (forbiddenUri == null) {
88: response.sendError(HttpServletResponse.SC_FORBIDDEN);
89: } else {
90: response.sendRedirect(forbiddenUri);
91: /* other option...
92: RequestDispatcher dispatcher = request.getRequestDispatcher(forbiddenUri);
93: dispatcher.forward(request,response);
94: */
95: }
96: }
97: }
|