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: package edu.iu.uis.eden.test.web.framework.actions;
018:
019: import java.util.Map;
020: import java.util.regex.Matcher;
021: import java.util.regex.Pattern;
022:
023: import org.w3c.dom.Node;
024:
025: import edu.iu.uis.eden.test.web.framework.Filter;
026: import edu.iu.uis.eden.test.web.framework.Property;
027: import edu.iu.uis.eden.test.web.framework.PropertyScheme;
028: import edu.iu.uis.eden.test.web.framework.Script;
029: import edu.iu.uis.eden.test.web.framework.ScriptState;
030: import edu.iu.uis.eden.test.web.framework.Util;
031:
032: /**
033: * ScriptAction that submits an HTTP request with parameters in the current RESPONSE map,
034: * and populates the RESPONSE variable map with any variables defined in meta-data
035: * syntax in the output.
036: * <pre>
037: * <submit [ method="..." ] [ uri="..." ]/>
038: * </pre>
039: * The 'uri' attribute is resolved via {@link edu.iu.uis.eden.test.web.framework.Util#getResolvableAttribute(Node, String, PropertyScheme)},
040: * defaulting to literal scheme.<br/>
041: * <br/>
042: * The meta-data syntax is:<br/>
043: * <br/>
044: * <code>(?i)\\[var ([\\p{Alnum}_\\.\\[\\]]+)=(.*)\\]</code><br/>
045: * <br/>
046: * e.g.<br/>
047: * <pre>
048: * <!--
049: * let's define some variables!
050: * [var color=green]
051: * [var shape=square]
052: * ok, 'color' and 'shape' will now be present in the RESPONSE map once the response is parsed!
053: * -->
054: * </pre>
055: * There are also some "special" variables defined:<br/>
056: * <dl>
057: * <dt>OUTPUT</dt>
058: * <dd>The literal response output before any filtering</dd>
059: * <dt>FILTERED_OUTPUT</dt>
060: * <dd>The response output after filters have been applied</dd>
061: * </dl>
062: * The only filter applied by default at this time is the CANONICALIZE filter.
063: * @author Aaron Hamid (arh14 at cornell dot edu)
064: */
065: public class SubmitAction extends BaseScriptAction {
066: public static final String RESPONSE_OUTPUT = "OUTPUT";
067: public static final String RESPONSE_FILTERED_OUTPUT = "FILTERED_OUTPUT";
068:
069: private static final String[] NAMES = { "submit" };
070: private static final Pattern RESPONSE_VAR_PATTERN;
071: static {
072: RESPONSE_VAR_PATTERN = Pattern
073: .compile("(?i)\\[var ([\\p{Alnum}_\\.\\[\\]]+)=(.*)\\]");
074: }
075:
076: public String[] getNames() {
077: return NAMES;
078: }
079:
080: public void process(Script script, Node node) {
081: String method = Util.getAttribute(node, "method");
082: if (method == null) {
083: method = "GET";
084: }
085:
086: Property uriProp = Util.getResolvableAttribute(node, "uri",
087: PropertyScheme.LITERAL_SCHEME);
088: String uri = null;
089: if (uriProp != null) {
090: Object uriValue = script.getState().retrieveProperty(
091: uriProp);
092: if (uriValue == null) {
093: String message = "Could not load uri property: "
094: + uriProp;
095: log.error(message);
096: throw new RuntimeException(message);
097: }
098: uri = Util.getAsString(uriValue, "uri");
099: }
100:
101: String user = script.getState().getUser();
102: log.info("Submitting request: user=" + user + ", method="
103: + method + ", uri=" + uri);
104: String output;
105: try {
106: output = script.getController().submit(method, uri, script);
107: } catch (Exception e) {
108: throw new RuntimeException("Error invoking controller", e);
109: }
110:
111: Map response = script.getState().getResponse();
112: response.clear();
113: response.put(RESPONSE_OUTPUT, output);
114:
115: Matcher matcher = RESPONSE_VAR_PATTERN.matcher(output);
116: /* TODO - look into Matcher.find()... could be problem
117: * EX: Pattern [-+]?[0-9]*\.?[0-9]+
118: * Value to Match = 123456.a34
119: * Matcher.matches() = false
120: * Matcher.find() = true
121: * Matcher.group(0) = 123456.
122: * next find() = 34
123: */
124: while (matcher.find()) {
125: String name = matcher.group(1);
126: String value = matcher.group(2);
127: log.info("Extracted variable from response: " + name + "="
128: + value);
129: response.put(name, value);
130: }
131:
132: //log.info("Submit response: " + output);
133: Filter filter = (Filter) script.getState().getFilters().get(
134: ScriptState.CANONICALIZE_FILTER_NAME);
135: if (filter == null) {
136: log.warn(ScriptState.CANONICALIZE_FILTER_NAME
137: + " filter not found");
138: } else {
139: output = ScriptState.CANONICALIZE_FILTER.filter(output);
140: }
141: response.put(RESPONSE_FILTERED_OUTPUT, output);
142: }
143: }
|