Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: openfuture/util/servlet/HttpActionServlet.java


1   package openfuture.util.servlet;
2   /*
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2 of the License, or (at your option) any later version.<p>
7    *
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11   * Lesser General Public License for more details.<p>
12   *
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA<br>
16   * http://www.gnu.org/copyleft/lesser.html
17   */
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  import java.util.StringTokenizer;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpUtils;
30  
31  // Configuration Management Information: 
32  // -------------------------------------
33  // $Id: HttpActionServlet.java,v 1.2 2001/07/22 09:32:00 wreissen Exp $
34  //
35  // Version History:
36  // ----------------
37  // $Log: HttpActionServlet.java,v $
38  // Revision 1.2  2001/07/22 09:32:00  wreissen
39  // PUT parameters are only extracted from the query string
40  // in order to avoid problems with serialized objects.
41  //
42  // Revision 1.1.1.1  2001/07/08 18:29:34  wreissen
43  // initial version registered ad SourceForge
44  //
45  // Revision 1.1  2001/05/09 19:54:11  wreissen
46  // After ideas from <a href="http://www.openfuture.de/bugbase">Bug Base</a>
47  //
48  //
49  //
50  //
51  // ***********************************************************************************
52  /**
53   * HTTP Servlet with unified GET and POST access. Request parameters
54   * for the GET are coded into the query string:<p>
55   *
56   * <code>
57   * &lt;action-name&gt;(&amp;&lt;identifier&gt;=&lt;value&gt;)*
58   * </code><p>
59   *
60   * The parameters for POST are accessible via 
61   * {@link javax.servlet.http.HttpServletRequest#getParameter}.<p>
62   *
63   * Subclasses should override {@link #handleAction}.<p>
64   *
65   * Created: Sun Nov 12 10:35:37 2000 (Originally created in 
66   * <a href="www.openfuture.de/webglossary">Web Glossary</a>).
67   *
68   * @author Wolfgang Reissenberger
69   * @version $Revision: 1.2 $
70   */
71  
72  public class HttpActionServlet extends HttpServlet {
73      
74  
75      /**
76       * The separator symbol separating key-value pairs.
77       */
78      public static final String SEPARATOR = "&";
79  
80      /**
81       * The equal sign separating key and value.
82       */
83      public static final String EQUAL_SIGN = "=";
84  
85  
86  
87      /**
88       * Extracts the action command and the parameters from the 
89       * query string of GET requests and redirects it to
90       * {@link #handleAction}. 
91       * @param req HTTP request
92       * @param res HTTP response
93       * @exception ServletException if an error occurs
94       * @exception IOException if an error occurs
95       */
96      public void doGet(HttpServletRequest req, HttpServletResponse res)
97          throws ServletException, IOException {
98    
99    Hashtable parameters;
100   try {
101       parameters = HttpUtils.parseQueryString(req.getQueryString());
102   } catch (IllegalArgumentException e) {
103       parameters = new Hashtable();
104       addParameter(parameters, "query", req.getQueryString());
105   }
106 
107   handleAction(parameters, req, res);
108 
109     }
110 
111 
112 
113     /*
114      * Extracts the action command and the parameters from the POST
115      * request and redirects it to {@link #handleAction}. 
116      * The query string is interpreted as the action and remains
117      * unparsed in contrast to {@link doGet}.
118      *
119      * @param req HTTP request
120      * @param res HTTP response
121      * @exception ServletException if an error occurs
122      * @exception IOException if an error occurs
123      */
124     public void doPost(HttpServletRequest req, HttpServletResponse res)
125         throws ServletException, IOException {
126 
127   System.out.println("query string: " + req.getQueryString());
128   
129   Hashtable parameters = new Hashtable();
130   try {
131       parameters = HttpUtils.parseQueryString(req.getQueryString());
132   } catch (IllegalArgumentException e) {
133       parameters = new Hashtable();
134       addParameter(parameters, "query", req.getQueryString());
135   }
136 
137   handleAction(parameters, req, res);
138     }
139 
140 
141     /**
142      * Dispatcher for the action commands. Shows the received action 
143      * and parameters. <p>
144      * <b>Subclasses should override this method</b>.
145      *
146      * @param parameters the parameters
147      * @param req HTTP request
148      * @param res HTTP response
149      * @exception IOException if an error occurs
150      */
151     protected void handleAction(Hashtable parameters,
152         HttpServletRequest req, 
153         HttpServletResponse res)  throws IOException{
154 
155   res.setContentType("text/html");
156   PrintWriter writer = null;
157 
158   try {
159       writer = res.getWriter();
160 
161       writer.println("<h1>" + getClass().getName() + "</h1>");
162 
163       writer.println("<b>Character encoding: </b>" + 
164          req.getCharacterEncoding() + "<p>");
165       writer.println("<b>content type: </b>" + 
166          req.getContentType() + "<p>");
167       writer.println("<b>Parameters: </b><br>");
168 
169       Enumeration enum = parameters.keys();
170 
171       while (enum.hasMoreElements()) {
172     String key = (String) enum.nextElement();
173     writer.print(key + " = ");
174     Object[] values = (Object[]) parameters.get(key);
175     for (int i = 0; i < values.length; i++) {
176         writer.print(values[i]);
177         if (i < values.length - 1) writer.print(", ");
178         else writer.println("<br>");
179     }
180       }
181       
182 
183   } catch (IOException e) {
184       if (writer != null) {
185     writer.println("<h2>Exception occured:</h2>");
186     writer.print("<b>");
187     writer.print(e.getMessage());
188     writer.print("</b><br>");
189     e.printStackTrace(writer);
190       }
191   }
192     }
193 
194 
195     /**
196      * Write an exception to the log file of the servlet 
197      * container.
198      *
199      * @param e exception to be logged
200      */
201     public void logException(Exception e) {
202 
203   StringWriter writer = new StringWriter();
204   e.printStackTrace(new PrintWriter(writer));
205 
206   log(writer.toString());
207     }
208 
209 
210     /**
211      * Add a new parameter to the hashtable. Each value
212      * in the hashtable is an <code>String</code> array.
213      *
214      * @param parameters table with the actual parameters
215      * @param key parameter name
216      * @param value parameter value
217      */
218     protected void addParameter(Hashtable parameters,
219         String key, String value) {
220 
221   if (parameters == null) return;
222 
223   String[] parameter;
224   if (parameters.containsKey(key)) {
225       String oldVals[] = (String []) parameters.get(key);
226       parameter = new String[oldVals.length + 1];
227       for (int i = 0; i < oldVals.length; i++) 
228     parameter[i] = oldVals[i];
229 
230       parameter[oldVals.length] = value;
231   } else {
232       parameter = new String[1];
233       parameter[0] = value;
234   }
235 
236   parameters.put(key, parameter);
237     }
238 }