IDI6.1.1提供的连接器HTTPClientConnector和OldHTTPClientConnector都是把输出映射数据放到http头部。而有时需要把输出映射数据类似于FORM数据提交到后台。因此,开发自定义IDI连接器是必要的。
1。所需jar包commons-httpclient-3.1.jar,commons-codec-1.3.jar,commons-logging-1.0.4.jar,log4j-1.2.13.jar
2。开发一个连接器ApacheHTTPClientConnector
public class ApacheHTTPCLient extends Connector implements ConnectorInterface, CheckpointRestartInterface需要继承com.ibm.di.connector.Connector和实现两个接口。
private PostMethod method ;
private HttpClient client;
public ApacheHTTPCLient () {
setName (myName);
setModes( new String[]{
ConnectorConfig.ADDONLY_MODE,//仅增加
ConnectorConfig.ITERATOR_MODE,//迭代
ConnectorConfig.LOOKUP_MODE,//查询
ConnectorConfig.CALL_REPLY_MODE,//调用回复,需实现queryReply方法
} );
}
构造方法里可以设定连接器需要支持的模式。
public Entry queryReply (Entry entry) throws Exception{
this.initHttp(getParam("url"), false);
// System.out.println(entry.toString());
this.doOutput(entry);
// System.out.println(entry.toString());
return entry;
}
public void terminate () throws Exception {
method.releaseConnection();
super.terminate();
}
private void doOutput ( Entry e ) throws Exception {
// boolean didOutput = false;
// BufferedReader input = null;
// BufferedWriter output;
Entry tmp = new Entry();
// Set HTTP request headers
String[] names = e.getAttributeNames();
for ( int i = 0; i < names.length; i++ ) {
if ( names[i].startsWith ("http.") ) {
if ( names[i].equalsIgnoreCase("http.body") )
continue;
method.setParameter( names[i].substring(5), e.getString( names[i] ) );
}
tmp.setAttribute ( e.getAttribute(names[i]) );
}
names = e.getPropertyNames();
for ( int i = 0; i < names.length; i++ ) {
if ( names[i].equalsIgnoreCase("http.body") )
continue;
if ( names[i].startsWith ("http.") ) {
method.setParameter( names[i].substring(5), e.getProperty( names[i] ).toString() );
}
}
// System.out.println(method.getParameters());
// Create a buffered writer for the output stream
// NOTE! Once the outputstream has been obtained, the http object will no longer
// accept any changes to headers etc .
// output = new BufferedWriter ( new OutputStreamWriter ( method.getResponseBodyAsStream()) );
// // If we have a parser use that one
if ( hasParser() ) {
debug ("Initialize parser with internal output stream");
CharArrayWriter caw = new CharArrayWriter();
initParser (null, caw);
getParser().writeEntry ( tmp );
getParser().closeParser ( );
caw.close();
// caw.writeTo(null);
// caw.writeTo (method.get);
} else {
debug ( "Get http.body from attribute/property http.body");
// Get value object for the data attribute
Object data = e.getAttribute ("http.body");
if ( data != null )
data = ((Attribute)data).getValue(0);
// Try property if no attribute
if ( data == null )
data = e.getProperty ("http.body");
}
// Flush and close output writer
client.executeMethod(method);
Entry res = result();
e.merge ( res );
}
private Entry result () throws Exception {
Entry e = new Entry();
e.setAttribute ( "http.responseCode", new Integer (method.getStatusCode()) );
e.setAttribute ( "http.responseMsg", method.getResponseBodyAsString());
e.setAttribute ( "http.content-type", method.getResponseHeader("Content-Type"));
e.setAttribute ( "http.content-encoding", method.getResponseCharSet());
e.setAttribute ( "http.content-length", new Integer(String.valueOf(method.getResponseContentLength()) ));
e.setAttribute ( "http.body", method.getResponseBodyAsString());
if ( method.getResponseHeader("Content-Type").getValue().startsWith("text/") ) {
e.setAttribute ( "http.text-body", method.getResponseBodyAsString());
}
//http.connect ();
return e;
}
private void initHttp (String strURL, boolean output) throws Exception {
if ( strURL == null )
throw new com.ibm.di.exceptions.MissingConfigurationException ("ApacheHTTPCLient", "url");
debug ("connect to: " + strURL);
client = new HttpClient();
method = new PostMethod(strURL);
if ( hasConfigValue("username") ) {
method.setDoAuthentication(true);
method.setRequestHeader("Authorization",
"Basic " + b64Encode ( getParam("username") + ":" + getParam("password")));
}
}