自定义开发IDI连接器,实现POST数据到HTTP URL

本文介绍如何通过Apache HTTP客户端库创建自定义IBM Data Integration Designer (IDI) 连接器,以支持POST请求并发送类似FORM的数据。该连接器能够设置HTTP头部参数,并处理HTTP响应。
摘要由CSDN通过智能技术生成

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")));
    }
    
  }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值