001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.recorder;
014:
015: import org.apache.commons.httpclient.HttpClient;
016: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
017: import org.apache.commons.httpclient.NameValuePair;
018: import org.apache.commons.httpclient.methods.GetMethod;
019: import org.apache.commons.httpclient.methods.PostMethod;
020:
021: import java.io.IOException;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: /**
026: * @author hengels
027: */
028: public abstract class Script {
029: private HttpClient client;
030: private float delay = 1.0f;
031: private String url;
032:
033: public Script() {
034: client = new HttpClient(
035: new MultiThreadedHttpConnectionManager());
036: }
037:
038: public void setDelay(float delay) {
039: this .delay = delay;
040: }
041:
042: public void setTimeout(int timeout) {
043: client.setConnectionTimeout(timeout);
044: }
045:
046: public void setUrl(String url) {
047: if (url.endsWith("/"))
048: this .url = url;
049: else
050: this .url = url + "/";
051: }
052:
053: public void delay(long time) {
054: if (delay > 0.0) {
055: try {
056: Thread.sleep((long) (time * delay));
057: } catch (InterruptedException e) { /* shit happens */
058: }
059: }
060: }
061:
062: public String send(GET request) throws IOException {
063: GetMethod get = new GetMethod(url + request.getResource());
064: addHeaders(request, get);
065:
066: NameValuePair[] nvps = eventsAsNameValuePairs(request);
067: get.setQueryString(nvps);
068:
069: int result = client.executeMethod(get);
070: return get.getResponseBodyAsString();
071: }
072:
073: public String send(POST request) throws IOException {
074: PostMethod post = new PostMethod(url + request.getResource());
075: addHeaders(request, post);
076:
077: NameValuePair[] nvps = eventsAsNameValuePairs(request);
078: post.setRequestBody(nvps);
079:
080: int result = client.executeMethod(post);
081: return post.getResponseBodyAsString();
082: }
083:
084: private void addHeaders(Request request, GetMethod post) {
085: List headers = request.getHeaders();
086: for (Iterator iterator = headers.iterator(); iterator.hasNext();) {
087: Request.Header header = (Request.Header) iterator.next();
088: post.addRequestHeader(header.getName(), header.getValue());
089: }
090: }
091:
092: private NameValuePair[] eventsAsNameValuePairs(Request request) {
093: List events = request.getEvents();
094: int size = 0;
095: for (Iterator iterator = events.iterator(); iterator.hasNext();) {
096: Request.Event event = (Request.Event) iterator.next();
097: size += event.getValues().length;
098: }
099:
100: NameValuePair[] nvps = new NameValuePair[size];
101: int index = 0;
102: for (Iterator iterator = events.iterator(); iterator.hasNext();) {
103: Request.Event event = (Request.Event) iterator.next();
104: String[] values = event.getValues();
105: for (int i = 0; i < values.length; i++) {
106: String value = values[i];
107: nvps[index++] = new NameValuePair(event.getName(),
108: value);
109: }
110: }
111: return nvps;
112: }
113:
114: public abstract void execute() throws Exception;
115:
116: public static void main(String[] args) throws Exception {
117: String name = args[0];
118: String url = args[1];
119: float delay = args.length == 3 ? Float.parseFloat(args[2])
120: : 1.0f;
121:
122: Class clazz = Class.forName(name);
123: Script script = (Script) clazz.newInstance();
124: script.setUrl(url);
125: script.setDelay(delay);
126: script.execute();
127: }
128: }
|