001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: // Created on May 8, 2006
018: package edu.iu.uis.eden.test.web;
019:
020: import java.io.IOException;
021: import java.util.Arrays;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Random;
027:
028: import javax.servlet.ServletException;
029: import javax.servlet.ServletOutputStream;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: /**
035: * A trivial servlet which echoes back request params, and inserts a few
036: * other parameters which can be used for testing purposes, into parseable response metadata.
037: * @author Aaron Hamid (arh14 at cornell dot edu)
038: */
039: public class TestServlet extends HttpServlet {
040: private static final Random RANDOM = new Random();
041:
042: private static String getValue(Object value) {
043: if (value instanceof String[]) {
044: String[] va = (String[]) value;
045: if (va.length > 0) {
046: value = va[0];
047: } else {
048: value = "";
049: }
050: }
051: return String.valueOf(value);
052: }
053:
054: private static String redact(Object o) {
055: return "<!-- [transient start] -->" + String.valueOf(o)
056: + "<!-- [transient end] -->";
057: }
058:
059: protected void service(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: Map params = request.getParameterMap();
063: /* some data to redact */
064: Date date = new Date();
065: int rand = RANDOM.nextInt(10000);
066: /* some data that we will persist accross requests */
067: String s = request.getParameter("persist_num");
068: boolean first_creation;
069: int persist_num;
070: if (s == null) {
071: persist_num = RANDOM.nextInt(10000);
072: first_creation = true;
073: } else {
074: persist_num = Integer.parseInt(s);
075: first_creation = false;
076: }
077:
078: /* list of incoming params we know to be "variable" and thus need to be redacted
079: * We only need this because we are iterating through and printing all the incoming
080: * params arbitrarily...so we need to know which ones to skip. A real servlet wouldn't be
081: * arbitrarily echoing parameters and would intrinsically know about output that needs to be redacted
082: */
083: final List redact_params = Arrays.asList(new String[] {
084: "persist_num",
085: "TheRandomNumberWeGotFromTheInitialRequest" });
086:
087: ServletOutputStream out = response.getOutputStream();
088:
089: out.println("Method: " + request.getMethod() + "\r\n\r\n");
090:
091: Iterator it = params.entrySet().iterator();
092: /* first print out parameters for human interpretation */
093: out.println("Request parameters:\r\n");
094: while (it.hasNext()) {
095: Map.Entry entry = (Map.Entry) it.next();
096: out.print(entry.getKey() + ": ");
097: boolean redact = redact_params.contains(entry.getKey());
098: if (redact) {
099: out.print("<!-- [transient start] -->");
100: }
101: out.print(getValue(entry.getValue()));
102: if (redact) {
103: out.print("<!-- [transient end] -->");
104: }
105: out.print("\r\n");
106: }
107:
108: /* now let's put print the data that should be redacted before comparison */
109: out.print("\r\nSome data to redact:\r\n");
110: out.print("Date: " + redact(date) + "\r\n");
111: out.print("Random number: " + redact(new Integer(rand))
112: + "\r\n");
113: /* now print the persistent number */
114: out.print("Persistent number: "
115: + redact(new Integer(persist_num))
116: + (first_creation ? " (created)"
117: : " (taken from input parameters)"));
118:
119: /* then use meta-data notation to print those parameters in a format that can be interpreted
120: * by the script
121: */
122: out
123: .print("\r\n<!-- Meta-data block for automation/testing\r\n");
124: it = params.entrySet().iterator();
125: while (it.hasNext()) {
126: Map.Entry entry = (Map.Entry) it.next();
127: boolean redact = redact_params.contains(entry.getKey());
128: if (redact) {
129: out.print("redacting: " + entry.getKey() + "\r\n");
130: out.print("[transient start]\r\n");
131: }
132: out.print("[var " + entry.getKey() + "="
133: + getValue(entry.getValue()) + "]\r\n");
134: if (redact) {
135: out.print("[transient end]\r\n");
136: }
137: }
138: /* now let's put some data to be redacted in there
139: * the script knows to redact anything between [transient start]/[transient end]
140: * (maybe that is not the best choice of words)
141: */
142: out.print("some data to redact follows\r\n");
143: out.print("[transient start]\r\n");
144: out.print("[var date=" + date + "]\r\n");
145: out.print("[var random_number=" + rand + "]\r\n");
146: /* finally write that number we want to persist accross requests
147: * This is just a normal variable. We will configure our script to
148: * look for it, and resend it, to simulate, for example, a docId.
149: * It is in the "transient" block because it is random (so perhaps
150: * "transient" should be changed to "variable" or "dynamic" or simply "redact"
151: * to contrast against "static" unchanging content which can be easily
152: * compared for verification. The redaction will occur AFTER
153: * the variable is parsed.
154: */
155: out.print("[var persist_num=" + persist_num + "]\r\n");
156: out.print("[transient end]\r\n");
157: out.print("-->");
158: }
159: }
|