package org.codehaus.xfire.transport.http; import java.io.IOException; import java.io.InputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.methods.PostMethod; import org.codehaus.xfire.XFireException; import org.codehaus.xfire.exchange.InMessage; import org.codehaus.xfire.fault.XFireFault; import org.codehaus.xfire.util.STAXUtils; /** * Http Sender * * @author Dan Diephouse * @since Oct 26, 2004 */ public class HttpMessageSender { private String username; private String password; private String encoding; private String urlString; private String action; private PostMethod postMethod; private HttpClient client; public HttpMessageSender(String urlString, String encoding) { this.urlString = urlString; this.encoding = encoding; } public void open() throws IOException, XFireFault { client = new HttpClient(); client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); client.getParams() .setParameter("http.useragent", "XFire Client +http://xfire.codehaus.org"); client.getParams().setParameter("http.protocol.content-charset", encoding); postMethod = new PostMethod(urlString); postMethod.setRequestHeader("Content-Type", "text/xml; charset="+this.encoding); if (action != null) { postMethod.setRequestHeader("SOAPAction", action); } } public void send() throws HttpException, IOException { client.executeMethod(postMethod); } public InMessage getInMessage() throws IOException { InputStream in = postMethod.getResponseBodyAsStream(); return new InMessage(STAXUtils.createXMLStreamReader(in, encoding), urlString); } public PostMethod getMethod() { return this.postMethod; } public void close() throws XFireException { postMethod.releaseConnection(); } /** * @return Returns the url. */ public String getUrl() { return urlString; } /** * @param url * The url to set. */ public void setUrl(String url) { this.urlString = url; } /** * @return Returns the password. */ public String getPassword() { return password; } /** * @param password * The password to set. */ public void setPassword(String password) { this.password = password; } /** * @return Returns the username. */ public String getUsername() { return username; } /** * @param username * The username to set. */ public void setUsername(String username) { this.username = username; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } }