Evidently the list dislikes attachments.  Here's the code that goes with my previous 
post:

import java.util.Properties;
import javax.servlet.http.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.logging.Log;

public class XmlHTTPClient {
    private static Log log = Logger.getLog(XmlHTTPClient.class);
    private Properties props = null;
    private String strURL = null;
    private int timeout = 300000; //five minute default
    private String userName = null;
    private String password = null;
    private boolean authenticationRequired;
    private boolean handleAuthentication;
    private HttpClient httpClient = null;
    private int pooledConnections;
    
    public XmlHTTPClient(Properties props) {    
        this.props = props;
    }

    public synchronized void start() throws AIException { 
        Logger.debug(log, "start begin");
        parseProps();
        MultiThreadedHttpConnectionManager mhcm = new 
MultiThreadedHttpConnectionManager();
        mhcm.setMaxConnectionsPerHost(pooledConnections);
        httpClient = new HttpClient(mhcm);
        httpClient.setTimeout(timeout);
        if (authenticationRequired) {
            httpClient.getState().setCredentials(
                null,
                new UsernamePasswordCredentials(userName, password)
            );
            httpClient.getState().setAuthenticationPreemptive(true);
        }
        Logger.debug(log, "started with the following parameters: " + "\n" +
                          "strURL " + strURL + " \n" +
                          "timeout " + timeout + " \n" +
                          "userName " + userName + " \n" +
                          "password " + password + " \n" +
                          "handleAuthentication " + handleAuthentication
                          );
    }
    
    public String sendMessage(String requestBody) throws AIException {
        Logger.debug(log, "sending request");
        String xmlResponse = null;
        PostMethod httppost = null;
        try {
            // create the postmethod using the url passed into constructor
            httppost = new PostMethod(strURL);
            
            // Tell the post method to automatically handle authentication. The
            // method will use any appropriate credentials to handle basic
            // authentication requests.  Setting this value to false will cause
            // any request for authentication to return with a status of 401.
            // It will then be up to the client to handle the authentication.
            if (authenticationRequired) {
                 httppost.setDoAuthentication(handleAuthentication);
            }
           
            // now set the body and headers and then execute the post
            httppost.setRequestBody(requestBody);
            httppost.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO);
            httppost.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
            httpClient.executeMethod(httppost); 
            
            // check the response code and throw and aiException if it's anything
            // other than a success code
            if (httppost.getStatusCode() == HttpServletResponse.SC_OK) {
                xmlResponse = httppost.getResponseBodyAsString();
            } else {
                Logger.error(log, "error status code: " + httppost.getStatusCode());
                throw new AIException("error status code: " + 
httppost.getStatusCode());
            } 
            
        } catch (java.io.IOException ie) {
            Logger.error(log, "Unable to execute post: " + ie.toString());
            throw new AIException("Unable to execute post: " + ie.toString());
        } finally {
            httppost.releaseConnection();    
        }    
        Logger.debug(log, "received response");
        return xmlResponse;
    }
    
    
    public void stop() {
        // httppost.releaseConnection cleans up resources following each post, so ...
        // nothing to clean up here.
    }
    
    private synchronized void parseProps() throws AIException {
        if (props != null) {
            this.userName = props.getProperty("user");
            this.password = props.getProperty("password");
            if (userName != null && password != null) {
                String handleAuthenticationStr = props.getProperty("authenticate");
                if (handleAuthenticationStr == null) {
                    Logger.error(log,"authenticate parameter is null");
                    throw new AIException("authenticate parameter is null");
                } else {
                    this.authenticationRequired = true;
                    this.handleAuthentication = (new 
Boolean(handleAuthenticationStr)).booleanValue();
                }    
            }
            
            try {
                timeout = Integer.parseInt(props.getProperty("timeout"));;    
            } catch(NumberFormatException ne) {
                Logger.error(log,"timeout property is not a valid int");
                throw new AIException("timeout property is not a valid int");
            }
            
            this.strURL = props.getProperty("url");
            if (strURL == null || strURL.equals("")) {
                Logger.error(log,"url is either null or an empty string");
                throw new AIException("url is either null or an empty string");
            }
            
            try {
                pooledConnections = 
Integer.parseInt(props.getProperty("connections"));;    
            } catch(NumberFormatException ne) {
                Logger.error(log,"connections property is not a valid int");
                throw new AIException("connections property is not a valid int");
            }
        }    
    }
}

Reply via email to