All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 entry=null;
			while(iterator.hasNext())
			{
				//get next entry
				entry=iterator.next();
				
				//set header values
				httpPost.addRequestHeader((String)entry.getKey(),(String)entry.getValue());
			}
		}

		//set content
		byte[] content=httpRequest.getContent();
		if(content!=null)
		{
			ByteArrayRequestEntity requestEntity=new ByteArrayRequestEntity(content,"binary/octet-stream");
	        httpPost.setRequestEntity(requestEntity);
            httpPost.setContentChunked(false);
		}

        //get logger
        Logger logger=faxClientSpi.getLogger();
        
        try
		{
            logger.logDebug(new Object[]{"Submitting HTTP request: ",httpPost.getURI()},null);
		}
		catch(URIException exception)
		{
            logger.logDebug(new Object[]{"Submitting HTTP request"},null);
		}

		byte[] responseContent=null;
		int statusCode=-1;
		try
		{
			//submit HTTP request
			statusCode=httpClient.executeMethod(httpPost);

			if(statusCode>=400)
			{
				throw new FaxException("Error while invoking HTTP request, return status code: "+statusCode);
			}
			
			//get response content
			responseContent=httpPost.getResponseBody();
		}
		catch(FaxException exception)
		{
			throw exception;
		}
		catch(Exception exception)
		{
			throw new FaxException("Error while executing HTTP request.",exception);
		}
		finally
		{
			//release connection
			httpPost.releaseConnection();
		}

		//create HTTP response
		HTTPResponse httpResponse=new HTTPResponse();
		httpResponse.setStatusCode(statusCode);
		httpResponse.setContent(responseContent);

		return httpResponse;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy