Skip to content

Commit 546fb31

Browse files
Merge branch 'develop' into revertDiscardLink
2 parents 8c7edbd + ed7b062 commit 546fb31

16 files changed

Lines changed: 980 additions & 546 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ Follow these steps if you want to integrate the SDM CAP Plugin with your own CAP
267267
properties:
268268
timeout: 3600000
269269
```
270+
**Note**: approuter version should be >= 16.8.2
270271
271272
6. Add the following facet in _fiori-service.cds_ in the _app_ folder. Refer the following [example](https://github.com/cap-java/sdm/blob/16c1b17d521a141ef1b1adfbed1e06c5bf7a980f/cap-notebook/demoapp/app/admin-books/fiori-service.cds#L24) from a sample Bookshop app.
272273

sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java

Lines changed: 149 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,65 +35,155 @@ private CacheConfig() {
3535
public static void initializeCache() {
3636
// Expiring the cache after 11 hours
3737
logger.info("Cache for user token and access token initialized");
38-
cacheManager.init();
39-
40-
userTokenCache =
41-
cacheManager.createCache(
42-
"userToken",
43-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
44-
CacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
45-
.withExpiry(
46-
Expirations.timeToLiveExpiration(
47-
new Duration(USER_TOKEN_EXPIRY, TimeUnit.MINUTES))));
48-
clientCredentialsTokenCache =
49-
cacheManager.createCache(
50-
"clientCredentialsToken",
51-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
52-
CacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
53-
.withExpiry(
54-
Expirations.timeToLiveExpiration(
55-
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
56-
repoCache =
57-
cacheManager.createCache(
58-
"versionedRepo",
59-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
60-
RepoKey.class, RepoValue.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
61-
.withExpiry(
62-
Expirations.timeToLiveExpiration(
63-
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
64-
65-
userAuthoritiesTokenCache =
66-
cacheManager.createCache(
67-
"userAuthoritiesToken",
68-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
69-
TokenCacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
70-
.withExpiry(
71-
Expirations.timeToLiveExpiration(
72-
new Duration(USER_TOKEN_EXPIRY, TimeUnit.MINUTES))));
73-
74-
secondaryTypesCache =
75-
cacheManager.createCache(
76-
"secondaryTypes",
77-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
78-
SecondaryTypesKey.class,
79-
(Class<List<String>>) (Class<?>) List.class,
80-
ResourcePoolsBuilder.heap(HEAP_SIZE))
81-
.withExpiry(Expirations.noExpiration()));
82-
83-
maxAllowedAttachmentsCache =
84-
cacheManager.createCache(
85-
"maxAllowedAttachmentsCache",
86-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
87-
String.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
88-
.withExpiry(Expirations.noExpiration()));
89-
secondaryPropertiesCache =
90-
cacheManager.createCache(
91-
"secondaryProperties",
92-
CacheConfigurationBuilder.newCacheConfigurationBuilder(
93-
SecondaryPropertiesKey.class,
94-
(Class<List<String>>) (Class<?>) List.class,
95-
ResourcePoolsBuilder.heap(HEAP_SIZE))
96-
.withExpiry(Expirations.noExpiration()));
38+
39+
try {
40+
cacheManager.init();
41+
} catch (IllegalStateException e) {
42+
// Cache manager already initialized
43+
logger.warn("Cache manager already initialized: {}", e.getMessage());
44+
}
45+
46+
// Initialize caches with defensive error handling
47+
try {
48+
userTokenCache =
49+
cacheManager.createCache(
50+
"userToken",
51+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
52+
CacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
53+
.withExpiry(
54+
Expirations.timeToLiveExpiration(
55+
new Duration(USER_TOKEN_EXPIRY, TimeUnit.MINUTES))));
56+
} catch (Exception e) {
57+
logger.warn("userTokenCache already exists or failed to create: {}", e.getMessage());
58+
// Try to get existing cache
59+
try {
60+
userTokenCache = cacheManager.getCache("userToken", CacheKey.class, String.class);
61+
} catch (Exception ex) {
62+
logger.error("Failed to retrieve existing userTokenCache: {}", ex.getMessage());
63+
}
64+
}
65+
66+
try {
67+
clientCredentialsTokenCache =
68+
cacheManager.createCache(
69+
"clientCredentialsToken",
70+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
71+
CacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
72+
.withExpiry(
73+
Expirations.timeToLiveExpiration(
74+
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
75+
} catch (Exception e) {
76+
logger.warn(
77+
"clientCredentialsTokenCache already exists or failed to create: {}", e.getMessage());
78+
try {
79+
clientCredentialsTokenCache =
80+
cacheManager.getCache("clientCredentialsToken", CacheKey.class, String.class);
81+
} catch (Exception ex) {
82+
logger.error(
83+
"Failed to retrieve existing clientCredentialsTokenCache: {}", ex.getMessage());
84+
}
85+
}
86+
87+
try {
88+
repoCache =
89+
cacheManager.createCache(
90+
"versionedRepo",
91+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
92+
RepoKey.class, RepoValue.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
93+
.withExpiry(
94+
Expirations.timeToLiveExpiration(
95+
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
96+
} catch (Exception e) {
97+
logger.warn("repoCache already exists or failed to create: {}", e.getMessage());
98+
try {
99+
repoCache = cacheManager.getCache("versionedRepo", RepoKey.class, RepoValue.class);
100+
} catch (Exception ex) {
101+
logger.error("Failed to retrieve existing repoCache: {}", ex.getMessage());
102+
}
103+
}
104+
105+
try {
106+
userAuthoritiesTokenCache =
107+
cacheManager.createCache(
108+
"userAuthoritiesToken",
109+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
110+
TokenCacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
111+
.withExpiry(
112+
Expirations.timeToLiveExpiration(
113+
new Duration(USER_TOKEN_EXPIRY, TimeUnit.MINUTES))));
114+
} catch (Exception e) {
115+
logger.warn(
116+
"userAuthoritiesTokenCache already exists or failed to create: {}", e.getMessage());
117+
try {
118+
userAuthoritiesTokenCache =
119+
cacheManager.getCache("userAuthoritiesToken", TokenCacheKey.class, String.class);
120+
} catch (Exception ex) {
121+
logger.error("Failed to retrieve existing userAuthoritiesTokenCache: {}", ex.getMessage());
122+
}
123+
}
124+
125+
try {
126+
secondaryTypesCache =
127+
cacheManager.createCache(
128+
"secondaryTypes",
129+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
130+
SecondaryTypesKey.class,
131+
(Class<List<String>>) (Class<?>) List.class,
132+
ResourcePoolsBuilder.heap(HEAP_SIZE))
133+
.withExpiry(Expirations.noExpiration()));
134+
} catch (Exception e) {
135+
logger.warn("secondaryTypesCache already exists or failed to create: {}", e.getMessage());
136+
try {
137+
secondaryTypesCache =
138+
cacheManager.getCache(
139+
"secondaryTypes",
140+
SecondaryTypesKey.class,
141+
(Class<List<String>>) (Class<?>) List.class);
142+
} catch (Exception ex) {
143+
logger.error("Failed to retrieve existing secondaryTypesCache: {}", ex.getMessage());
144+
}
145+
}
146+
147+
try {
148+
maxAllowedAttachmentsCache =
149+
cacheManager.createCache(
150+
"maxAllowedAttachmentsCache",
151+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
152+
String.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
153+
.withExpiry(Expirations.noExpiration()));
154+
} catch (Exception e) {
155+
logger.warn(
156+
"maxAllowedAttachmentsCache already exists or failed to create: {}", e.getMessage());
157+
try {
158+
maxAllowedAttachmentsCache =
159+
cacheManager.getCache("maxAllowedAttachmentsCache", String.class, String.class);
160+
} catch (Exception ex) {
161+
logger.error("Failed to retrieve existing maxAllowedAttachmentsCache: {}", ex.getMessage());
162+
}
163+
}
164+
165+
try {
166+
secondaryPropertiesCache =
167+
cacheManager.createCache(
168+
"secondaryProperties",
169+
CacheConfigurationBuilder.newCacheConfigurationBuilder(
170+
SecondaryPropertiesKey.class,
171+
(Class<List<String>>) (Class<?>) List.class,
172+
ResourcePoolsBuilder.heap(HEAP_SIZE))
173+
.withExpiry(Expirations.noExpiration()));
174+
} catch (Exception e) {
175+
logger.warn(
176+
"secondaryPropertiesCache already exists or failed to create: {}", e.getMessage());
177+
try {
178+
secondaryPropertiesCache =
179+
cacheManager.getCache(
180+
"secondaryProperties",
181+
SecondaryPropertiesKey.class,
182+
(Class<List<String>>) (Class<?>) List.class);
183+
} catch (Exception ex) {
184+
logger.error("Failed to retrieve existing secondaryPropertiesCache: {}", ex.getMessage());
185+
}
186+
}
97187
}
98188

99189
public static Cache<CacheKey, String> getUserTokenCache() {

sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.sap.cds.sdm.constants;
22

3+
import java.util.Collection;
4+
import java.util.HashSet;
35
import java.util.List;
46
import java.util.Map;
7+
import java.util.Set;
58

69
public class SDMConstants {
710
private SDMConstants() {
@@ -23,7 +26,6 @@ private SDMConstants() {
2326
"The following files could not be renamed as they already exist:\n%s\n";
2427
public static final String COULD_NOT_UPDATE_THE_ATTACHMENT = "Could not update the attachment";
2528
public static final String ATTACHMENT_NOT_FOUND = "Attachment not found";
26-
public static final String DUPLICATE_FILES_ERROR = "%s already exists.";
2729
public static final String GENERIC_ERROR = "Could not %s the document.";
2830
public static final String VERSIONED_REPO_ERROR =
2931
"Upload not supported for versioned repositories.";
@@ -140,45 +142,46 @@ private SDMConstants() {
140142
"Failed to parse repository response";
141143
public static final String ERROR_IN_SETTING_TIMEOUT_MESSAGE = "Error in setting timeout";
142144
public static final String FAILED_TO_CREATE_FOLDER = "Failed to create folder";
145+
public static final String FILENAME_WHITESPACE_ERROR_MESSAGE =
146+
"The object name cannot be empty or consist entirely of space characters. Enter a value.";
147+
public static final String SINGLE_RESTRICTED_CHARACTER_IN_FILE =
148+
"\"%s\" contains unsupported characters (‘/’ or ‘\\’). Rename and try again.";
149+
public static final String SINGLE_DUPLICATE_FILENAME =
150+
"An object named \"%s\" already exists. Rename the object and try again.";
143151

144-
public static String nameConstraintMessage(
145-
List<String> fileNameWithRestrictedCharacters, String operation) {
146-
// Create the base message
147-
String prefixMessage =
148-
"%s unsuccessful. The following filename(s) contain unsupported characters (/, \\). \n\n";
149-
150-
// Create the formatted prefix message
151-
String formattedPrefixMessage = String.format(prefixMessage, operation);
152-
153-
// Initialize the StringBuilder with the formatted message prefix
154-
StringBuilder bulletPoints = new StringBuilder(formattedPrefixMessage);
155-
156-
// Append each unsupported file name to the StringBuilder
157-
for (String file : fileNameWithRestrictedCharacters) {
158-
bulletPoints.append(String.format("\t• %s%n", file));
152+
// Helper Methods to create error/warning messages
153+
public static String buildErrorMessage(
154+
Collection<String> filenames, StringBuilder prefixTemplate, String closingRemark) {
155+
for (String file : filenames) {
156+
prefixTemplate.append(String.format("\t• %s%n", file));
159157
}
160-
bulletPoints.append("\nRename the files and try again.");
161-
return bulletPoints.toString();
158+
if (closingRemark != null && !closingRemark.isEmpty())
159+
prefixTemplate.append("\n ").append(closingRemark);
160+
return prefixTemplate.toString();
162161
}
163162

164-
public static String linkNameConstraintMessage(
165-
List<String> fileNameWithRestrictedCharacters, String operation) {
166-
// Create the base message
167-
String prefixMessage =
168-
"Link could not be %s. The following name(s) contain unsupported characters (/, \\). \n\n";
169-
170-
// Create the formatted prefix message
171-
String formattedPrefixMessage = String.format(prefixMessage, operation);
172-
173-
// Initialize the StringBuilder with the formatted message prefix
174-
StringBuilder bulletPoints = new StringBuilder(formattedPrefixMessage);
163+
// Restricted characters: / and \
164+
public static String nameConstraintMessage(List<String> invalidFileNames) {
165+
// if only 1 restricted character is there in file, so different error will throw
166+
if (invalidFileNames.size() == 1) {
167+
return String.format(SINGLE_RESTRICTED_CHARACTER_IN_FILE, invalidFileNames.iterator().next());
168+
}
169+
StringBuilder prefix = new StringBuilder();
170+
prefix.append(
171+
"The following names contain unsupported characters (‘/’ or ‘\\’). Rename and try again:\n\n");
172+
return buildErrorMessage(invalidFileNames, prefix, null);
173+
}
175174

176-
// Append each unsupported file name to the StringBuilder
177-
for (String file : fileNameWithRestrictedCharacters) {
178-
bulletPoints.append(String.format("\t• %s%n", file));
175+
// Duplicate file names error message
176+
public static String duplicateFilenameFormat(Collection<String> duplicateFileNames) {
177+
// if only 1 duplicate file, so different error will throw
178+
if (duplicateFileNames.size() == 1) {
179+
return String.format(SINGLE_DUPLICATE_FILENAME, duplicateFileNames.iterator().next());
179180
}
180-
bulletPoints.append("\nRename the link and try again.");
181-
return bulletPoints.toString();
181+
StringBuilder prefix = new StringBuilder();
182+
prefix.append("Objects with the following names already exist:\n\n");
183+
String closingRemark = "Rename the objects and try again";
184+
return buildErrorMessage(duplicateFileNames, prefix, closingRemark);
182185
}
183186

184187
public static String fileNotFound(List<String> fileNameNotFound) {
@@ -253,7 +256,9 @@ public static String unsupportedPropertiesMessage(List<String> propertiesList) {
253256
}
254257

255258
public static String getDuplicateFilesError(String filename) {
256-
return String.format(DUPLICATE_FILES_ERROR, filename);
259+
Set<String> filenames = new HashSet<>();
260+
filenames.add(filename);
261+
return duplicateFilenameFormat(filenames);
257262
}
258263

259264
public static String getGenericError(String event) {

0 commit comments

Comments
 (0)