Skip to content

Commit 25099ef

Browse files
Merge pull request #321 from cap-java/filesizeissue
DINC0645325:Empty error message when large file is uploaded
2 parents b2edf0c + 7be8775 commit 25099ef

File tree

12 files changed

+389
-178
lines changed

12 files changed

+389
-178
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.cds.sdm.caching;
22

3+
import com.sap.cds.sdm.model.RepoValue;
34
import java.util.List;
45
import java.util.concurrent.TimeUnit;
56
import org.ehcache.Cache;
@@ -18,7 +19,7 @@ public class CacheConfig {
1819
private static Cache<CacheKey, String> userTokenCache;
1920
private static Cache<CacheKey, String> clientCredentialsTokenCache;
2021
private static Cache<TokenCacheKey, String> userAuthoritiesTokenCache;
21-
private static Cache<RepoKey, String> versionedRepoCache;
22+
private static Cache<RepoKey, RepoValue> repoCache;
2223
private static Cache<SecondaryTypesKey, List<String>> secondaryTypesCache;
2324
private static Cache<String, String> maxAllowedAttachmentsCache;
2425
private static Cache<SecondaryPropertiesKey, List<String>> secondaryPropertiesCache;
@@ -52,11 +53,11 @@ public static void initializeCache() {
5253
.withExpiry(
5354
Expirations.timeToLiveExpiration(
5455
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
55-
versionedRepoCache =
56+
repoCache =
5657
cacheManager.createCache(
5758
"versionedRepo",
5859
CacheConfigurationBuilder.newCacheConfigurationBuilder(
59-
RepoKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
60+
RepoKey.class, RepoValue.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
6061
.withExpiry(
6162
Expirations.timeToLiveExpiration(
6263
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
@@ -107,8 +108,8 @@ public static Cache<CacheKey, String> getClientCredentialsTokenCache() {
107108
return clientCredentialsTokenCache;
108109
}
109110

110-
public static Cache<RepoKey, String> getVersionedRepoCache() {
111-
return versionedRepoCache;
111+
public static Cache<RepoKey, RepoValue> getRepoCache() {
112+
return repoCache;
112113
}
113114

114115
public static Cache<String, String> getMaxAllowedAttachmentsCache() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ private SDMConstants() {
2727
public static final String GENERIC_ERROR = "Could not %s the document.";
2828
public static final String VERSIONED_REPO_ERROR =
2929
"Upload not supported for versioned repositories.";
30+
public static final String VIRUS_REPO_ERROR_MORE_THAN_400MB =
31+
"You cannot upload files that are larger than 400 MB";
3032
public static final String VIRUS_ERROR = "%s contains potential malware and cannot be uploaded.";
3133
public static final String REPOSITORY_ERROR = "Failed to get repository info.";
3234
public static final String NOT_FOUND_ERROR = "Failed to read document.";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.sap.cds.sdm.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class RepoValue {
11+
private Boolean virusScanEnabled;
12+
private Boolean versionEnabled;
13+
private Boolean disableVirusScannerForLargeFile;
14+
}

sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void addAttachmentToDraft(
130130
updatedFields.put("folderId", cmisDocument.getFolderId());
131131
updatedFields.put("status", "Clean");
132132
updatedFields.put("type", "sap-icon://document");
133+
updatedFields.put("mimeType", cmisDocument.getMimeType());
133134
CqnUpdate updateQuery =
134135
Update.entity(attachmentEntity)
135136
.data(updatedFields)

sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private void formResponse(
298298
String status = "success";
299299
String name = cmisDocument.getFileName();
300300
String id = cmisDocument.getAttachmentId();
301-
String objectId = "";
301+
String objectId = "", mimeType = "";
302302
String error = "";
303303
try {
304304
String responseString = EntityUtils.toString(response.getEntity());
@@ -308,6 +308,7 @@ private void formResponse(
308308
JSONObject succinctProperties = jsonResponse.getJSONObject("succinctProperties");
309309
status = "success";
310310
objectId = succinctProperties.getString("cmis:objectId");
311+
mimeType = succinctProperties.getString("cmis:contentStreamMimeType");
311312
} else {
312313
if (responseCode == 409) {
313314
JSONObject jsonResponse = new JSONObject(responseString);
@@ -339,6 +340,7 @@ private void formResponse(
339340
finalResponse.put("message", error);
340341
if (!objectId.isEmpty()) {
341342
finalResponse.put("objectId", objectId);
343+
finalResponse.put("mimeType", mimeType);
342344
}
343345
} catch (IOException e) {
344346
throw new ServiceException(SDMConstants.getGenericError("upload"));

sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sap.cds.Result;
44
import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext;
55
import com.sap.cds.sdm.model.CmisDocument;
6+
import com.sap.cds.sdm.model.RepoValue;
67
import com.sap.cds.sdm.model.SDMCredentials;
78
import com.sap.cds.services.ServiceException;
89
import com.sap.cds.services.persistence.PersistenceService;
@@ -27,12 +28,10 @@ public String getFolderIdByPath(
2728
String parentId, String repositoryId, SDMCredentials sdmCredentials, boolean isSystemUser)
2829
throws IOException;
2930

30-
public String checkRepositoryType(String repositoryId, String tenant) throws IOException;
31+
public RepoValue checkRepositoryType(String repositoryId, String tenant) throws IOException;
3132

3233
public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) throws IOException;
3334

34-
public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) throws IOException;
35-
3635
public int deleteDocument(String cmisaction, String objectId, String user) throws IOException;
3736

3837
public void readDocument(

sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55

66
import com.sap.cds.Result;
77
import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext;
8-
import com.sap.cds.sdm.caching.CacheConfig;
9-
import com.sap.cds.sdm.caching.RepoKey;
10-
import com.sap.cds.sdm.caching.SecondaryPropertiesKey;
11-
import com.sap.cds.sdm.caching.SecondaryTypesKey;
8+
import com.sap.cds.sdm.caching.*;
129
import com.sap.cds.sdm.constants.SDMConstants;
1310
import com.sap.cds.sdm.handler.TokenHandler;
1411
import com.sap.cds.sdm.model.CmisDocument;
12+
import com.sap.cds.sdm.model.RepoValue;
1513
import com.sap.cds.sdm.model.SDMCredentials;
1614
import com.sap.cds.sdm.utilities.SDMUtils;
1715
import com.sap.cds.services.ServiceException;
1816
import com.sap.cds.services.environment.CdsProperties;
1917
import com.sap.cds.services.persistence.PersistenceService;
2018
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
21-
import com.sap.cloud.sdk.cloudplatform.connectivity.*;
2219
import java.io.ByteArrayInputStream;
2320
import java.io.IOException;
2421
import java.io.InputStream;
@@ -480,33 +477,23 @@ else if (responseCode == 403) {
480477
}
481478

482479
@Override
483-
public String checkRepositoryType(String repositoryId, String tenant) {
480+
public RepoValue checkRepositoryType(String repositoryId, String tenant) {
484481
RepoKey repoKey = new RepoKey();
485482
repoKey.setSubdomain(tenant);
486483
repoKey.setRepoId(repositoryId);
487-
String type = CacheConfig.getVersionedRepoCache().get(repoKey);
488-
Boolean isVersioned;
489-
if (type == null) {
484+
RepoValue repoValue = CacheConfig.getRepoCache().get(repoKey);
485+
if (repoValue == null) {
490486
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
491487
JSONObject repoInfo = getRepositoryInfo(sdmCredentials);
492-
isVersioned = isRepositoryVersioned(repoInfo, repositoryId);
493-
} else {
494-
isVersioned = "Versioned".equals(type);
495-
}
496-
497-
if (Boolean.TRUE.equals(isVersioned)) {
498-
repoKey = new RepoKey();
499-
repoKey.setSubdomain(tenant);
500-
repoKey.setRepoId(repositoryId);
501-
CacheConfig.getVersionedRepoCache().put(repoKey, "Versioned");
502-
return "Versioned";
503-
} else {
488+
Map<String, RepoValue> repoValueMap = fetchRepositoryData(repoInfo, repositoryId);
504489
repoKey = new RepoKey();
505490
repoKey.setSubdomain(tenant);
506491
repoKey.setRepoId(repositoryId);
507-
CacheConfig.getVersionedRepoCache().put(repoKey, "Non Versioned");
508-
return "Non Versioned";
492+
RepoValue value = repoValueMap.get(repositoryId);
493+
CacheConfig.getRepoCache().put(repoKey, value);
494+
return repoValueMap.get(repositoryId);
509495
}
496+
return repoValue;
510497
}
511498

512499
public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) {
@@ -526,17 +513,28 @@ public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) {
526513
}
527514
}
528515

529-
public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) {
516+
public Map<String, RepoValue> fetchRepositoryData(JSONObject repoInfo, String repositoryId) {
517+
Map<String, RepoValue> repoValueMap = new HashMap<>();
530518
repoInfo = repoInfo.getJSONObject(repositoryId);
531519
JSONObject capabilities = repoInfo.getJSONObject("capabilities");
532520
String type = capabilities.getString("capabilityContentStreamUpdatability");
533-
if ("pwconly".equals(type)) {
534-
type = "Versioned";
535-
} else {
536-
type = "Non Versioned";
521+
RepoValue repoValue = new RepoValue();
522+
repoValue.setVersionEnabled("pwconly".equals(type) ? true : false);
523+
JSONArray extendedFeaturesArray = repoInfo.getJSONArray("extendedFeatures");
524+
// Iterate over the array and find the object with featureData
525+
for (int i = 0; i < extendedFeaturesArray.length(); i++) {
526+
JSONObject feature = extendedFeaturesArray.getJSONObject(i);
527+
if (feature.has("featureData")) {
528+
JSONObject featureData = feature.getJSONObject("featureData");
529+
// Fetch the 'virusScanner' value
530+
repoValue.setVirusScanEnabled(featureData.getBoolean("virusScanner"));
531+
// Fetch the disableVirusScannerForLargeFile
532+
repoValue.setDisableVirusScannerForLargeFile(
533+
featureData.getBoolean("disableVirusScannerForLargeFile"));
534+
}
537535
}
538-
539-
return "Versioned".equals(type);
536+
repoValueMap.put(repositoryId, repoValue);
537+
return repoValueMap;
540538
}
541539

542540
@Override

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.sap.cds.sdm.service.handler;
22

3-
import static com.sap.cds.sdm.persistence.DBQuery.*;
4-
53
import com.sap.cds.Result;
64
import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.MediaData;
75
import com.sap.cds.feature.attachments.service.AttachmentService;
@@ -13,6 +11,7 @@
1311
import com.sap.cds.sdm.constants.SDMConstants;
1412
import com.sap.cds.sdm.handler.TokenHandler;
1513
import com.sap.cds.sdm.model.CmisDocument;
14+
import com.sap.cds.sdm.model.RepoValue;
1615
import com.sap.cds.sdm.model.SDMCredentials;
1716
import com.sap.cds.sdm.persistence.DBQuery;
1817
import com.sap.cds.sdm.service.DocumentUploadService;
@@ -143,15 +142,24 @@ private boolean isObjectIdPresent(List<CmisDocument> documents, String objectId)
143142
private void validateRepository(AttachmentCreateEventContext eventContext)
144143
throws ServiceException, IOException {
145144
String repositoryId = SDMConstants.REPOSITORY_ID;
146-
String repocheck =
145+
RepoValue repoValue =
147146
sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant());
148-
if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) {
147+
if (repoValue.getVersionEnabled()) {
149148
throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR);
150149
}
150+
String len = eventContext.getParameterInfo().getHeaders().get("content-length");
151+
long contentLen = !StringUtils.isEmpty(len) ? Long.parseLong(len) : -1;
152+
// Check if repository is virus scanned
153+
if (repoValue.getVirusScanEnabled()
154+
&& contentLen > 400 * 1024 * 1024
155+
&& !repoValue.getDisableVirusScannerForLargeFile()) {
156+
throw new ServiceException(SDMConstants.VIRUS_REPO_ERROR_MORE_THAN_400MB);
157+
}
151158
}
152159

153160
private void processEntities(AttachmentCreateEventContext eventContext)
154161
throws ServiceException, IOException {
162+
155163
Map<String, Object> attachmentIds = eventContext.getAttachmentIds();
156164
CdsEntity attachmentDraftEntity = getAttachmentDraftEntity(eventContext);
157165
String upIdKey = getUpIdKey(attachmentDraftEntity);
@@ -297,6 +305,7 @@ private void handleCreateDocumentResult(
297305
throw new ServiceException(SDMConstants.MIMETYPE_INVALID_ERROR);
298306
default:
299307
cmisDocument.setObjectId(createResult.get("objectId").toString());
308+
cmisDocument.setMimeType(createResult.get("mimeType").toString());
300309
dbQuery.addAttachmentToDraft(
301310
getAttachmentDraftEntity(eventContext), persistenceService, cmisDocument);
302311
finalizeContext(eventContext, cmisDocument);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import com.sap.cds.reflect.CdsModel;
1313
import com.sap.cds.sdm.constants.SDMConstants;
1414
import com.sap.cds.sdm.handler.TokenHandler;
15-
import com.sap.cds.sdm.model.AttachmentReadContext;
16-
import com.sap.cds.sdm.model.CmisDocument;
17-
import com.sap.cds.sdm.model.CopyAttachmentInput;
18-
import com.sap.cds.sdm.model.SDMCredentials;
15+
import com.sap.cds.sdm.model.*;
1916
import com.sap.cds.sdm.persistence.DBQuery;
2017
import com.sap.cds.sdm.service.DocumentUploadService;
2118
import com.sap.cds.sdm.service.RegisterService;
@@ -118,9 +115,9 @@ public void openAttachment(AttachmentReadContext context) throws Exception {
118115

119116
private void validateRepository(EventContext eventContext) throws ServiceException, IOException {
120117
String repositoryId = SDMConstants.REPOSITORY_ID;
121-
String repocheck =
118+
RepoValue repoValue =
122119
sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant());
123-
if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) {
120+
if (repoValue.getVersionEnabled()) {
124121
throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR);
125122
}
126123
}

0 commit comments

Comments
 (0)