プログラマ38の日記

主にプログラムメモです。

Java: axis2でSalesforce APIを使用する

まずはサンプルコード

    String url = "https://login.salesforce.com/services/Soap/u/39.0";   
SforceServiceStub soapBinding = new SforceServiceStub(url);


Options options = soapBinding._getServiceClient().getOptions();
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP, Boolean.TRUE);
options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);

//httpプロキシを指定する
{
HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
proxyProperties.setProxyName("samplehost");
proxyProperties.setProxyPort(9999);
proxyProperties.setUserName("sampleproxyuser");
proxyProperties.setPassWord("sampleproxypasword");
options.setProperty(HTTPConstants.PROXY, proxyProperties);
}


LoginDocument logininfo = LoginDocument.Factory.newInstance();
Login login = Login.Factory.newInstance();
login.setUsername("sampleuser@sample.username");
login.setPassword("samplepassword");
logininfo.setLogin(login);


LoginResult loginResult = soapBinding.login(logininfo, null,null).getLoginResponse().getResult();

options.setTo(new EndpointReference(loginResult.getServerUrl()));

SessionHeaderDocument sh = SessionHeaderDocument.Factory.newInstance();

SessionHeader sheader = SessionHeaderDocument.SessionHeader.Factory.newInstance();
sheader.setSessionId(loginResult.getSessionId());
sh.setSessionHeader(sheader);

GetServerTimestampDocument getServerTimestampDocument = GetServerTimestampDocument.Factory.newInstance();
getServerTimestampDocument.setGetServerTimestamp(GetServerTimestamp.Factory.newInstance());
System.out.println(soapBinding.getServerTimestamp(getServerTimestampDocument, sh, null).getGetServerTimestampResponse().getResult().getTimestamp());

コードの生成とライブラリの作成

apache2のサイト(Apache Axis2 – Apache Axis2/Java - Next Generation Web Services

)からライブラリをダウンロード

ダウンロードしたaxis2のライブラリにクラスパスを指定しコマンドを実行する。

set classpath=.;..\lib\*

java -cp %classpath% org.apache.axis2.wsdl.WSDL2Java -Euwc -Ejavaversion 1.5 -d xmlbeans -uri partner.wsdl
java -cp %classpath% org.apache.axis2.wsdl.WSDL2Java -Euwc -Ejavaversion 1.5 -d xmlbeans -uri metadata.wsdl

コマンドを実行するとJavaコードと共にresourcesのフォルダにxsbが出力されます。

resourcesフォルダのファイルは実行時に必要なので、Javaコードをコンパイルした後、コンパイルしたクラスとresourcesフォルダのファイルを含めてライブラリを作成します。

httpproxyの設定

httpproxyはプログラム内部の指定となります。

          HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
proxyProperties.setProxyName("samplehost");
proxyProperties.setProxyPort(9999);
proxyProperties.setUserName("sampleproxyuser");
proxyProperties.setPassWord("sampleproxypasword");
options.setProperty(HTTPConstants.PROXY, proxyProperties);

エンドポイントの設定

コンストラクタで受けてくれるタイプです。

    String url = "https://login.salesforce.com/services/Soap/u/39.0";    
SforceServiceStub soapBinding = new SforceServiceStub(url);

セッションIDと、インスタンスURLのセット

optionsで、エンドポイントを指定しますが、SoapHeaderにセッションヘッダをセットはしません。セッションヘッダは実行時に毎回引数で指定することになります。

        options.setTo(new EndpointReference(loginResult.getServerUrl()));

SessionHeaderDocument sh = SessionHeaderDocument.Factory.newInstance();

SessionHeader sheader = SessionHeaderDocument.SessionHeader.Factory.newInstance();
sheader.setSessionId(loginResult.getSessionId());
sh.setSessionHeader(sheader);

 

これで上記コードのsoapBindingはSOAP APIを使うことができます。サンプルコードの最後に、Salesforceのサーバ時刻を取得するAPIをコールしています。

サーバ時刻を取得するAPIをコールするだけで、このコード量ですから、アプリケーションで書く場合のコード量はとても多くなります。