/
DefaultRestClient.java
468 lines (371 loc) · 18.4 KB
/
DefaultRestClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.salesforce.internal.client;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.camel.component.salesforce.SalesforceHttpClient;
import org.apache.camel.component.salesforce.SalesforceLoginConfig;
import org.apache.camel.component.salesforce.api.SalesforceException;
import org.apache.camel.component.salesforce.api.utils.Version;
import org.apache.camel.component.salesforce.internal.SalesforceSession;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;
import org.eclipse.jetty.client.InputStreamRequestContent;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
public class DefaultRestClient extends AbstractClientBase implements RestClient {
private static final String SERVICES_DATA = "/services/data/";
private static final String TOKEN_HEADER = "Authorization";
private static final String TOKEN_PREFIX = "Bearer ";
private static final String SERVICES_APEXREST = "/services/apexrest/";
private static final String GET_SCHEMA_MINIMUM_VERSION = "40.0";
public DefaultRestClient(final SalesforceHttpClient httpClient, final String version,
final SalesforceSession session,
final SalesforceLoginConfig loginConfig) throws SalesforceException {
super(version, session, httpClient, loginConfig);
}
@Override
protected void doHttpRequest(Request request, ClientResponseCallback callback) {
// set standard headers for all requests
request.headers(h -> h.add(HttpHeader.ACCEPT, APPLICATION_JSON_UTF8));
request.headers(h -> h.add(HttpHeader.ACCEPT_CHARSET, StandardCharsets.UTF_8.name()));
// request content type and charset is set by the request entity
super.doHttpRequest(request, callback);
}
@Override
public void approval(final InputStream request, Map<String, List<String>> headers, final ResponseCallback callback) {
final Request post = getRequest(HttpMethod.POST, versionUrl() + "process/approvals/", headers);
// authorization
setAccessToken(post);
// input stream as entity content
post.body(new InputStreamRequestContent(request));
post.headers(h -> h.add(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_UTF8));
doHttpRequest(post, new DelegatingClientCallback(callback));
}
@Override
public void approvals(Map<String, List<String>> headers, final ResponseCallback callback) {
final Request get = getRequest(HttpMethod.GET, versionUrl() + "process/approvals/", headers);
// authorization
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getVersions(Map<String, List<String>> headers, final ResponseCallback callback) {
Request get = getRequest(HttpMethod.GET, servicesDataUrl(), headers);
// does not require authorization token
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getResources(Map<String, List<String>> headers, ResponseCallback callback) {
Request get = getRequest(HttpMethod.GET, versionUrl(), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getGlobalObjects(Map<String, List<String>> headers, ResponseCallback callback) {
Request get = getRequest(HttpMethod.GET, sobjectsUrl(""), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getBasicInfo(String sObjectName, Map<String, List<String>> headers, ResponseCallback callback) {
Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/"), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getDescription(String sObjectName, Map<String, List<String>> headers, ResponseCallback callback) {
Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/describe/"), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void getSObject(
String sObjectName, String id, String[] fields, Map<String, List<String>> headers, ResponseCallback callback) {
// parse fields if set
String params = "";
if (fields != null && fields.length > 0) {
StringBuilder fieldsValue = new StringBuilder("?fields=");
for (int i = 0; i < fields.length; i++) {
fieldsValue.append(fields[i]);
if (i < (fields.length - 1)) {
fieldsValue.append(',');
}
}
params = fieldsValue.toString();
}
Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" + id + params), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void createSObject(
String sObjectName, InputStream sObject, Map<String, List<String>> headers, ResponseCallback callback) {
// post the sObject
final Request post = getRequest(HttpMethod.POST, sobjectsUrl(sObjectName), headers);
// authorization
setAccessToken(post);
// input stream as entity content
post.body(new InputStreamRequestContent(sObject));
post.headers(h -> h.add(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_UTF8));
doHttpRequest(post, new DelegatingClientCallback(callback));
}
@Override
public void updateSObject(
String sObjectName, String id, InputStream sObject, Map<String, List<String>> headers, ResponseCallback callback) {
final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName + "/" + id), headers);
// requires authorization token
setAccessToken(patch);
// input stream as entity content
patch.body(new InputStreamRequestContent(sObject));
patch.headers(h -> h.add(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_UTF8));
doHttpRequest(patch, new DelegatingClientCallback(callback));
}
@Override
public void deleteSObject(String sObjectName, String id, Map<String, List<String>> headers, ResponseCallback callback) {
final Request delete = getRequest(HttpMethod.DELETE, sobjectsUrl(sObjectName + "/" + id), headers);
// requires authorization token
setAccessToken(delete);
doHttpRequest(delete, new DelegatingClientCallback(callback));
}
@Override
public void getSObjectWithId(
String sObjectName, String fieldName, String fieldValue, Map<String, List<String>> headers,
ResponseCallback callback) {
final Request get = getRequest(HttpMethod.GET, sobjectsExternalIdUrl(sObjectName, fieldName, fieldValue), headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void upsertSObject(
String sObjectName, String fieldName, String fieldValue, Map<String, List<String>> headers, InputStream sObject,
ResponseCallback callback) {
final Request patch = getRequest("PATCH", sobjectsExternalIdUrl(sObjectName, fieldName, fieldValue), headers);
// requires authorization token
setAccessToken(patch);
// input stream as entity content
patch.body(new InputStreamRequestContent(sObject));
// TODO will the encoding always be UTF-8??
patch.headers(h -> h.add(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_UTF8));
doHttpRequest(patch, new DelegatingClientCallback(callback));
}
@Override
public void deleteSObjectWithId(
String sObjectName, String fieldName, String fieldValue, Map<String, List<String>> headers,
ResponseCallback callback) {
final Request delete
= getRequest(HttpMethod.DELETE, sobjectsExternalIdUrl(sObjectName, fieldName, fieldValue), headers);
// requires authorization token
setAccessToken(delete);
doHttpRequest(delete, new DelegatingClientCallback(callback));
}
@Override
public void getBlobField(
String sObjectName, String id, String blobFieldName, Map<String, List<String>> headers, ResponseCallback callback) {
final Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" + id + "/" + blobFieldName), headers);
// TODO this doesn't seem to be required, the response is always the
// content binary stream
// get.header(HttpHeader.ACCEPT_ENCODING, "base64");
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void query(String soqlQuery, Map<String, List<String>> headers, ResponseCallback callback) {
try {
String encodedQuery = urlEncode(soqlQuery);
final Request get = getRequest(HttpMethod.GET, versionUrl() + "query/?q=" + encodedQuery, headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
} catch (UnsupportedEncodingException e) {
String msg = "Unexpected error: " + e.getMessage();
callback.onResponse(null, Collections.emptyMap(), new SalesforceException(msg, e));
}
}
@Override
public void queryMore(String nextRecordsUrl, Map<String, List<String>> headers, ResponseCallback callback) {
final Request get = getRequest(HttpMethod.GET, instanceUrl + nextRecordsUrl, headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
}
@Override
public void queryAll(String soqlQuery, Map<String, List<String>> headers, ResponseCallback callback) {
try {
String encodedQuery = urlEncode(soqlQuery);
final Request get = getRequest(HttpMethod.GET, versionUrl() + "queryAll/?q=" + encodedQuery, headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
} catch (UnsupportedEncodingException e) {
String msg = "Unexpected error: " + e.getMessage();
callback.onResponse(null, Collections.emptyMap(), new SalesforceException(msg, e));
}
}
@Override
public void search(String soslQuery, Map<String, List<String>> headers, ResponseCallback callback) {
try {
String encodedQuery = urlEncode(soslQuery);
final Request get = getRequest(HttpMethod.GET, versionUrl() + "search/?q=" + encodedQuery, headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(callback));
} catch (UnsupportedEncodingException e) {
String msg = "Unexpected error: " + e.getMessage();
callback.onResponse(null, Collections.emptyMap(), new SalesforceException(msg, e));
}
}
@Override
public void apexCall(
String httpMethod, String apexUrl, Map<String, Object> queryParams, InputStream requestDto,
Map<String, List<String>> headers, ResponseCallback callback) {
// create APEX call request
final Request request;
try {
request = getRequest(httpMethod, apexCallUrl(apexUrl, queryParams), headers);
// set request SObject and content type
if (requestDto != null) {
// guard against requests that do not support bodies
switch (request.getMethod()) {
case "PUT":
case "PATCH":
case "POST":
request.body(new InputStreamRequestContent(requestDto));
request.headers(h -> h.add(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_UTF8));
break;
default:
// ignore body for other methods
}
}
// requires authorization token
setAccessToken(request);
doHttpRequest(request, new DelegatingClientCallback(callback));
} catch (UnsupportedEncodingException | URISyntaxException e) {
String msg = "Unexpected error: " + e.getMessage();
callback.onResponse(null, Collections.emptyMap(), new SalesforceException(msg, e));
}
}
@Override
public void getEventSchemaByEventName(
String eventName, String payloadFormat, Map<String, List<String>> headers, ResponseCallback callback) {
validateMinimumVersion(GET_SCHEMA_MINIMUM_VERSION);
final Request request;
request = getRequest(HttpMethod.GET, sobjectsUrl(eventName) + "/eventSchema" + "?payloadFormat=" + payloadFormat,
headers);
// requires authorization token
setAccessToken(request);
doHttpRequest(request, new DelegatingClientCallback(callback));
}
@Override
public void getEventSchemaBySchemaId(
String schemaId, String payloadFormat, Map<String, List<String>> headers, ResponseCallback callback) {
validateMinimumVersion(GET_SCHEMA_MINIMUM_VERSION);
final Request request;
request = getRequest(HttpMethod.GET, versionUrl() + "event/eventSchema/" + schemaId + "?payloadFormat=" + payloadFormat,
headers);
// requires authorization token
setAccessToken(request);
doHttpRequest(request, new DelegatingClientCallback(callback));
}
private String apexCallUrl(String apexUrl, Map<String, Object> queryParams)
throws UnsupportedEncodingException, URISyntaxException {
if (queryParams != null && !queryParams.isEmpty()) {
apexUrl = URISupport.appendParametersToURI(apexUrl, queryParams);
}
return instanceUrl + SERVICES_APEXREST + apexUrl;
}
@Override
public void recent(final Integer limit, Map<String, List<String>> headers, final ResponseCallback responseCallback) {
final String param = Optional.ofNullable(limit).map(v -> "?limit=" + v).orElse("");
final Request get = getRequest(HttpMethod.GET, versionUrl() + "recent/" + param, headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(responseCallback));
}
@Override
public void limits(Map<String, List<String>> headers, final ResponseCallback responseCallback) {
final Request get = getRequest(HttpMethod.GET, versionUrl() + "limits/", headers);
// requires authorization token
setAccessToken(get);
doHttpRequest(get, new DelegatingClientCallback(responseCallback));
}
private String servicesDataUrl() {
return instanceUrl + SERVICES_DATA;
}
private void validateMinimumVersion(String minimumVersion) {
if (Version.create(version).compareTo(Version.create(minimumVersion)) < 0) {
throw new IllegalArgumentException(
"Salesforce API version " + minimumVersion + " or newer is required, version " + version + " was detected");
}
}
private String versionUrl() {
ObjectHelper.notNull(version, "version");
return servicesDataUrl() + "v" + version + "/";
}
private String sobjectsUrl(String sObjectName) {
ObjectHelper.notNull(sObjectName, "sObjectName");
return versionUrl() + "sobjects/" + sObjectName;
}
private String sobjectsExternalIdUrl(String sObjectName, String fieldName, String fieldValue) {
if (fieldName == null || fieldValue == null) {
throw new IllegalArgumentException("External field name and value cannot be NULL");
}
try {
String encodedValue = urlEncode(fieldValue);
return sobjectsUrl(sObjectName + "/" + fieldName + "/" + encodedValue);
} catch (UnsupportedEncodingException e) {
String msg = "Unexpected error: " + e.getMessage();
throw new IllegalArgumentException(msg, e);
}
}
@Override
protected void setAccessToken(Request request) {
// replace old token
request.headers(h -> h.add(TOKEN_HEADER, TOKEN_PREFIX + accessToken));
}
private String urlEncode(String query) throws UnsupportedEncodingException {
String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8);
// URLEncoder likes to use '+' for spaces
encodedQuery = encodedQuery.replace("+", "%20");
return encodedQuery;
}
private static class DelegatingClientCallback implements ClientResponseCallback {
private final ResponseCallback callback;
DelegatingClientCallback(ResponseCallback callback) {
this.callback = callback;
}
@Override
public void onResponse(InputStream response, Map<String, String> headers, SalesforceException ex) {
callback.onResponse(response, headers, ex);
}
}
}