数週間前、私は REST API の代わりに SOAP API を使用して Salesforce データを読み取ることについて質問しました ( Trying to use Apache Gobblin to read Salesforce data using SOAP API(s) instead of REST API を参照)。それなので、私はソリューションを(少し助けて)直接実装しようとしています。
REST API を使用して、(REST API を呼び出して) テーブルの定義を読み取る既存のコードは次のようになります。
// the URL starts with the Salesforce REST API endpoint, and ends with "/describe"
public String getSchema(String url, String accessToken, HttpClient httpClient) {
String jsonStr;
HttpRequestBase httpRequest = new HttpGet(url);
if (accessToken != null) {
httpRequest.addHeader("Authorization", "OAuth " + this.accessToken);
}
httpRequest.addHeader("Content-Type", "application/json");
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpRequest);
StatusLine status = httpResponse.getStatusLine();
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
jsonStr = EntityUtils.toString(httpEntity);
}
if (status.getStatusCode() >= 400) {
System.out.println("There was an error. Http Status code of " + status.getStatusCode());
EntityUtils.consumeEntity(httpEntity);
return null;
}
return jsonStr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return jsonStr;
}
次の不完全なコードのような Salesforce SOAP API (生成された「partner.wsdl」ファイルを使用) を使用するメソッドを作成したいと考えています。
public String getSchemaViaSoap(String tableName) {
String jsonStr;
PartnerConnection partnerConnection = ...;
try {
DescribeSObjectResult[] dsrArray = partnerConnection.describeSObjects(new String[] { entity });
// Since we described only one sObject, we should have only
// one element in the DescribeSObjectResult array.
DescribeSObjectResult dsr = dsrArray[0];
String jsonStr = ...; /* this is where I need help in converting dsr into a similar JSON string. */
} catch (ConnectionException e) {
e.printStackTrace();
log.error("Error getting connection", e);
System.out.println("Error getting connection" + e);
return null;
}
return jsonStr;
}
DescribeSObjectResult オブジェクトから同様の JsonString / HttpEntity / StringEntity オブジェクトに変換する方法を決定する際のあらゆる種類のヘルプをいただければ幸いです。