001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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: */
018:
019: package org.apache.jmeter.protocol.http.control;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.FileWriter;
025: import java.io.IOException;
026: import java.io.PrintWriter;
027: import java.io.Serializable;
028: import java.util.ArrayList;
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.apache.jmeter.config.ConfigTestElement;
033: import org.apache.jmeter.testelement.property.CollectionProperty;
034: import org.apache.jmeter.util.JMeterUtils;
035: import org.apache.jorphan.util.JOrphanUtils;
036:
037: /**
038: * This class provides an interface to headers file to pass HTTP headers along
039: * with a request.
040: *
041: * author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
042: * @version $Revision: 535138 $ $Date: 2007-05-04 10:29:15 +0100 (Fri, 04 May 2007) $
043: */
044: public class HeaderManager extends ConfigTestElement implements
045: Serializable {
046:
047: public static final String HEADERS = "HeaderManager.headers";// $NON-NLS-1$
048:
049: private final static int columnCount = 2;
050:
051: private final static String[] columnNames = {
052: JMeterUtils.getResString("name")// $NON-NLS-1$
053: , JMeterUtils.getResString("value") };// $NON-NLS-1$
054:
055: /**
056: * Apache SOAP driver does not provide an easy way to get and set the cookie
057: * or HTTP header. Therefore it is necessary to store the SOAPHTTPConnection
058: * object and reuse it.
059: */
060: private Object SOAPHeader = null;
061:
062: public HeaderManager() {
063: setProperty(new CollectionProperty(HEADERS, new ArrayList()));
064: }
065:
066: public void clear() {
067: super .clear();
068: setProperty(new CollectionProperty(HEADERS, new ArrayList()));
069: }
070:
071: public CollectionProperty getHeaders() {
072: return (CollectionProperty) getProperty(HEADERS);
073: }
074:
075: public int getColumnCount() {
076: return columnCount;
077: }
078:
079: public String getColumnName(int column) {
080: return columnNames[column];
081: }
082:
083: public Class getColumnClass(int column) {
084: return columnNames[column].getClass();
085: }
086:
087: public Header getHeader(int row) {
088: return (Header) getHeaders().get(row).getObjectValue();
089: }
090:
091: /**
092: * Save the header data to a file.
093: */
094: public void save(String headFile) throws IOException {
095: File file = new File(headFile);
096: if (!file.isAbsolute()) {
097: file = new File(System.getProperty("user.dir")// $NON-NLS-1$
098: + File.separator + headFile);
099: }
100: PrintWriter writer = new PrintWriter(new FileWriter(file));
101: writer.println("# JMeter generated Header file");// $NON-NLS-1$
102: for (int i = 0; i < getHeaders().size(); i++) {
103: Header head = (Header) getHeaders().get(i);
104: writer.println(head.toString());
105: }
106: writer.flush();
107: writer.close();
108: }
109:
110: /**
111: * Add header data from a file.
112: */
113: public void addFile(String headerFile) throws IOException {
114: File file = new File(headerFile);
115: if (!file.isAbsolute()) {
116: file = new File(System.getProperty("user.dir")// $NON-NLS-1$
117: + File.separator + headerFile);
118: }
119: BufferedReader reader = null;
120: if (file.canRead()) {
121: reader = new BufferedReader(new FileReader(file));
122: } else {
123: throw new IOException(
124: "The file you specified cannot be read.");
125: }
126:
127: String line;
128: while ((line = reader.readLine()) != null) {
129: try {
130: if (line.startsWith("#") || line.trim().length() == 0) {// $NON-NLS-1$
131: continue;
132: }
133: String[] st = JOrphanUtils.split(line, "\t", " ");// $NON-NLS-1$ $NON-NLS-2$
134: int name = 0;
135: int value = 1;
136: Header header = new Header(st[name], st[value]);
137: getHeaders().addItem(header);
138: } catch (Exception e) {
139: throw new IOException("Error parsing header line\n\t'"
140: + line + "'\n\t" + e);
141: }
142: }
143: reader.close();
144: }
145:
146: /**
147: * Add a header.
148: */
149: public void add(Header h) {
150: getHeaders().addItem(h);
151: }
152:
153: /**
154: * Add an empty header.
155: */
156: public void add() {
157: getHeaders().addItem(new Header());
158: }
159:
160: /**
161: * Remove a header.
162: */
163: public void remove(int index) {
164: getHeaders().remove(index);
165: }
166:
167: /**
168: * Return the number of headers.
169: */
170: public int size() {
171: return getHeaders().size();
172: }
173:
174: /**
175: * Return the header at index i.
176: */
177: public Header get(int i) {
178: return (Header) getHeaders().get(i).getObjectValue();
179: }
180:
181: /*
182: * public String getHeaderHeaderForURL(URL url) { if
183: * (!url.getProtocol().toUpperCase().trim().equals("HTTP") &&
184: * !url.getProtocol().toUpperCase().trim().equals("HTTPS")) { return null; }
185: *
186: * StringBuffer sbHeader = new StringBuffer(); for (Iterator enum =
187: * headers.iterator(); enum.hasNext();) { Header header = (Header)
188: * enum.next(); if (url.getHost().endsWith(header.getDomain()) &&
189: * url.getFile().startsWith(header.getPath()) && (System.currentTimeMillis() /
190: * 1000) <= header.getExpires()) { if (sbHeader.length() > 0) {
191: * sbHeader.append("; "); }
192: * sbHeader.append(header.getName()).append("=").append( header.getValue()); } }
193: *
194: * if (sbHeader.length() != 0) { return sbHeader.toString(); } else { return
195: * null; } }
196: */
197:
198: /*
199: * public void addHeaderFromHeader(String headerHeader, URL url) {
200: * StringTokenizer st = new StringTokenizer(headerHeader, ";"); String nvp;
201: * // first n=v is name=value nvp = st.nextToken(); int index =
202: * nvp.indexOf("="); String name = nvp.substring(0, index); String value =
203: * nvp.substring(index + 1); String domain = url.getHost();
204: *
205: * Header newHeader = new Header(name, value); // check the rest of the
206: * headers while (st.hasMoreTokens()) { nvp = st.nextToken(); nvp =
207: * nvp.trim(); index = nvp.indexOf("="); if (index == -1) { index =
208: * nvp.length(); } String key = nvp.substring(0, index);
209: *
210: * Vector removeIndices = new Vector(); for (int i = headers.size() - 1; i >=
211: * 0; i--) { Header header = (Header) headers.get(i); if (header == null) {
212: * continue; } if (header.getName().equals(newHeader.getName())) {
213: * removeIndices.addElement(new Integer(i)); } }
214: *
215: * for (Enumeration e = removeIndices.elements(); e.hasMoreElements(); ) {
216: * index = ((Integer) e.nextElement()).intValue(); headers.remove(index); }
217: * }
218: */
219: public void removeHeaderNamed(String name) {
220: Vector removeIndices = new Vector();
221: for (int i = getHeaders().size() - 1; i >= 0; i--) {
222: Header header = (Header) getHeaders().get(i)
223: .getObjectValue();
224: if (header == null) {
225: continue;
226: }
227: if (header.getName().equalsIgnoreCase(name)) {
228: removeIndices.addElement(new Integer(i));
229: }
230: }
231:
232: for (Enumeration e = removeIndices.elements(); e
233: .hasMoreElements();) {
234: getHeaders().remove(((Integer) e.nextElement()).intValue());
235: }
236: }
237:
238: public String getClassLabel() {
239: return JMeterUtils.getResString("header_manager_title");// $NON-NLS-1$
240: }
241:
242: /**
243: * Added support for SOAP related header stuff. 1-29-04 Peter Lin
244: *
245: * @return the SOAP header Object
246: */
247: public Object getSOAPHeader() {
248: return this .SOAPHeader;
249: }
250:
251: /**
252: * Set the SOAPHeader with the SOAPHTTPConnection object. We may or may not
253: * want to rename this to setHeaderObject(Object). Concievably, other
254: * samplers may need this kind of functionality. 1-29-04 Peter Lin
255: *
256: * @param header
257: */
258: public void setSOAPHeader(Object header) {
259: this.SOAPHeader = header;
260: }
261: }
|