KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > server > AbstractWebTestController


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.server;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.cactus.internal.HttpServiceDefinition;
26 import org.apache.cactus.internal.ServiceEnumeration;
27 import org.apache.cactus.spi.server.ImplicitObjects;
28 import org.apache.cactus.spi.server.TestController;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Controller that extracts the requested service from the HTTP request and
34  * executes the request. Examples of requests are: executing a given test,
35  * returning the test result, verifying that the controller is correctly
36  * configured, etc.
37  *
38  * @version $Id: AbstractWebTestController.java,v 1.1 2004/05/22 11:34:45 vmassol Exp $
39  */

40 public abstract class AbstractWebTestController implements TestController
41 {
42     /**
43      * The logger
44      */

45     private static final Log LOGGER =
46         LogFactory.getLog(AbstractWebTestController.class);
47
48     /**
49      * @param theObjects the implicit objects coming from the redirector
50      * @return the test caller that will be used to execute the test
51      */

52     protected abstract AbstractWebTestCaller getTestCaller(
53         WebImplicitObjects theObjects);
54
55     /**
56      * Handles the incoming request by extracting the requested service and
57      * calling the correct method on a <code>WebTestCaller</code>.
58      *
59      * @param theObjects the implicit objects (they are different for the
60      * different redirectors)
61      * @exception ServletException if an error occurs when servicing the
62      * request
63      */

64     public void handleRequest(ImplicitObjects theObjects)
65         throws ServletException JavaDoc
66     {
67         WebImplicitObjects webImplicitObjects = (WebImplicitObjects) theObjects;
68
69         // If the Cactus user has forgotten to put a needed jar on the server
70
// classpath (i.e. in WEB-INF/lib), then the servlet engine Webapp
71
// class loader will throw a NoClassDefFoundError exception. As this
72
// method is the entry point of the webapp, we'll catch all
73
// NoClassDefFoundError exceptions and report a nice error message
74
// for the user so that he knows he has forgotten to put a jar in the
75
// classpath. If we don't do this, the error will be trapped by the
76
// container and may not result in an ... err ... understandable error
77
// message (like in Tomcat) ...
78
try
79         {
80             String JavaDoc serviceName =
81                 getServiceName(webImplicitObjects.getHttpServletRequest());
82
83             AbstractWebTestCaller caller = getTestCaller(webImplicitObjects);
84
85             // TODO: will need a factory here real soon...
86

87             ServiceEnumeration service =
88                 ServiceEnumeration.valueOf(serviceName);
89             
90             // Is it the call test method service ?
91
if (service == ServiceEnumeration.CALL_TEST_SERVICE)
92             {
93                 caller.doTest();
94             }
95             // Is it the get test results service ?
96
else if (service == ServiceEnumeration.GET_RESULTS_SERVICE)
97             {
98                 caller.doGetResults();
99             }
100             // Is it the test connection service ?
101
// This service is only used to verify that connection between
102
// client and server is working fine
103
else if (service == ServiceEnumeration.RUN_TEST_SERVICE)
104             {
105                 caller.doRunTest();
106             }
107             // Is it the service to create an HTTP session?
108
else if (service == ServiceEnumeration.CREATE_SESSION_SERVICE)
109             {
110                 caller.doCreateSession();
111             }
112             else if (service == ServiceEnumeration.GET_VERSION_SERVICE)
113             {
114                 caller.doGetVersion();
115             }
116             else
117             {
118                 String JavaDoc message = "Unknown service [" + serviceName
119                     + "] in HTTP request.";
120
121                 LOGGER.error(message);
122                 throw new ServletException JavaDoc(message);
123             }
124         }
125         catch (NoClassDefFoundError JavaDoc e)
126         {
127             // try to display messages as descriptive as possible !
128
if (e.getMessage().startsWith("junit/framework"))
129             {
130                 String JavaDoc message = "You must put the JUnit jar in "
131                     + "your server classpath (in WEB-INF/lib for example)";
132
133                 LOGGER.error(message, e);
134                 throw new ServletException JavaDoc(message, e);
135             }
136             else
137             {
138                 String JavaDoc message = "You are missing a jar in your "
139                     + "classpath (class [" + e.getMessage()
140                     + "] could not " + "be found";
141
142                 LOGGER.error(message, e);
143                 throw new ServletException JavaDoc(message, e);
144             }
145         }
146     }
147
148     /**
149      * @param theRequest the HTTP request
150      * @return the service name of the service to call (there are 2 services
151      * "do test" and "get results"), extracted from the HTTP request
152      * @exception ServletException if the service to execute is missing from
153      * the HTTP request
154      */

155     private String JavaDoc getServiceName(HttpServletRequest JavaDoc theRequest)
156         throws ServletException JavaDoc
157     {
158         // Call the correct Service method
159
String JavaDoc queryString = theRequest.getQueryString();
160         String JavaDoc serviceName = ServletUtil.getQueryStringParameter(queryString,
161             HttpServiceDefinition.SERVICE_NAME_PARAM);
162
163         if (serviceName == null)
164         {
165             String JavaDoc message = "Missing service name parameter ["
166                 + HttpServiceDefinition.SERVICE_NAME_PARAM
167                 + "] in HTTP request. Received query string is ["
168                 + queryString + "].";
169
170             LOGGER.debug(message);
171             throw new ServletException JavaDoc(message);
172         }
173
174         LOGGER.debug("Service to call = " + serviceName);
175
176         return serviceName;
177     }
178 }
179
Popular Tags