|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractWebTestController.java | 28.6% | 42.4% | 100% | 40.8% |
|
||||||||||||||
| 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;
|
|
| 23 |
import javax.servlet.http.HttpServletRequest;
|
|
| 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 238991 2004-05-22 11:34:50Z vmassol $
|
|
| 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 | 48 |
public void handleRequest(ImplicitObjects theObjects) |
| 65 |
throws ServletException
|
|
| 66 |
{
|
|
| 67 | 48 |
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 | 48 |
try
|
| 79 |
{
|
|
| 80 | 48 |
String serviceName = |
| 81 |
getServiceName(webImplicitObjects.getHttpServletRequest()); |
|
| 82 |
|
|
| 83 | 48 |
AbstractWebTestCaller caller = getTestCaller(webImplicitObjects); |
| 84 |
|
|
| 85 |
// TODO: will need a factory here real soon...
|
|
| 86 |
|
|
| 87 | 48 |
ServiceEnumeration service = |
| 88 |
ServiceEnumeration.valueOf(serviceName); |
|
| 89 |
|
|
| 90 |
// Is it the call test method service ?
|
|
| 91 | 48 |
if (service == ServiceEnumeration.CALL_TEST_SERVICE)
|
| 92 |
{
|
|
| 93 | 24 |
caller.doTest(); |
| 94 |
} |
|
| 95 |
// Is it the get test results service ?
|
|
| 96 | 24 |
else if (service == ServiceEnumeration.GET_RESULTS_SERVICE) |
| 97 |
{
|
|
| 98 | 24 |
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 | 0 |
else if (service == ServiceEnumeration.RUN_TEST_SERVICE) |
| 104 |
{
|
|
| 105 | 0 |
caller.doRunTest(); |
| 106 |
} |
|
| 107 |
// Is it the service to create an HTTP session?
|
|
| 108 | 0 |
else if (service == ServiceEnumeration.CREATE_SESSION_SERVICE) |
| 109 |
{
|
|
| 110 | 0 |
caller.doCreateSession(); |
| 111 |
} |
|
| 112 | 0 |
else if (service == ServiceEnumeration.GET_VERSION_SERVICE) |
| 113 |
{
|
|
| 114 | 0 |
caller.doGetVersion(); |
| 115 |
} |
|
| 116 |
else
|
|
| 117 |
{
|
|
| 118 | 0 |
String message = "Unknown service [" + serviceName
|
| 119 |
+ "] in HTTP request.";
|
|
| 120 |
|
|
| 121 | 0 |
LOGGER.error(message); |
| 122 | 0 |
throw new ServletException(message); |
| 123 |
} |
|
| 124 |
} |
|
| 125 |
catch (NoClassDefFoundError e)
|
|
| 126 |
{
|
|
| 127 |
// try to display messages as descriptive as possible !
|
|
| 128 | 0 |
if (e.getMessage().startsWith("junit/framework")) |
| 129 |
{
|
|
| 130 | 0 |
String message = "You must put the JUnit jar in "
|
| 131 |
+ "your server classpath (in WEB-INF/lib for example)";
|
|
| 132 |
|
|
| 133 | 0 |
LOGGER.error(message, e); |
| 134 | 0 |
throw new ServletException(message, e); |
| 135 |
} |
|
| 136 |
else
|
|
| 137 |
{
|
|
| 138 | 0 |
String message = "You are missing a jar in your "
|
| 139 |
+ "classpath (class [" + e.getMessage()
|
|
| 140 |
+ "] could not " + "be found"; |
|
| 141 |
|
|
| 142 | 0 |
LOGGER.error(message, e); |
| 143 | 0 |
throw new ServletException(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 | 48 |
private String getServiceName(HttpServletRequest theRequest)
|
| 156 |
throws ServletException
|
|
| 157 |
{
|
|
| 158 |
// Call the correct Service method
|
|
| 159 | 48 |
String queryString = theRequest.getQueryString(); |
| 160 | 48 |
String serviceName = ServletUtil.getQueryStringParameter(queryString, |
| 161 |
HttpServiceDefinition.SERVICE_NAME_PARAM); |
|
| 162 |
|
|
| 163 | 48 |
if (serviceName == null) |
| 164 |
{
|
|
| 165 | 0 |
String message = "Missing service name parameter ["
|
| 166 |
+ HttpServiceDefinition.SERVICE_NAME_PARAM |
|
| 167 |
+ "] in HTTP request. Received query string is ["
|
|
| 168 |
+ queryString + "].";
|
|
| 169 |
|
|
| 170 | 0 |
LOGGER.debug(message); |
| 171 | 0 |
throw new ServletException(message); |
| 172 |
} |
|
| 173 |
|
|
| 174 | 48 |
LOGGER.debug("Service to call = " + serviceName);
|
| 175 |
|
|
| 176 | 48 |
return serviceName;
|
| 177 |
} |
|
| 178 |
} |
|
| 179 |
|
|
||||||||||