org.fax4j.spi.http.ApacheHTTPClient Maven / Gradle / Ivy
package org.fax4j.spi.http;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.fax4j.FaxException;
import org.fax4j.FaxJob;
import org.fax4j.common.Logger;
import org.fax4j.spi.FaxClientSpi;
/**
* This HTTP client is based on the Apache HTTP client and is used
* to submit HTTP requests.
*
* @author Sagie Gur-Ari
* @version 1.10
* @since 0.1
*/
public class ApacheHTTPClient extends AbstractHTTPClient
{
/**
* This is the default constructor.
*/
public ApacheHTTPClient()
{
super();
}
/**
* This function creates and returns a new HTTP client.
*
* @return The new HTTP client
*/
protected HttpClient createHttpClient()
{
//create HTTP client
HttpClient httpClient=new HttpClient();
return httpClient;
}
/**
* This function creates and returns a new HTTP method.
*
* @param url
* The target URL
* @return The new HTTP method
*/
protected PostMethod createMethod(String url)
{
//create method
PostMethod httpPost=new PostMethod(url);
return httpPost;
}
/**
* Submits the HTTP request and returns the HTTP response.
*
* @param faxClientSpi
* The fax client SPI
* @param faxJob
* The fax job object
* @param httpRequest
* The HTTP request to send
* @param httpClientConfiguration
* HTTP client configuration
* @return The HTTP response
*/
public HTTPResponse submitHTTPRequest(FaxClientSpi faxClientSpi,FaxJob faxJob,HTTPRequest httpRequest,HTTPClientConfiguration httpClientConfiguration)
{
//get configuration
CommonHTTPClientConfiguration configuration=(CommonHTTPClientConfiguration)httpClientConfiguration;
//create HTTP client
HttpClient httpClient=this.createHttpClient();
//create URL
StringBuilder buffer=new StringBuilder();
buffer.append("http");
if(configuration.isSSL())
{
buffer.append("s");
}
buffer.append("://");
buffer.append(configuration.getHostName());
int port=configuration.getPort();
if(port!=-1)
{
buffer.append(":");
buffer.append(port);
}
String resource=httpRequest.getResource();
if((resource!=null)&&(resource.length()>0))
{
if(!resource.startsWith("/"))
{
buffer.append("/");
}
buffer.append(resource);
}
String parameters=httpRequest.getParametersText();
if((parameters!=null)&&(parameters.length()>0))
{
if((resource!=null)&&(resource.endsWith("/")))
{
int length=buffer.length();
buffer.delete(length-1,length);
}
if(!parameters.startsWith("?"))
{
buffer.append("?");
}
try
{
String[] parameterPairs=parameters.split("&");
int amount=parameterPairs.length;
String parameterPair=null;
String[] values=null;
boolean addedParameters=false;
for(int index=0;index> iterator=headerProperties.entrySet().iterator();
Entry