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