Skip to content
Merged
11 changes: 6 additions & 5 deletions sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.cds.sdm.caching;

import com.sap.cds.sdm.model.RepoValue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.ehcache.Cache;
Expand All @@ -18,7 +19,7 @@ public class CacheConfig {
private static Cache<CacheKey, String> userTokenCache;
private static Cache<CacheKey, String> clientCredentialsTokenCache;
private static Cache<TokenCacheKey, String> userAuthoritiesTokenCache;
private static Cache<RepoKey, String> versionedRepoCache;
private static Cache<RepoKey, RepoValue> repoCache;
private static Cache<SecondaryTypesKey, List<String>> secondaryTypesCache;
private static Cache<String, String> maxAllowedAttachmentsCache;
private static Cache<SecondaryPropertiesKey, List<String>> secondaryPropertiesCache;
Expand Down Expand Up @@ -52,11 +53,11 @@ public static void initializeCache() {
.withExpiry(
Expirations.timeToLiveExpiration(
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
versionedRepoCache =
repoCache =
cacheManager.createCache(
"versionedRepo",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
RepoKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
RepoKey.class, RepoValue.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
.withExpiry(
Expirations.timeToLiveExpiration(
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
Expand Down Expand Up @@ -107,8 +108,8 @@ public static Cache<CacheKey, String> getClientCredentialsTokenCache() {
return clientCredentialsTokenCache;
}

public static Cache<RepoKey, String> getVersionedRepoCache() {
return versionedRepoCache;
public static Cache<RepoKey, RepoValue> getRepoCache() {
return repoCache;
}

public static Cache<String, String> getMaxAllowedAttachmentsCache() {
Expand Down
2 changes: 2 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ private SDMConstants() {
public static final String GENERIC_ERROR = "Could not %s the document.";
public static final String VERSIONED_REPO_ERROR =
"Upload not supported for versioned repositories.";
public static final String VIRUS_REPO_ERROR_MORE_THAN_400MB =
"You cannot upload files that are larger than 400 MB";
Comment on lines +30 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked this error message with UX

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes approved by UX

public static final String VIRUS_ERROR = "%s contains potential malware and cannot be uploaded.";
public static final String REPOSITORY_ERROR = "Failed to get repository info.";
public static final String NOT_FOUND_ERROR = "Failed to read document.";
Expand Down
14 changes: 14 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/model/RepoValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sap.cds.sdm.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RepoValue {
private Boolean virusScanEnabled;
private Boolean versionEnabled;
private Boolean disableVirusScannerForLargeFile;
}
1 change: 1 addition & 0 deletions sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void addAttachmentToDraft(
updatedFields.put("folderId", cmisDocument.getFolderId());
updatedFields.put("status", "Clean");
updatedFields.put("type", "sap-icon://document");
updatedFields.put("mimeType", cmisDocument.getMimeType());
CqnUpdate updateQuery =
Update.entity(attachmentEntity)
.data(updatedFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private void formResponse(
String status = "success";
String name = cmisDocument.getFileName();
String id = cmisDocument.getAttachmentId();
String objectId = "";
String objectId = "", mimeType = "";
String error = "";
try {
String responseString = EntityUtils.toString(response.getEntity());
Expand All @@ -308,6 +308,7 @@ private void formResponse(
JSONObject succinctProperties = jsonResponse.getJSONObject("succinctProperties");
status = "success";
objectId = succinctProperties.getString("cmis:objectId");
mimeType = succinctProperties.getString("cmis:contentStreamMimeType");
} else {
if (responseCode == 409) {
JSONObject jsonResponse = new JSONObject(responseString);
Expand Down Expand Up @@ -339,6 +340,7 @@ private void formResponse(
finalResponse.put("message", error);
if (!objectId.isEmpty()) {
finalResponse.put("objectId", objectId);
finalResponse.put("mimeType", mimeType);
}
} catch (IOException e) {
throw new ServiceException(SDMConstants.getGenericError("upload"));
Expand Down
5 changes: 2 additions & 3 deletions sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sap.cds.Result;
import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext;
import com.sap.cds.sdm.model.CmisDocument;
import com.sap.cds.sdm.model.RepoValue;
import com.sap.cds.sdm.model.SDMCredentials;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.persistence.PersistenceService;
Expand All @@ -27,12 +28,10 @@ public String getFolderIdByPath(
String parentId, String repositoryId, SDMCredentials sdmCredentials, boolean isSystemUser)
throws IOException;

public String checkRepositoryType(String repositoryId, String tenant) throws IOException;
public RepoValue checkRepositoryType(String repositoryId, String tenant) throws IOException;

public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) throws IOException;

public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) throws IOException;

public int deleteDocument(String cmisaction, String objectId, String user) throws IOException;

public void readDocument(
Expand Down
58 changes: 28 additions & 30 deletions sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@

import com.sap.cds.Result;
import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext;
import com.sap.cds.sdm.caching.CacheConfig;
import com.sap.cds.sdm.caching.RepoKey;
import com.sap.cds.sdm.caching.SecondaryPropertiesKey;
import com.sap.cds.sdm.caching.SecondaryTypesKey;
import com.sap.cds.sdm.caching.*;
import com.sap.cds.sdm.constants.SDMConstants;
import com.sap.cds.sdm.handler.TokenHandler;
import com.sap.cds.sdm.model.CmisDocument;
import com.sap.cds.sdm.model.RepoValue;
import com.sap.cds.sdm.model.SDMCredentials;
import com.sap.cds.sdm.utilities.SDMUtils;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.environment.CdsProperties;
import com.sap.cds.services.persistence.PersistenceService;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.sdk.cloudplatform.connectivity.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -480,33 +477,23 @@ else if (responseCode == 403) {
}

@Override
public String checkRepositoryType(String repositoryId, String tenant) {
public RepoValue checkRepositoryType(String repositoryId, String tenant) {
RepoKey repoKey = new RepoKey();
repoKey.setSubdomain(tenant);
repoKey.setRepoId(repositoryId);
String type = CacheConfig.getVersionedRepoCache().get(repoKey);
Boolean isVersioned;
if (type == null) {
RepoValue repoValue = CacheConfig.getRepoCache().get(repoKey);
if (repoValue == null) {
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
JSONObject repoInfo = getRepositoryInfo(sdmCredentials);
isVersioned = isRepositoryVersioned(repoInfo, repositoryId);
} else {
isVersioned = "Versioned".equals(type);
}

if (Boolean.TRUE.equals(isVersioned)) {
repoKey = new RepoKey();
repoKey.setSubdomain(tenant);
repoKey.setRepoId(repositoryId);
CacheConfig.getVersionedRepoCache().put(repoKey, "Versioned");
return "Versioned";
} else {
Map<String, RepoValue> repoValueMap = fetchRepositoryData(repoInfo, repositoryId);
repoKey = new RepoKey();
repoKey.setSubdomain(tenant);
repoKey.setRepoId(repositoryId);
CacheConfig.getVersionedRepoCache().put(repoKey, "Non Versioned");
return "Non Versioned";
RepoValue value = repoValueMap.get(repositoryId);
CacheConfig.getRepoCache().put(repoKey, value);
return repoValueMap.get(repositoryId);
}
return repoValue;
}

public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) {
Expand All @@ -526,17 +513,28 @@ public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) {
}
}

public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) {
public Map<String, RepoValue> fetchRepositoryData(JSONObject repoInfo, String repositoryId) {
Map<String, RepoValue> repoValueMap = new HashMap<>();
repoInfo = repoInfo.getJSONObject(repositoryId);
JSONObject capabilities = repoInfo.getJSONObject("capabilities");
String type = capabilities.getString("capabilityContentStreamUpdatability");
if ("pwconly".equals(type)) {
type = "Versioned";
} else {
type = "Non Versioned";
RepoValue repoValue = new RepoValue();
repoValue.setVersionEnabled("pwconly".equals(type) ? true : false);
JSONArray extendedFeaturesArray = repoInfo.getJSONArray("extendedFeatures");
// Iterate over the array and find the object with featureData
for (int i = 0; i < extendedFeaturesArray.length(); i++) {
JSONObject feature = extendedFeaturesArray.getJSONObject(i);
if (feature.has("featureData")) {
JSONObject featureData = feature.getJSONObject("featureData");
// Fetch the 'virusScanner' value
repoValue.setVirusScanEnabled(featureData.getBoolean("virusScanner"));
// Fetch the disableVirusScannerForLargeFile
repoValue.setDisableVirusScannerForLargeFile(
featureData.getBoolean("disableVirusScannerForLargeFile"));
}
}

return "Versioned".equals(type);
repoValueMap.put(repositoryId, repoValue);
return repoValueMap;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sap.cds.sdm.service.handler;

import static com.sap.cds.sdm.persistence.DBQuery.*;

import com.sap.cds.Result;
import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.MediaData;
import com.sap.cds.feature.attachments.service.AttachmentService;
Expand All @@ -13,6 +11,7 @@
import com.sap.cds.sdm.constants.SDMConstants;
import com.sap.cds.sdm.handler.TokenHandler;
import com.sap.cds.sdm.model.CmisDocument;
import com.sap.cds.sdm.model.RepoValue;
import com.sap.cds.sdm.model.SDMCredentials;
import com.sap.cds.sdm.persistence.DBQuery;
import com.sap.cds.sdm.service.DocumentUploadService;
Expand Down Expand Up @@ -143,15 +142,24 @@ private boolean isObjectIdPresent(List<CmisDocument> documents, String objectId)
private void validateRepository(AttachmentCreateEventContext eventContext)
throws ServiceException, IOException {
String repositoryId = SDMConstants.REPOSITORY_ID;
String repocheck =
RepoValue repoValue =
sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant());
if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) {
if (repoValue.getVersionEnabled()) {
throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR);
}
String len = eventContext.getParameterInfo().getHeaders().get("content-length");
long contentLen = !StringUtils.isEmpty(len) ? Long.parseLong(len) : -1;
// Check if repository is virus scanned
if (repoValue.getVirusScanEnabled()
&& contentLen > 400 * 1024 * 1024
&& !repoValue.getDisableVirusScannerForLargeFile()) {
throw new ServiceException(SDMConstants.VIRUS_REPO_ERROR_MORE_THAN_400MB);
}
}

private void processEntities(AttachmentCreateEventContext eventContext)
throws ServiceException, IOException {

Map<String, Object> attachmentIds = eventContext.getAttachmentIds();
CdsEntity attachmentDraftEntity = getAttachmentDraftEntity(eventContext);
String upIdKey = getUpIdKey(attachmentDraftEntity);
Expand Down Expand Up @@ -297,6 +305,7 @@ private void handleCreateDocumentResult(
throw new ServiceException(SDMConstants.MIMETYPE_INVALID_ERROR);
default:
cmisDocument.setObjectId(createResult.get("objectId").toString());
cmisDocument.setMimeType(createResult.get("mimeType").toString());
dbQuery.addAttachmentToDraft(
getAttachmentDraftEntity(eventContext), persistenceService, cmisDocument);
finalizeContext(eventContext, cmisDocument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.sdm.constants.SDMConstants;
import com.sap.cds.sdm.handler.TokenHandler;
import com.sap.cds.sdm.model.AttachmentReadContext;
import com.sap.cds.sdm.model.CmisDocument;
import com.sap.cds.sdm.model.CopyAttachmentInput;
import com.sap.cds.sdm.model.SDMCredentials;
import com.sap.cds.sdm.model.*;
import com.sap.cds.sdm.persistence.DBQuery;
import com.sap.cds.sdm.service.DocumentUploadService;
import com.sap.cds.sdm.service.RegisterService;
Expand Down Expand Up @@ -118,9 +115,9 @@ public void openAttachment(AttachmentReadContext context) throws Exception {

private void validateRepository(EventContext eventContext) throws ServiceException, IOException {
String repositoryId = SDMConstants.REPOSITORY_ID;
String repocheck =
RepoValue repoValue =
sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant());
if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) {
if (repoValue.getVersionEnabled()) {
throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR);
}
}
Expand Down
Loading
Loading