axis2 Connecting to the Web Service API Using Java and Axis 1.4

來源:互聯網
上載者:User

This document contains conceptual and procedural information on connecting your development environment or other systems to the ExactTarget SOAP web service API using
Java via the
Axis 1.4 SOAP client.

Why Connect to the Web Service API using
Java and Axis 1.4

You can use the connection to the SOAP web service API to test your calls and perform various tasks, such as sending email and retrieving tracking information.

How To Connect to the Web Service API using
Java and Axis 1.4

Download Apache
Axis 1.4 and follow the appropriate instructions at that site to install the service on your computer. You can use the sample code below to authenticate your installation and exchange information
with the SOAP web service API servers.

Code

The sample code below demonstrates how to connect and interact with the SOAP web service API.

Authenticating with Apache
Axis 1.4

This sample code demonstrates how to use
Java and Axis 1.4 to authenticate with the SOAP web service API.

Password Callback Handler

Place this sample code in the Class-path.file to define PasswordTokenHandler and what type of Password method the service is using. In this case, the service uses the
PasswordText method.

view plaincopy
to clipboardprint?
  1. public class PasswordTokenHandler implements CallbackHandler {   
  2.     public void handle(Callback[] callbacks) throws IOException,   
  3.         UnsupportedCallbackException {   
  4.     for (int i = 0; i < callbacks.length; i++) {   
  5.         if (callbacks[i] instanceof WSPasswordCallback) {   
  6.             WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];   
  7.             pc.setPassword("ET Password");   
  8.         } else {   
  9.             throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");   
  10.             }   
  11.         }   
  12.     }   
  13. }  
public class PasswordTokenHandler implements CallbackHandler {    public void handle(Callback[] callbacks) throws IOException,        UnsupportedCallbackException {    for (int i = 0; i < callbacks.length; i++) {        if (callbacks[i] instanceof WSPasswordCallback) {            WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];            pc.setPassword("ET Password");        } else {            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");            }        }    }}

The sample code below demonstrates how to define a callback handler.

view plaincopy
to clipboardprint?
  1. <deployment   
  2.         name="commonsHTTPConfig"  
  3.         xmlns="http://xml.apache.org/axis/wsdd/"  
  4.         xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">   
  5.     <!-- <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/> -->   
  6.     <globalConfiguration>   
  7.         <requestFlow>   
  8.             <handler type="java:org.apache.ws.axis.security.WSDoAllSender">   
  9.                 <parameter name="action" value="UsernameToken"/>   
  10.                 <parameter name="passwordCallbackClass" value="com.et.util.PasswordTokenHandler"/>   
  11.                 <parameter name="passwordType" value="PasswordText"/>   
  12.             </handler>   
  13.         </requestFlow>   
  14.     </globalConfiguration>   
  15.     <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"></transport>   
  16.   
  17.    <!-- <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"/>   
  18.     <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender"/>   
  19.     <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender"/>-->   
  20. </deployment>  
<deployment        name="commonsHTTPConfig"        xmlns="http://xml.apache.org/axis/wsdd/"        xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">    <!-- <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/> -->    <globalConfiguration>        <requestFlow>            <handler type="java:org.apache.ws.axis.security.WSDoAllSender">                <parameter name="action" value="UsernameToken"/>                <parameter name="passwordCallbackClass" value="com.et.util.PasswordTokenHandler"/>                <parameter name="passwordType" value="PasswordText"/>            </handler>        </requestFlow>    </globalConfiguration>    <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"></transport>   <!-- <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"/>    <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender"/>    <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender"/>--></deployment>

The sample code below accesses the SOAP client and assigns the username and password in the request.

view plaincopy
to clipboardprint?
  1. public Soap_PortType init() {   
  2.     Soap_PortType stub = null;   
  3.     try {   
  4.         InputStream inConfig = BaseTestCase.class.getClassLoader().getResourceAsStream("axis_client_config.xml");   
  5.         EngineConfiguration config = new FileProvider(inConfig);   
  6.         PartnerAPILocator locator = new PartnerAPILocator(config);   
  7.         inConfig.close();   
  8.   
  9.         stub = locator.getSoap();   
  10.            
  11.         Stub axisPort = (Stub) stub;   
  12.         axisPort._setProperty(UsernameToken.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);   
  13.         axisPort._setProperty(WSHandlerConstants.USER, "ET USERNAME");   
  14.         axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF, new PasswordTokenHandler());               
  15.     }   
  16.     catch (Exception e) {   
  17.         e.printStackTrace();   
  18.     }   
  19.     return stub;   
  20. }  
public Soap_PortType init() {    Soap_PortType stub = null;    try {        InputStream inConfig = BaseTestCase.class.getClassLoader().getResourceAsStream("axis_client_config.xml");        EngineConfiguration config = new FileProvider(inConfig);        PartnerAPILocator locator = new PartnerAPILocator(config);        inConfig.close();        stub = locator.getSoap();                Stub axisPort = (Stub) stub;        axisPort._setProperty(UsernameToken.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);        axisPort._setProperty(WSHandlerConstants.USER, "ET USERNAME");        axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF, new PasswordTokenHandler());                }    catch (Exception e) {        e.printStackTrace();    }    return stub;}

Content of
Axis Client config.xml Fileview plaincopy
to clipboardprint?
  1. <deployment   
  2.         name="commonsHTTPConfig"  
  3.         xmlns="http://xml.apache.org/axis/wsdd/"  
  4.         xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">   
  5.     <globalConfiguration>   
  6.         <requestFlow>   
  7.             <handler type="java:org.apache.ws.axis.security.WSDoAllSender">   
  8.                 <parameter name="action" value="UsernameToken"/>   
  9.                 <parameter name="passwordCallbackClass" value="com.et.util.PasswordTokenHandler"/>   
  10.                 <parameter name="passwordType" value="PasswordText"/>   
  11.             </handler>   
  12.         </requestFlow>   
  13.     </globalConfiguration>   
  14.     <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"></transport>   
  15.  </deployment>  
<deployment        name="commonsHTTPConfig"        xmlns="http://xml.apache.org/axis/wsdd/"        xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">    <globalConfiguration>        <requestFlow>            <handler type="java:org.apache.ws.axis.security.WSDoAllSender">                <parameter name="action" value="UsernameToken"/>                <parameter name="passwordCallbackClass" value="com.et.util.PasswordTokenHandler"/>                <parameter name="passwordType" value="PasswordText"/>            </handler>        </requestFlow>    </globalConfiguration>    <transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender"></transport> </deployment>

Additional Data

Passing credentials using WS-Security headersby Adobe
There are no ratings yet. Be the first to rate this article.Modified

15 October 2007

    • Comments
      (2)
    Page tools
    Share
    on FacebookShare
    on TwitterShare
    on LinkedInPrintLiveCyclesecurityweb services
    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.