KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > sampler > SoapSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/SoapSampler.java,v 1.11.2.1 2004/09/21 18:25:37 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.sampler;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.net.HttpURLConnection JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27
28 import org.apache.jmeter.samplers.Entry;
29 import org.apache.jmeter.samplers.SampleResult;
30 import org.apache.jorphan.logging.LoggingManager;
31 import org.apache.log.Logger;
32 import org.apache.jmeter.protocol.http.control.HeaderManager;
33 import org.apache.jmeter.protocol.http.control.Header;
34
35 /**
36  * Sampler to handle SOAP Requests.
37  *
38  * @author Jordi Salvat i Alabart
39  * @version $Id: SoapSampler.java,v 1.11.2.1 2004/09/21 18:25:37 sebb Exp $
40  */

41 public class SoapSampler extends HTTPSampler
42 {
43     private static Logger log = LoggingManager.getLoggerForClass();
44     public static final String JavaDoc XML_DATA = "HTTPSamper.xml_data";
45     public static final String JavaDoc URL_DATA = "SoapSampler.URL_DATA";
46
47     public void setXmlData(String JavaDoc data)
48     {
49         setProperty(XML_DATA, data);
50     }
51
52     public String JavaDoc getXmlData()
53     {
54         return getPropertyAsString(XML_DATA);
55     }
56
57     public String JavaDoc getURLData()
58     {
59         return getPropertyAsString(URL_DATA);
60     }
61
62     public void setURLData(String JavaDoc url)
63     {
64         setProperty(URL_DATA, url);
65     }
66
67     /**
68      * Set the HTTP request headers in preparation to open the connection
69      * and sending the POST data.
70      *
71      * @param connection <code>URLConnection</code> to set headers on
72      * @exception IOException if an I/O exception occurs
73      */

74     public void setPostHeaders(URLConnection JavaDoc connection) throws IOException JavaDoc
75     {
76         ((HttpURLConnection JavaDoc) connection).setRequestMethod("POST");
77         connection.setRequestProperty(
78             "Content-Length",
79             "" + getXmlData().length());
80         // my first attempt at fixing the bug failed, due to user
81
// error on my part. HeaderManager does not use the normal
82
// setProperty, and getPropertyAsString methods. Instead,
83
// it uses it's own String array and Header object.
84
if (getHeaderManager() != null){
85                 // headerManager was set, so let's set the connection
86
// to use it.
87
HeaderManager mngr = getHeaderManager();
88                 int headerSize = mngr.size();
89                 // we set all the header properties
90
for (int idx=0; idx < headerSize; idx++){
91                     Header hd = mngr.getHeader(idx);
92                     connection.setRequestProperty(hd.getName(),hd.getValue());
93                 }
94         } else {
95             // otherwise we use "text/xml" as the default
96
connection.setRequestProperty("Content-Type", "text/xml");
97         }
98         connection.setDoOutput(true);
99     }
100
101     /**
102      * Send POST data from <code>Entry</code> to the open connection.
103      *
104      * @param connection <code>URLConnection</code> of where POST data
105      * should be sent
106      * @exception IOException if an I/O exception occurs
107      */

108     public void sendPostData(URLConnection JavaDoc connection) throws IOException JavaDoc
109     {
110         PrintWriter JavaDoc out = new PrintWriter JavaDoc(connection.getOutputStream());
111         out.print(getXmlData());
112         out.close();
113     }
114     
115     /* (non-Javadoc)
116      * @see Sampler#sample(Entry)
117      */

118     public SampleResult sample(Entry e)
119     {
120         try
121         {
122             URL JavaDoc url = new URL JavaDoc(getURLData());
123             setDomain(url.getHost());
124             setPort(url.getPort());
125             setProtocol(url.getProtocol());
126             setMethod(POST);
127             if (url.getQuery() != null && url.getQuery().compareTo("") != 0)
128             {
129                 setPath(url.getPath() + "?" + url.getQuery());
130             }
131             else
132             {
133                 setPath(url.getPath());
134             }
135             // make sure the Post header is set
136
URLConnection JavaDoc conn = url.openConnection();
137             setPostHeaders(conn);
138         }
139         catch (MalformedURLException JavaDoc e1)
140         {
141             log.error("Bad url: " + getURLData(), e1);
142         }
143         catch (IOException JavaDoc e1){
144             log.error("Bad url: " + getURLData(), e1);
145         }
146         return super.sample(e);
147     }
148
149     public String JavaDoc toString()
150     {
151         try
152         {
153             String JavaDoc xml = getXmlData();
154             if (xml.length() > 100)
155             {
156                 xml = xml.substring(0, 100);
157             }
158             return this.getUrl().toString() + "\nXML Data: " + xml;
159         }
160         catch (MalformedURLException JavaDoc e)
161         {
162             return "";
163         }
164     }
165 }
166
Popular Tags