KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > recorder > Script


1 /* $Id: Script.java,v 1.3 2004/12/01 07:54:26 hengels Exp $ */
2 /*
3  * $Id: Script.java,v 1.3 2004/12/01 07:54:26 hengels Exp $
4  * Copyright 2000,2005 wingS development team.
5  *
6  * This file is part of wingS (http://www.j-wings.org).
7  *
8  * wingS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * Please see COPYING for the complete licence.
14  */

15 package org.wings.recorder;
16
17 import org.apache.commons.httpclient.HttpClient;
18 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
19 import org.apache.commons.httpclient.NameValuePair;
20 import org.apache.commons.httpclient.methods.GetMethod;
21 import org.apache.commons.httpclient.methods.PostMethod;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * @author hengels
29  */

30 public abstract class Script {
31     private HttpClient client;
32     private float delay = 1.0f;
33     private String JavaDoc url;
34
35     public Script() {
36         client = new HttpClient(new MultiThreadedHttpConnectionManager());
37     }
38
39     public void setDelay(float delay) { this.delay = delay; }
40
41     public void setTimeout(int timeout) { client.setConnectionTimeout(timeout); }
42
43     public void setUrl(String JavaDoc url) {
44         if (url.endsWith("/"))
45             this.url = url;
46         else
47             this.url = url + "/";
48     }
49
50     public void delay(long time) {
51         if (delay > 0.0) {
52             try {
53                 Thread.sleep((long) (time * delay));
54             } catch (InterruptedException JavaDoc e) { /* shit happens */ }
55         }
56     }
57
58     public String JavaDoc send(GET request) throws IOException JavaDoc {
59         GetMethod get = new GetMethod(url + request.getResource());
60         addHeaders(request, get);
61
62         NameValuePair[] nvps = eventsAsNameValuePairs(request);
63         get.setQueryString(nvps);
64
65         int result = client.executeMethod(get);
66         return get.getResponseBodyAsString();
67     }
68
69     public String JavaDoc send(POST request) throws IOException JavaDoc {
70         PostMethod post = new PostMethod(url + request.getResource());
71         addHeaders(request, post);
72
73         NameValuePair[] nvps = eventsAsNameValuePairs(request);
74         post.setRequestBody(nvps);
75
76         int result = client.executeMethod(post);
77         return post.getResponseBodyAsString();
78     }
79
80     private void addHeaders(Request request, GetMethod post) {
81         List JavaDoc headers = request.getHeaders();
82         for (Iterator JavaDoc iterator = headers.iterator(); iterator.hasNext();) {
83             Request.Header header = (Request.Header) iterator.next();
84             post.addRequestHeader(header.getName(), header.getValue());
85         }
86     }
87
88     private NameValuePair[] eventsAsNameValuePairs(Request request) {
89         List JavaDoc events = request.getEvents();
90         int size = 0;
91         for (Iterator JavaDoc iterator = events.iterator(); iterator.hasNext();) {
92             Request.Event event = (Request.Event) iterator.next();
93             size += event.getValues().length;
94         }
95
96         NameValuePair[] nvps = new NameValuePair[size];
97         int index = 0;
98         for (Iterator JavaDoc iterator = events.iterator(); iterator.hasNext();) {
99             Request.Event event = (Request.Event) iterator.next();
100             String JavaDoc[] values = event.getValues();
101             for (int i = 0; i < values.length; i++) {
102                 String JavaDoc value = values[i];
103                 nvps[index++] = new NameValuePair(event.getName(), value);
104             }
105         }
106         return nvps;
107     }
108
109     public abstract void execute() throws Exception JavaDoc;
110
111     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
112         String JavaDoc name = args[0];
113         String JavaDoc url = args[1];
114         float delay = args.length == 3 ? Float.parseFloat(args[2]) : 1.0f;
115
116         Class JavaDoc clazz = Class.forName(name);
117         Script script = (Script) clazz.newInstance();
118         script.setUrl(url);
119         script.setDelay(delay);
120         script.execute();
121     }
122 }
123
Popular Tags