org.fax4j.bridge.AbstractContextFaxBridge Maven / Gradle / Ivy
package org.fax4j.bridge;
import java.util.Map;
import org.fax4j.FaxException;
import org.fax4j.FaxJob;
import org.fax4j.util.ReflectionHelper;
/**
* This provides almost complete implementation of the context fax bridge.
*
* Below table describes the configuration values relevant for this class.
* Configuration:
*
*
* Name
* Description
* Preconfigured Value
* Default Value
* Mandatory
*
*
* org.fax4j.bridge.vendor.policy.class.name
* The vendor policy class name.
* org.fax4j.bridge.EmptyVendorPolicy
* org.fax4j.bridge.EmptyVendorPolicy
* false
*
*
*
* @param
* The context type
* @author Sagie Gur-Ari
* @version 1.02
* @since 0.41.4
*/
public abstract class AbstractContextFaxBridge extends FaxBridgeImpl implements ContextFaxBridge
{
/**The request parser*/
private RequestParser requestParser;
/**
* This is the class constructor.
*/
public AbstractContextFaxBridge()
{
super();
}
/**
* This function initializes the fax bridge.
*/
@Override
protected void initializeImpl()
{
//create parser
this.requestParser=this.createRequestParser();
if(this.requestParser==null)
{
throw new FaxException("Unable to create request parser.");
}
}
/**
* This function will submit a new fax job.
*
* @param inputData
* The input data holding the fax job information
* @return The submitted fax job
*/
public FaxJob submitFaxJob(T inputData)
{
if(inputData==null)
{
throw new FaxException("Input data not provided.");
}
//create fax job
FaxJob faxJob=this.createFaxJob();
//update fax job info
this.requestParser.updateFaxJobFromInputData(inputData,faxJob);
//create file info
FileInfo fileInfo=this.requestParser.getFileInfoFromInputData(inputData);
if(fileInfo==null)
{
throw new FaxException("Unable to extract file info from input data.");
}
//submit fax job
this.submitFaxJob(faxJob,fileInfo);
return faxJob;
}
/**
* This function creates, initializes and returns a new request parser.
*
* @return The new request parser
*/
protected RequestParser createRequestParser()
{
//get parser configuration
String configurationKey=this.getRequestParserConfigurationKey();
String defaultParserClassName=this.getDefaultParserClassName();
//create parser
RequestParser parser=this.createRequestParser(configurationKey,defaultParserClassName);
return parser;
}
/**
* This function creates, initializes and returns a new request parser.
*
* @param configurationKey
* The configuration key used to fetch the parser class name
* @param defaultParserClassName
* The default parser class name
* @return The new request parser
*/
@SuppressWarnings("unchecked")
protected RequestParser createRequestParser(String configurationKey,String defaultParserClassName)
{
if(configurationKey==null)
{
throw new FaxException("Configuration key for request parser not provided.");
}
//get class name
String className=this.getConfigurationValue(configurationKey);
//if no class name configured, use default
if(className==null)
{
className=defaultParserClassName;
if(className==null)
{
throw new FaxException("Request parser class name not found.");
}
}
//create new instance
RequestParser parser=(RequestParser)ReflectionHelper.createInstance(className);
//initialize
Map configuration=this.getConfiguration();
parser.initialize(configuration);
return parser;
}
/**
* This function returns the key used to fetch the request parser
* class name from the fax4j.properties.
*
* @return The request parser class name configuration key
*/
protected abstract String getRequestParserConfigurationKey();
/**
* This function returns the default request parser class name.
*
* @return The default request parser class name
*/
protected abstract String getDefaultParserClassName();
}