KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > control > HeaderManager


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java,v 1.9.2.1 2004/05/19 21:24:24 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.control;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.apache.jmeter.config.ConfigTestElement;
34 import org.apache.jmeter.testelement.property.CollectionProperty;
35 import org.apache.jmeter.util.JMeterUtils;
36
37 /**
38  * This class provides an interface to headers file to
39  * pass HTTP headers along with a request.
40  *
41  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
42  * @version $Revision: 1.9.2.1 $ $Date: 2004/05/19 21:24:24 $
43  */

44 public class HeaderManager extends ConfigTestElement implements Serializable JavaDoc
45 {
46
47     public static final String JavaDoc HEADERS = "HeaderManager.headers";
48
49     private final static int columnCount = 2;
50     private final static String JavaDoc[] columnNames =
51         { JMeterUtils.getResString("name"), JMeterUtils.getResString("value")};
52     /**
53      * Apache SOAP driver does not provide an easy
54      * way to get and set the cookie or HTTP
55      * header. Therefore it is necessary to store
56      * the SOAPHTTPConnection object and reuse it.
57      */

58     private Object JavaDoc SOAPHeader = null;
59     
60     public HeaderManager()
61     {
62         setProperty(new CollectionProperty(HEADERS, new ArrayList JavaDoc()));
63     }
64
65     public CollectionProperty getHeaders()
66     {
67         return (CollectionProperty) getProperty(HEADERS);
68     }
69
70     public int getColumnCount()
71     {
72         return columnCount;
73     }
74
75     public String JavaDoc getColumnName(int column)
76     {
77         return columnNames[column];
78     }
79
80     public Class JavaDoc getColumnClass(int column)
81     {
82         return columnNames[column].getClass();
83     }
84
85     public Header getHeader(int row)
86     {
87         return (Header) getHeaders().get(row).getObjectValue();
88     }
89
90     /**
91      * Save the header data to a file.
92      */

93     public void save(String JavaDoc headFile) throws IOException JavaDoc
94     {
95         File JavaDoc file = new File JavaDoc(headFile);
96         if (!file.isAbsolute())
97         {
98             file =
99                 new File JavaDoc(
100                     System.getProperty("user.dir") + File.separator + headFile);
101         }
102         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
103         writer.println("# JMeter generated Header file");
104         for (int i = 0; i < getHeaders().size(); i++)
105         {
106             Header head = (Header) getHeaders().get(i);
107             writer.println(head.toString());
108         }
109         writer.flush();
110         writer.close();
111     }
112
113     /**
114      * Add header data from a file.
115      */

116     public void addFile(String JavaDoc headerFile) throws IOException JavaDoc
117     {
118         File JavaDoc file = new File JavaDoc(headerFile);
119         if (!file.isAbsolute())
120         {
121             file =
122                 new File JavaDoc(
123                     System.getProperty("user.dir")
124                         + File.separator
125                         + headerFile);
126         }
127         BufferedReader JavaDoc reader = null;
128         if (file.canRead())
129         {
130             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
131         }
132         else
133         {
134             throw new IOException JavaDoc("The file you specified cannot be read.");
135         }
136
137         String JavaDoc line;
138         while ((line = reader.readLine()) != null)
139         {
140             try
141             {
142                 if (line.startsWith("#") || line.trim().length() == 0)
143                 {
144                     continue;
145                 }
146                 String JavaDoc[] st = split(line, "\t", " ");
147                 int name = 0;
148                 int value = 1;
149                 Header header = new Header(st[name], st[value]);
150                 getHeaders().addItem(header);
151             }
152             catch (Exception JavaDoc e)
153             {
154                 throw new IOException JavaDoc(
155                     "Error parsing header line\n\t'" + line + "'\n\t" + e);
156             }
157         }
158         reader.close();
159     }
160
161     /**
162      * Add a header.
163      */

164     public void add(Header h)
165     {
166         getHeaders().addItem(h);
167     }
168
169     /**
170      * Add an empty header.
171      */

172     public void add()
173     {
174         getHeaders().addItem(new Header());
175     }
176
177     /**
178      * Remove a header.
179      */

180     public void remove(int index)
181     {
182         getHeaders().remove(index);
183     }
184
185     /**
186      * Return the number of headers.
187      */

188     public int size()
189     {
190         return getHeaders().size();
191     }
192
193     /**
194      * Return the header at index i.
195      */

196     public Header get(int i)
197     {
198         return (Header) getHeaders().get(i).getObjectValue();
199     }
200
201     /*
202     public String getHeaderHeaderForURL(URL url)
203     {
204         if (!url.getProtocol().toUpperCase().trim().equals("HTTP")
205             && !url.getProtocol().toUpperCase().trim().equals("HTTPS"))
206         {
207             return null;
208         }
209
210         StringBuffer sbHeader = new StringBuffer();
211         for (Iterator enum = headers.iterator(); enum.hasNext();)
212         {
213             Header header = (Header) enum.next();
214             if (url.getHost().endsWith(header.getDomain())
215                 && url.getFile().startsWith(header.getPath())
216                 && (System.currentTimeMillis() / 1000) <= header.getExpires())
217             {
218                 if (sbHeader.length() > 0)
219                 {
220                     sbHeader.append("; ");
221                 }
222                 sbHeader.append(header.getName()).append("=").append(
223                     header.getValue());
224             }
225         }
226
227         if (sbHeader.length() != 0)
228         {
229             return sbHeader.toString();
230         }
231         else
232         {
233             return null;
234         }
235     }
236     */

237     
238     /*
239     public void addHeaderFromHeader(String headerHeader, URL url)
240     {
241         StringTokenizer st = new StringTokenizer(headerHeader, ";");
242         String nvp;
243
244         // first n=v is name=value
245         nvp = st.nextToken();
246         int index = nvp.indexOf("=");
247         String name = nvp.substring(0, index);
248         String value = nvp.substring(index + 1);
249         String domain = url.getHost();
250
251         Header newHeader = new Header(name, value);
252         // check the rest of the headers
253         while (st.hasMoreTokens())
254         {
255             nvp = st.nextToken();
256             nvp = nvp.trim();
257             index = nvp.indexOf("=");
258             if (index == -1)
259             {
260                 index = nvp.length();
261             }
262             String key = nvp.substring(0, index);
263
264             Vector removeIndices = new Vector();
265             for (int i = headers.size() - 1; i >= 0; i--)
266             {
267                 Header header = (Header) headers.get(i);
268                 if (header == null)
269                 {
270                     continue;
271                 }
272                 if (header.getName().equals(newHeader.getName()))
273                 {
274                     removeIndices.addElement(new Integer(i));
275                 }
276             }
277
278             for (Enumeration e = removeIndices.elements();
279                 e.hasMoreElements();
280                 )
281             {
282                 index = ((Integer) e.nextElement()).intValue();
283                 headers.remove(index);
284             }
285
286         }
287         */

288     public void removeHeaderNamed(String JavaDoc name)
289     {
290         Vector JavaDoc removeIndices = new Vector JavaDoc();
291         for (int i = getHeaders().size() - 1; i >= 0; i--)
292         {
293             Header header = (Header) getHeaders().get(i).getObjectValue();
294             if (header == null)
295             {
296                 continue;
297             }
298             if (header.getName().equalsIgnoreCase(name))
299             {
300                 removeIndices.addElement(new Integer JavaDoc(i));
301             }
302         }
303
304         for (Enumeration JavaDoc e = removeIndices.elements(); e.hasMoreElements();)
305         {
306             getHeaders().remove(((Integer JavaDoc) e.nextElement()).intValue());
307         }
308     }
309
310     /**
311      * Takes a String and a tokenizer character, and returns a new array of
312      * strings of the string split by the tokenizer character.
313      *
314      * @param splittee string to be split
315      * @param splitChar character to split the string on
316      * @param def default value to place between two split chars that have
317      * nothing between them
318      * @return array of all the tokens.
319      */

320     public String JavaDoc[] split(String JavaDoc splittee, String JavaDoc splitChar, String JavaDoc def)
321     {
322         if (splittee == null || splitChar == null)
323         {
324             return new String JavaDoc[0];
325         }
326         StringTokenizer JavaDoc tokens;
327         String JavaDoc temp;
328         int spot;
329         while ((spot = splittee.indexOf(splitChar + splitChar)) != -1)
330         {
331             splittee =
332                 splittee.substring(0, spot + splitChar.length())
333                     + def
334                     + splittee.substring(
335                         spot + (1 * splitChar.length()),
336                         splittee.length());
337         }
338         Vector JavaDoc returns = new Vector JavaDoc();
339         tokens = new StringTokenizer JavaDoc(splittee, splitChar);
340         while (tokens.hasMoreTokens())
341         {
342             temp = (String JavaDoc) tokens.nextToken();
343             returns.addElement(temp);
344         }
345         String JavaDoc[] values = new String JavaDoc[returns.size()];
346         returns.copyInto(values);
347         return values;
348     }
349
350     public String JavaDoc getClassLabel()
351     {
352         return JMeterUtils.getResString("header_manager_title");
353     }
354     
355     /**
356      * Added support for SOAP related header stuff.
357      * 1-29-04 Peter Lin
358      * @return the SOAP header Object
359      */

360     public Object JavaDoc getSOAPHeader(){
361         return this.SOAPHeader;
362     }
363     
364     /**
365      * Set the SOAPHeader with the SOAPHTTPConnection
366      * object. We may or may not want to rename this
367      * to setHeaderObject(Object). Concievably, other
368      * samplers may need this kind of functionality.
369      * 1-29-04 Peter Lin
370      * @param header
371      */

372     public void setSOAPHeader(Object JavaDoc header){
373         this.SOAPHeader = header;
374     }
375 }
376
Popular Tags