Skip to content

Commit 91c4187

Browse files
committed
Merge branch 'moveAttachmentsSupport' of https://www.github.com/cap-java/sdm into moveAttachmentsSupport
2 parents 1d20713 + 22237d0 commit 91c4187

3 files changed

Lines changed: 142 additions & 49 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ private void createLink(EventContext context) throws IOException {
381381
validateLinkName(filenameInRequest, result);
382382

383383
Boolean isSystemUser = context.getUserInfo().isSystemUser();
384-
String entityName = context.getTarget().getQualifiedName().split("\\.")[2];
384+
String[] parts = context.getTarget().getQualifiedName().split("\\.");
385+
String entityName = parts[parts.length - 1];
385386
String folderName = upID + "__" + entityName;
386387

387388
String folderId = sdmService.getFolderId(result, persistenceService, folderName, isSystemUser);

sdm/src/test/java/integration/com/sap/cds/sdm/Api.java

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class Api implements ApiInterface {
1313
private static final ObjectMapper objectMapper = new ObjectMapper();
1414
private final String token;
1515
private final String serviceName;
16+
private static final int MAX_RETRIES = 3;
17+
private static final int RETRY_DELAY_MS = 1000;
1618

1719
public Api(Map<String, String> config) {
1820
this.config = new HashMap<>(config);
@@ -26,6 +28,50 @@ public Api(Map<String, String> config) {
2628
this.serviceName = this.config.get("serviceName");
2729
}
2830

31+
private Response executeWithRetry(Request request) throws IOException {
32+
IOException lastException = null;
33+
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
34+
try {
35+
Response response = httpClient.newCall(request).execute();
36+
if (response.code() == 502 && attempt < MAX_RETRIES) {
37+
System.out.println(
38+
"Received 502 Bad Gateway, retrying... (attempt "
39+
+ attempt
40+
+ "/"
41+
+ MAX_RETRIES
42+
+ ")");
43+
response.close();
44+
Thread.sleep(RETRY_DELAY_MS);
45+
continue;
46+
}
47+
return response;
48+
} catch (java.net.SocketTimeoutException e) {
49+
lastException = e;
50+
if (attempt < MAX_RETRIES) {
51+
System.out.println(
52+
"Socket timeout occurred, retrying... (attempt "
53+
+ attempt
54+
+ "/"
55+
+ MAX_RETRIES
56+
+ "): "
57+
+ e.getMessage());
58+
try {
59+
Thread.sleep(RETRY_DELAY_MS);
60+
} catch (InterruptedException ie) {
61+
Thread.currentThread().interrupt();
62+
throw new IOException("Retry interrupted", ie);
63+
}
64+
} else {
65+
throw e;
66+
}
67+
} catch (InterruptedException e) {
68+
Thread.currentThread().interrupt();
69+
throw new IOException("Retry interrupted", e);
70+
}
71+
}
72+
throw lastException;
73+
}
74+
2975
public String createEntityDraft(
3076
String appUrl, String entityName, String entityName2, String srvpath) {
3177
MediaType mediaType = MediaType.parse("application/json");
@@ -46,7 +92,7 @@ public String createEntityDraft(
4692
.addHeader("Authorization", token)
4793
.build();
4894

49-
try (Response response = httpClient.newCall(request).execute()) {
95+
try (Response response = executeWithRetry(request)) {
5096
if (!response.isSuccessful()) {
5197
if (response.code() == 401) {
5298
System.out.println(
@@ -83,7 +129,7 @@ public String editEntityDraft(String appUrl, String entityName, String srvpath,
83129
.addHeader("Authorization", token)
84130
.build();
85131

86-
try (Response response = httpClient.newCall(request).execute()) {
132+
try (Response response = executeWithRetry(request)) {
87133
if (response.code() != 200) {
88134
System.out.println("Edit entity failed. Error : " + response.body().string());
89135
throw new IOException("Could not edit entity");
@@ -116,7 +162,7 @@ public String saveEntityDraft(String appUrl, String entityName, String srvpath,
116162
.addHeader("Authorization", token)
117163
.build();
118164

119-
try (Response response = httpClient.newCall(request).execute()) {
165+
try (Response response = executeWithRetry(request)) {
120166
if (response.code() != 200) {
121167
System.out.println("Save entity failed. Error : " + response.body().string());
122168
throw new IOException("Could not save entity");
@@ -139,7 +185,7 @@ public String saveEntityDraft(String appUrl, String entityName, String srvpath,
139185
.addHeader("Authorization", token)
140186
.build();
141187

142-
try (Response draftResponse = httpClient.newCall(request).execute()) {
188+
try (Response draftResponse = executeWithRetry(request)) {
143189
if (draftResponse.code() != 200) {
144190
String draftResponseBodyString = draftResponse.body().string();
145191
System.out.println("Save entity failed. Error : " + draftResponseBodyString);
@@ -178,7 +224,7 @@ public String deleteEntity(String appUrl, String entityName, String entityID) {
178224
.addHeader("Authorization", token)
179225
.build();
180226

181-
try (Response response = httpClient.newCall(request).execute()) {
227+
try (Response response = executeWithRetry(request)) {
182228
if (!response.isSuccessful()) {
183229
System.out.println("Delete entity failed. Error : " + response.body().string());
184230
throw new IOException("Could not delete entity");
@@ -207,7 +253,7 @@ public String deleteEntityDraft(String appUrl, String entityName, String entityI
207253
.addHeader("Authorization", token)
208254
.build();
209255

210-
try (Response response = httpClient.newCall(request).execute()) {
256+
try (Response response = executeWithRetry(request)) {
211257
if (!response.isSuccessful()) {
212258
System.out.println("Delete entity failed. Error : " + response.body().string());
213259
throw new IOException("Could not delete entity");
@@ -235,7 +281,7 @@ public String checkEntity(String appUrl, String entityName, String entityID) {
235281
.addHeader("Authorization", token)
236282
.build();
237283

238-
try (Response checkResponse = httpClient.newCall(request).execute()) {
284+
try (Response checkResponse = executeWithRetry(request)) {
239285
if (checkResponse.code() != 200) {
240286
System.out.println("Verify entity failed. Error : " + checkResponse.body().string());
241287
throw new IOException("Entity doesn't exist");
@@ -285,7 +331,7 @@ public List<String> createAttachment(
285331
.addHeader("Authorization", token)
286332
.build();
287333

288-
try (Response response = httpClient.newCall(postRequest).execute()) {
334+
try (Response response = executeWithRetry(postRequest)) {
289335
if (response.code() != 201) {
290336
System.out.println(
291337
"Create Attachment in the section: "
@@ -320,7 +366,7 @@ public List<String> createAttachment(
320366
.addHeader("Authorization", token)
321367
.build();
322368

323-
try (Response fileResponse = httpClient.newCall(fileRequest).execute()) {
369+
try (Response fileResponse = executeWithRetry(fileRequest)) {
324370
if (fileResponse.code() != 204) {
325371
String responseBodyString = fileResponse.body().string();
326372
System.out.println(
@@ -349,7 +395,7 @@ public List<String> createAttachment(
349395
.addHeader("Authorization", token)
350396
.build();
351397

352-
try (Response deleteResponse = httpClient.newCall(request).execute()) {
398+
try (Response deleteResponse = executeWithRetry(request)) {
353399
if (deleteResponse.code() != 204) {
354400
System.out.println(
355401
"Delete Attachment in section :"
@@ -416,7 +462,7 @@ public String readAttachment(
416462
.build();
417463

418464
try {
419-
Response response = httpClient.newCall(request).execute();
465+
Response response = executeWithRetry(request);
420466
if (!response.isSuccessful()) {
421467
System.out.println(
422468
"Read Attachment failed in the "
@@ -458,7 +504,7 @@ public String readAttachmentDraft(
458504
.build();
459505

460506
try {
461-
Response response = httpClient.newCall(request).execute();
507+
Response response = executeWithRetry(request);
462508
if (!response.isSuccessful()) {
463509
System.out.println("Read draft attachment failed. Error : " + response.body().string());
464510
throw new IOException("Could not read attachment");
@@ -492,7 +538,7 @@ public String deleteAttachment(
492538
.addHeader("Authorization", token)
493539
.build();
494540

495-
try (Response deleteResponse = httpClient.newCall(request).execute()) {
541+
try (Response deleteResponse = executeWithRetry(request)) {
496542
if (deleteResponse.code() != 204) {
497543
System.out.println(
498544
"Delete Attachment failed in the "
@@ -535,7 +581,7 @@ public String renameAttachment(
535581
.addHeader("Authorization", token)
536582
.build();
537583

538-
try (Response renameResponse = httpClient.newCall(request).execute()) {
584+
try (Response renameResponse = executeWithRetry(request)) {
539585
if (!renameResponse.isSuccessful()) {
540586
System.out.println(
541587
"Rename Attachment failed in the "
@@ -580,7 +626,7 @@ public String updateSecondaryProperty(
580626
.addHeader("Authorization", token)
581627
.build();
582628

583-
try (Response updateResponse = httpClient.newCall(request).execute()) {
629+
try (Response updateResponse = executeWithRetry(request)) {
584630
if (updateResponse.code() != 200) {
585631
System.out.println(
586632
"Updating secondary property failed. Error: " + updateResponse.body().string());
@@ -624,7 +670,7 @@ public String updateInvalidSecondaryProperty(
624670
.addHeader("Authorization", token)
625671
.build();
626672

627-
try (Response updateResponse = httpClient.newCall(request).execute()) {
673+
try (Response updateResponse = executeWithRetry(request)) {
628674
if (updateResponse.code() != 200) {
629675
System.out.println(
630676
"Updating secondary property failed. Error : " + updateResponse.body().string());
@@ -670,7 +716,7 @@ public String copyAttachment(
670716
Request request =
671717
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
672718

673-
try (Response response = httpClient.newCall(request).execute()) {
719+
try (Response response = executeWithRetry(request)) {
674720
if (!response.isSuccessful()) {
675721
throw new IOException(
676722
"Could not copy attachments: " + response.code() + " - " + response.body().string());
@@ -715,7 +761,7 @@ public String createLink(
715761
Request request =
716762
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
717763

718-
try (Response response = httpClient.newCall(request).execute()) {
764+
try (Response response = executeWithRetry(request)) {
719765
if (!response.isSuccessful()) {
720766
throw new IOException(
721767
"Could not create link: " + response.code() + " - " + response.body().string());
@@ -758,7 +804,7 @@ public String openAttachment(
758804
Request request =
759805
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
760806

761-
try (Response response = httpClient.newCall(request).execute()) {
807+
try (Response response = executeWithRetry(request)) {
762808
if (!response.isSuccessful()) {
763809
throw new IOException(
764810
"Could not open attachment: " + response.code() + " - " + response.body().string());
@@ -807,7 +853,7 @@ public String editLink(
807853
Request request =
808854
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
809855

810-
try (Response response = httpClient.newCall(request).execute()) {
856+
try (Response response = executeWithRetry(request)) {
811857
if (!response.isSuccessful()) {
812858
throw new IOException(
813859
"Could not edit link: " + response.code() + " - " + response.body().string());
@@ -842,7 +888,7 @@ public Map<String, Object> fetchMetadata(
842888
Request request =
843889
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
844890

845-
try (Response response = httpClient.newCall(request).execute()) {
891+
try (Response response = executeWithRetry(request)) {
846892
if (response.code() != 200) {
847893
System.out.println("Response code: " + response.code());
848894
System.out.println(
@@ -883,7 +929,7 @@ public Map<String, Object> fetchMetadataDraft(
883929
Request request =
884930
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
885931

886-
try (Response response = httpClient.newCall(request).execute()) {
932+
try (Response response = executeWithRetry(request)) {
887933
if (response.code() != 200) {
888934
System.out.println("Response code: " + response.code());
889935
System.out.println(
@@ -921,7 +967,7 @@ public List<Map<String, Object>> fetchEntityMetadata(
921967
Request request =
922968
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
923969

924-
try (Response response = httpClient.newCall(request).execute()) {
970+
try (Response response = executeWithRetry(request)) {
925971
if (response.code() != 200) {
926972
System.out.println("Response code: " + response.code());
927973
System.out.println(
@@ -965,7 +1011,7 @@ public List<Map<String, Object>> fetchEntityMetadataDraft(
9651011
Request request =
9661012
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
9671013

968-
try (Response response = httpClient.newCall(request).execute()) {
1014+
try (Response response = executeWithRetry(request)) {
9691015
if (response.code() != 200) {
9701016
System.out.println("Response code: " + response.code());
9711017
System.out.println(

0 commit comments

Comments
 (0)