retrieve()
このコールは、組織内の XML ファイル表現のコンポーネントを取得します。
構文
1AsyncResult = metadatabinding.retrieve(RetrieveRequest retrieveRequest)使用方法
組織内のファイル表現のコンポーネントを取得するには、このコールを使用します。
API バージョン 31.0 以降では、retrieve() コールを実行するプロセスが簡略化されました。取得操作の状況を取得する場合、retrieve() をコールしてから checkStatus() をコールする必要がなくなりました。代わりに、checkRetrieveStatus() のみをコールします。取得操作が進行中の場合、取得操作が完了するまで checkRetrieveStatus() を再コールします。checkStatus() コールは、API バージョン 30.0 以前では引き続きサポートされますが、API バージョン 31.0 以降では使用できません。
API バージョン 31.0 以降の場合、次の手順に従って、パッケージ化されたコンポーネントまたはパッケージ化されていないコンポーネントを取得します。
- retrieve() コールを発行し、非同期的な取得を開始すると、AsyncResult オブジェクトが返されます。id 項目の値をメモし、次のステップで使用します。
- checkRetrieveStatus() コールを発行して、最初のステップの AsyncResult オブジェクトから id 値を渡します。返された RetrieveResult の done 項目の値をチェックします。true の場合、コールが完了して、次のステップに進むことを意味します。それ以外の場合は、done 項目が true になるまで、このステップを繰り返して checkRetrieveStatus() を再度コールします。
- 前のステップの checkRetrieveStatus() への最後のコールで返された RetrieveResult から zip ファイル (zipFile 項目) および他の必要な項目を取得します。
API バージョン 30.0 以前の場合、次の手順に従って、パッケージ化されたコンポーネントまたはパッケージ化されていないコンポーネントを取得します。
- retrieve() コールを発行し、非同期的な取得を開始すると、AsyncResult オブジェクトが返されます。コールが完了すると、done 項目に true が含まれます。ほとんどの場合、コールはすぐに完了しないため、結果に記述されません。完了している場合、返された id 項目の値を書き留め、次のステップを省略します。
- コールが完了していない場合、前のステップで retrieve() コールから返された AsyncResult オブジェクトの id 項目の値を使用して、ループで checkStatus() コールを発行します。done 項目に true が含まれるまで、返される AsyncResult オブジェクトを確認します。retrieve() コールを完了するまでにかかる時間は、リリースされる zip ファイルのサイズによって異なるため、zip ファイルのサイズが大きいほど、反復中の待機時間をより長く設定します。
- 最初のステップで返された id 値を使用して、checkRetrieveStatus() コールを発行し、retrieve() コールの結果を取得します。
マニフェストファイルの例は、「package.xml マニフェストファイルのサンプル」を参照してください。
引数
| 名前 | 型 | 説明 |
|---|---|---|
| retrieveRequest | RetrieveRequest | 取得するパッケージまたはファイルを決定するためのオプションをカプセル化します。 |
応答
サンプルコード —Java
このサンプルでは、コンポーネントを zip ファイルにして取得する方法を示します。zip ファイルのリリース方法の詳細は、deploy() のサンプルコードを参照してください。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.doc.samples;
18
19import java.io.*;
20import java.util.*;
21import java.nio.ByteBuffer;
22import java.nio.channels.*;
23import java.rmi.RemoteException;
24import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.DocumentBuilderFactory;
26import javax.xml.parsers.ParserConfigurationException;
27import org.w3c.dom.Element;
28import org.w3c.dom.Node;
29import org.w3c.dom.NodeList;
30import org.xml.sax.SAXException;
31
32import com.sforce.soap.metadata.AsyncResult;
33import com.sforce.soap.metadata.MetadataConnection;
34import com.sforce.soap.enterprise.EnterpriseConnection;
35import com.sforce.soap.metadata.RetrieveMessage;
36import com.sforce.soap.metadata.RetrieveRequest;
37import com.sforce.soap.metadata.RetrieveResult;
38import com.sforce.soap.metadata.RetrieveStatus;
39import com.sforce.soap.enterprise.LoginResult;
40import com.sforce.ws.ConnectionException;
41import com.sforce.ws.ConnectorConfig;
42import com.sforce.soap.metadata.PackageTypeMembers;
43
44public class RetrieveSample {
45
46 // Binding for the metadata WSDL used for making metadata API calls
47 private MetadataConnection metadataConnection;
48
49 static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
50
51 // one second in milliseconds
52 private static final long ONE_SECOND = 1000;
53 // maximum number of attempts to retrieve the results
54 private static final int MAX_NUM_POLL_REQUESTS = 50;
55
56 // manifest file that controls which components get retrieved
57 private static final String MANIFEST_FILE = "package.xml";
58
59 private static final double API_VERSION = 31.0;
60
61 public static void main(String[] args) throws Exception {
62 final String USERNAME = "user@company.com";
63 // This is only a sample. Hard coding passwords in source files is a bad practice.
64 final String PASSWORD = "password";
65 final String URL = "https://login.salesforce.com/services/Soap/c/31.0";
66
67 RetrieveSample sample = new RetrieveSample(USERNAME, PASSWORD, URL);
68 sample.retrieveZip();
69 }
70
71 public RetrieveSample(String username, String password, String loginUrl)
72 throws ConnectionException {
73 createMetadataConnection(username, password, loginUrl);
74 }
75
76
77 private void retrieveZip() throws RemoteException, Exception
78 {
79 RetrieveRequest retrieveRequest = new RetrieveRequest();
80 // The version in package.xml overrides the version in RetrieveRequest
81 retrieveRequest.setApiVersion(API_VERSION);
82 setUnpackaged(retrieveRequest);
83
84 // Start the retrieve operation
85 AsyncResult asyncResult = metadataConnection.retrieve(retrieveRequest);
86 String asyncResultId = asyncResult.getId();
87
88 // Wait for the retrieve to complete
89 int poll = 0;
90 long waitTimeMilliSecs = ONE_SECOND;
91 RetrieveResult result = null;
92 do {
93 Thread.sleep(waitTimeMilliSecs);
94 // Double the wait time for the next iteration
95 waitTimeMilliSecs *= 2;
96 if (poll++ > MAX_NUM_POLL_REQUESTS) {
97 throw new Exception("Request timed out. If this is a large set " +
98 "of metadata components, check that the time allowed " +
99 "by MAX_NUM_POLL_REQUESTS is sufficient.");
100 }
101 result = metadataConnection.checkRetrieveStatus(
102 asyncResultId);
103 System.out.println("Retrieve Status: " + result.getStatus());
104 } while (!result.isDone());
105
106 if (result.getStatus() == RetrieveStatus.Failed) {
107 throw new Exception(result.getErrorStatusCode() + " msg: " +
108 result.getErrorMessage());
109 } else if (result.getStatus() == RetrieveStatus.Succeeded) {
110 // Print out any warning messages
111 StringBuilder buf = new StringBuilder();
112 if (result.getMessages() != null) {
113 for (RetrieveMessage rm : result.getMessages()) {
114 buf.append(rm.getFileName() + " - " + rm.getProblem());
115 }
116 }
117 if (buf.length() > 0) {
118 System.out.println("Retrieve warnings:\n" + buf);
119 }
120
121 // Write the zip to the file system
122 System.out.println("Writing results to zip file");
123 ByteArrayInputStream bais = new ByteArrayInputStream(result.getZipFile());
124 File resultsFile = new File("retrieveResults.zip");
125 FileOutputStream os = new FileOutputStream(resultsFile);
126 try {
127 ReadableByteChannel src = Channels.newChannel(bais);
128 FileChannel dest = os.getChannel();
129 copy(src, dest);
130
131 System.out.println("Results written to " + resultsFile.getAbsolutePath());
132 } finally {
133 os.close();
134 }
135 }
136 }
137
138 /**
139 * Helper method to copy from a readable channel to a writable channel,
140 * using an in-memory buffer.
141 */
142 private void copy(ReadableByteChannel src, WritableByteChannel dest)
143 throws IOException
144 {
145 // Use an in-memory byte buffer
146 ByteBuffer buffer = ByteBuffer.allocate(8092);
147 while (src.read(buffer) != -1) {
148 buffer.flip();
149 while(buffer.hasRemaining()) {
150 dest.write(buffer);
151 }
152 buffer.clear();
153 }
154 }
155
156 private void setUnpackaged(RetrieveRequest request) throws Exception
157 {
158 // Edit the path, if necessary, if your package.xml file is located elsewhere
159 File unpackedManifest = new File(MANIFEST_FILE);
160 System.out.println("Manifest file: " + unpackedManifest.getAbsolutePath());
161
162 if (!unpackedManifest.exists() || !unpackedManifest.isFile())
163 throw new Exception("Should provide a valid retrieve manifest " +
164 "for unpackaged content. " +
165 "Looking for " + unpackedManifest.getAbsolutePath());
166
167 // Note that we populate the _package object by parsing a manifest file here.
168 // You could populate the _package based on any source for your
169 // particular application.
170 com.sforce.soap.metadata.Package p = parsePackage(unpackedManifest);
171 request.setUnpackaged(p);
172 }
173
174 private com.sforce.soap.metadata.Package parsePackage(File file) throws Exception {
175 try {
176 InputStream is = new FileInputStream(file);
177 List<PackageTypeMembers> pd = new ArrayList<PackageTypeMembers>();
178 DocumentBuilder db =
179 DocumentBuilderFactory.newInstance().newDocumentBuilder();
180 Element d = db.parse(is).getDocumentElement();
181 for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling()) {
182 if (c instanceof Element) {
183 Element ce = (Element)c;
184 //
185 NodeList namee = ce.getElementsByTagName("name");
186 if (namee.getLength() == 0) {
187 // not
188 continue;
189 }
190 String name = namee.item(0).getTextContent();
191 NodeList m = ce.getElementsByTagName("members");
192 List<String> members = new ArrayList<String>();
193 for (int i = 0; i < m.getLength(); i++) {
194 Node mm = m.item(i);
195 members.add(mm.getTextContent());
196 }
197 PackageTypeMembers pdi = new PackageTypeMembers();
198 pdi.setName(name);
199 pdi.setMembers(members.toArray(new String[members.size()]));
200 pd.add(pdi);
201 }
202 }
203 com.sforce.soap.metadata.Package r = new com.sforce.soap.metadata.Package();
204 r.setTypes(pd.toArray(new PackageTypeMembers[pd.size()]));
205 r.setVersion(API_VERSION + "");
206 return r;
207 } catch (ParserConfigurationException pce) {
208 throw new Exception("Cannot create XML parser", pce);
209 } catch (IOException ioe) {
210 throw new Exception(ioe);
211 } catch (SAXException se) {
212 throw new Exception(se);
213 }
214 }
215
216
217 private void createMetadataConnection(final String username,
218 final String password, final String loginUrl)
219 throws ConnectionException {
220
221 final ConnectorConfig loginConfig = new ConnectorConfig();
222 loginConfig.setAuthEndpoint(loginUrl);
223 loginConfig.setServiceEndpoint(loginUrl);
224 loginConfig.setManualLogin(true);
225 LoginResult loginResult = (new EnterpriseConnection(loginConfig)).login(
226 username, password);
227
228 final ConnectorConfig metadataConfig = new ConnectorConfig();
229 metadataConfig.setServiceEndpoint(loginResult.getMetadataServerUrl());
230 metadataConfig.setSessionId(loginResult.getSessionId());
231 this.metadataConnection = new MetadataConnection(metadataConfig);
232 }
233
234 //The sample client application retrieves the user's login credentials.
235 // Helper function for retrieving user input from the console
236 String getUserInput(String prompt) {
237 System.out.print(prompt);
238 try {
239 return rdr.readLine();
240 }
241 catch (IOException ex) {
242 return null;
243 }
244 }
245
246}
247