KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > helper > FormGateway


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: FormGateway.java,v 1.9 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp.helper;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.apache.log4j.*;
28
29 /**
30  * <p>The purpose of this servlet is simply to process requests
31  * that come from a form and redirect based on the name of the button
32  * pressed.
33  */

34 public class FormGateway extends HttpServlet {
35
36     //public vars...eventually, these should probably be final
37
protected static final Logger logger = Logger.getLogger(FormGateway.class.getName());
38
39     //these are intentionally non-final, so that you can programatically
40
//change them if need be
41
public static String FORM_EXT = ".form_forward";
42     public static String FORM_TARGET = "ft::";
43
44     //-------------------- FormGateway ---------------------------
45
/**
46      * <p>Handle the default HttpRequest.
47      *
48      * @param req the servlet request
49      * @param resp the servlet response
50      * @throws ServletException
51      * @throws IOException
52      */

53     public void handleDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54     
55         //iterate through all the parameters, looking for those which indicate
56
//the key name is really an event handler or url
57
Enumeration enum = req.getParameterNames();
58         int matches = 0;
59         Servlet servlet = null;
60         String target = null;
61         if (logger.isDebugEnabled()) logger.debug("Looking for target parameters");
62         while (enum.hasMoreElements()) {
63             String key = (String) enum.nextElement();
64             String val = (String) req.getParameter(key);
65             if (logger.isDebugEnabled()) logger.debug("key:"+key+" val:"+val);
66             
67             if (key.startsWith(FORM_TARGET)) {
68                 if (logger.isDebugEnabled()) logger.debug("(form target match)");
69                 target = key.substring(FORM_TARGET.length());
70                 matches++;
71             }
72         }
73 //csc_101901.1 target = URLRewriter.encodeURL(req, resp, target); //csc_101701.1
74

75         //this is basically checks to ensure that only one of the req parameters
76
//matches a known form handler or URL. If more than one matches, then there's
77
//no way of knowing which one it's really supposed to be delivered to, so
78
//throw an exception
79
if (matches!=1) throw new ServletException ("Unable to determine destination handler");
80         else {
81             //find the RequestDispatcher and forward it there
82
RequestDispatcher rd = req.getRequestDispatcher(target);
83             if (logger.isDebugEnabled()) logger.debug("Redirecting to url:"+target);
84             if (rd!=null) {
85                 rd.forward(req, resp);
86             } else {
87                 throw new ServletException ("Unable to locate RequestDispatcher");
88             }
89         }
90     }
91
92
93
94
95     //-------------------- HTTPServlet ---------------------------
96
/**
97      * <p>By default the GET request is mapped to the handleDefault method
98      *
99      * @param req the servlet request
100      * @param resp the servlet response
101      * @throws ServletException
102      * @throws IOException
103      */

104     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
105         handleDefault(req, resp);
106     }
107     
108     /**
109      * <p>By default the POST request is mapped to the handleDefault method
110      *
111      * @param req the servlet request
112      * @param resp the servlet response
113      * @throws ServletException
114      * @throws IOException
115      */

116     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
117         handleDefault(req, resp);
118     }
119     
120     /**
121      * <p>By default the OPTIONS request is mapped to the handleDefault method
122      *
123      * @param req the servlet request
124      * @param resp the servlet response
125      * @throws ServletException
126      * @throws IOException
127      */

128     protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
129         handleDefault(req, resp);
130     }
131     
132     /**
133      * <p>By default the DELETE request is mapped to the handleDefault method
134      *
135      * @param req the servlet request
136      * @param resp the servlet response
137      * @throws ServletException
138      * @throws IOException
139      */

140     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
141         handleDefault(req, resp);
142     }
143     
144     /**
145      * <p>By default the PUT request is mapped to the handleDefault method
146      *
147      * @param req the servlet request
148      * @param resp the servlet response
149      * @throws ServletException
150      * @throws IOException
151      */

152     protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
153         handleDefault(req, resp);
154     }
155     
156     /**
157      * <p>By default the TRACE request is mapped to the handleDefault method
158      *
159      * @param req the servlet request
160      * @param resp the servlet response
161      * @throws ServletException
162      * @throws IOException
163      */

164     protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
165         handleDefault(req, resp);
166     }
167 }
168
Popular Tags