KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > HttpProxy


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal;
8
9
10 import java.io.IOException JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.RequestDispatcher JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20
21 /**
22  * The client-server implementation of the Proxy interface
23  * and is an HttpServlet.
24  *
25  * @author Brian Pontarelli
26  * @since 1.0
27  * @version 1.0
28  */

29 public class HttpProxy extends HttpServlet JavaDoc {
30
31     /**
32      * Handles a GET HTTP request by calling the POST method
33      */

34     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
35     throws ServletException JavaDoc, IOException JavaDoc {
36         doPost(request, response);
37     }
38
39     /**
40      * <p>
41      * Handles a POST HTTP request by first decoding the request to determine
42      * exactly what type of request this is. If it is a test request, then this
43      * method first calls into the createLocationObjects method to setup any
44      * objects that might be needed, then it calls the createTestCaller method
45      * to get the TestCaller instance, then it calls the runTest method on the
46      * TestCaller instance. With the Result object from the TestCaller, this
47      * calls the local toreResult to store the Result for later retrieval.
48      * </p>
49      * <p>
50      * However, if this is a Result request, this this method first calls the
51      * getResult method to retrieve the results. It then calls the toXML method
52      * on the Result instance. Finally, it writes the XML out to the response
53      * object and returns.
54      * </p>
55      */

56     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57     throws ServletException JavaDoc, IOException JavaDoc {
58
59         Map JavaDoc params = request.getParameterMap();
60         Set JavaDoc keys = params.keySet();
61         for (Iterator JavaDoc iterator = keys.iterator(); iterator.hasNext();) {
62             String JavaDoc key = (String JavaDoc) iterator.next();
63             System.out.print("-------------------------------- Parmeter [" + key + ", {");
64             Object JavaDoc[] values = (Object JavaDoc[]) params.get(key);
65             for (int i = 0; i < values.length; i++) {
66                 Object JavaDoc value = values[i];
67                 System.out.print(value);
68                 if (i < values.length - 1) {
69                     System.out.print(",");
70                 }
71             }
72             System.out.print("}]\n");
73         }
74
75         RemoteMediator mediator = new RemoteMediator();
76         try {
77             mediator.mediateServer(request, response);
78         } catch (Throwable JavaDoc t) {
79             if (t instanceof ServletException JavaDoc) {
80                 throw (ServletException JavaDoc) t;
81             }
82
83             if (t instanceof IOException JavaDoc) {
84                 throw (IOException JavaDoc) t;
85             }
86
87             throw new ServletException JavaDoc("Error executing test on the server.", t);
88         }
89     }
90 }
91
Popular Tags