01: /**
02: * 6/1/2006 by SolutionsIQ
03: * Processes an arbitrary SQL request against a specified database, returning the result as XML.
04: * Future versions may permits setting up persistent connections and sending multiple queries against them.
05: */package fitnesse.responders;
06:
07: import fitnesse.util.PropertiesUtil;
08: import fitnesse.FitNesseContext;
09: import fitnesse.Responder;
10: import fitnesse.http.Request;
11: import fitnesse.http.Response;
12: import fitnesse.http.SimpleResponse;
13: import fitnesse.responders.sql.SQLState;
14:
15: /**
16: * @author SolutionsIQ
17: *
18: */
19: public class SqlResponder implements Responder {
20: private static String STIQ_DATABASE_DRIVER = "STIQDatabaseDriver";
21: private static String STIQ_DATABASE_CONNECTION_STRING = "STIQDatabaseConnectionString";
22: private static String STIQ_DATABASE_USERNAME = "STIQDatabaseUsername";
23: private static String STIQ_DATABASE_PASSWORD = "STIQDatabasePassword";
24:
25: /**
26: * Serves up SQL responses from a database as XML.
27: *
28: * @see fitnesse.Responder#makeResponse(fitnesse.FitNesseContext,
29: * fitnesse.http.Request)
30: */
31: public Response makeResponse(FitNesseContext context,
32: Request request) throws Exception {
33: SimpleResponse response = new SimpleResponse();
34: response.setContentType(getContentType(request));
35: response.setContent("Hello world SQL 2!!!");
36: String driver = PropertiesUtil
37: .getPropertyOrFail(STIQ_DATABASE_DRIVER);
38: String connectionString = PropertiesUtil
39: .getPropertyOrFail(STIQ_DATABASE_CONNECTION_STRING);
40: String username = PropertiesUtil
41: .getPropertyOrFail(STIQ_DATABASE_USERNAME);
42: String password = PropertiesUtil
43: .getPropertyOrFail(STIQ_DATABASE_PASSWORD);
44:
45: SQLState sqlState = new SQLState();
46: sqlState.setData(driver, connectionString, username, password);
47: String sqlStatement = String.valueOf(request
48: .getInput("command"));
49: sqlState.executeStatement(java.net.URLDecoder.decode(
50: sqlStatement, "UTF-8"));
51: response.setContent(sqlState.getResultsAsXml());
52: return response;
53: }
54:
55: private String getContentType(Request request) {
56: if (request.getResource().endsWith(".hta")) {
57: return "application/hta";
58: }
59:
60: return "application/xml";
61: }
62:
63: }
|