-
Notifications
You must be signed in to change notification settings - Fork 7
Add support to restrict @Core.AcceptableMediaTypes #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samyuktaprabhu
wants to merge
8
commits into
main
Choose a base branch
from
sam-restrict-media-types-409
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63a7e88
single commit
samyuktaprabhu aa31200
multi attachments
samyuktaprabhu 9e26aaf
solve bot reiew comments
samyuktaprabhu 99d9742
apply formatting
samyuktaprabhu 5a8add8
add comments
samyuktaprabhu 2c24ef3
code formatting
samyuktaprabhu 4c34db9
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/atta…
samyuktaprabhu bbe590f
PR Review fixes - I
samyuktaprabhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
...chments/handler/applicationservice/helper/mimeTypeValidation/AttachmentDataExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice.helper.mimeTypeValidation; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.CdsDataProcessor; | ||
| import com.sap.cds.CdsDataProcessor.Filter; | ||
| import com.sap.cds.CdsDataProcessor.Validator; | ||
| import com.sap.cds.feature.attachments.handler.common.ApplicationHandlerHelper; | ||
| import com.sap.cds.reflect.CdsAssociationType; | ||
| import com.sap.cds.reflect.CdsElement; | ||
| import com.sap.cds.reflect.CdsEntity; | ||
| import com.sap.cds.services.ErrorStatuses; | ||
| import com.sap.cds.services.ServiceException; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public final class AttachmentDataExtractor { | ||
| private static final String FILE_NAME_FIELD = "fileName"; | ||
| public static final Filter FILE_NAME_FILTER = | ||
| (path, element, type) -> element.getName().contentEquals(FILE_NAME_FIELD); | ||
|
|
||
| /** | ||
| * Extracts and validates file names of attachments from the given entity data. | ||
| * | ||
| * @param entity the CDS entity definition | ||
| * @param data the incoming data containing attachment values | ||
| * @return a map of element names to sets of associated file names | ||
| */ | ||
| public static Map<String, Set<String>> extractAndValidateFileNamesByElement( | ||
| CdsEntity entity, List<? extends CdsData> data) { | ||
| // Collects file names from attachment-related elements in the entity | ||
| Map<String, Set<String>> fileNamesByElementName = collectFileNamesByElementName(entity, data); | ||
| // Ensures that all attachments have valid (non-null, non-empty) file names. | ||
| ensureAttachmentsHaveFileNames(entity, data, fileNamesByElementName); | ||
| return fileNamesByElementName; | ||
| } | ||
|
|
||
| private static Map<String, Set<String>> collectFileNamesByElementName( | ||
| CdsEntity entity, List<? extends CdsData> data) { | ||
| // Use CdsProcessor to traverse the data and collect file names for elements | ||
| // named "fileName" | ||
| Map<String, Set<String>> fileNamesByElementName = new HashMap<>(); | ||
| CdsDataProcessor processor = CdsDataProcessor.create(); | ||
| Validator fileNameValidator = generateFileNameFieldValidator(fileNamesByElementName); | ||
| processor.addValidator(FILE_NAME_FILTER, fileNameValidator).process(data, entity); | ||
| return fileNamesByElementName; | ||
| } | ||
|
|
||
| private static Validator generateFileNameFieldValidator(Map<String, Set<String>> result) { | ||
| Validator validator = | ||
| (path, element, value) -> { | ||
| String fileName = requireString(value); | ||
| String normalizedFileName = validateAndNormalize(fileName); | ||
| String key = element.getDeclaringType().getQualifiedName(); | ||
| result.computeIfAbsent(key, k -> new HashSet<>()).add(normalizedFileName); | ||
| }; | ||
| return validator; | ||
| } | ||
|
|
||
| private static String validateAndNormalize(String fileName) { | ||
| String trimmedFileName = fileName.trim(); | ||
| if (trimmedFileName.isEmpty()) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename must not be blank"); | ||
| } | ||
|
|
||
| if (trimmedFileName.endsWith(".")) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Invalid filename format: " + fileName); | ||
| } | ||
|
|
||
| return trimmedFileName; | ||
| } | ||
|
|
||
| private static void ensureAttachmentsHaveFileNames( | ||
| CdsEntity entity, List<? extends CdsData> data, Map<String, Set<String>> result) { | ||
| // Collect attachment-related elements/fields from the entity | ||
| List<CdsElement> attachmentElements = | ||
| entity | ||
| .elements() | ||
| .filter( | ||
| e -> { | ||
| // Only consider associations | ||
| if (!e.getType().isAssociation()) { | ||
| return false; | ||
| } | ||
| // Keep only associations targeting media entities | ||
| // that define acceptable media types | ||
| CdsAssociationType association = e.getType().as(CdsAssociationType.class); | ||
| return ApplicationHandlerHelper.isMediaEntity(association.getTarget()) | ||
| && MediaTypeResolver.getAcceptableMediaTypesAnnotation( | ||
| association.getTarget()) | ||
| .isPresent(); | ||
| }) | ||
| .toList(); | ||
|
|
||
| // Validate that required attachments have file names | ||
| ensureFilenamesPresent(data, result, attachmentElements); | ||
| } | ||
|
|
||
| private static void ensureFilenamesPresent( | ||
| List<? extends CdsData> data, | ||
| Map<String, Set<String>> result, | ||
| List<CdsElement> attachmentElements) { | ||
|
|
||
| // Extract keys of fields that actually contain data | ||
| Set<String> dataKeys = collectValidDataKeys(data); | ||
|
|
||
| // Check if any required attachment is missing a filename | ||
| boolean hasMissingFileName = hasMissingFileNames(result, attachmentElements, dataKeys); | ||
|
|
||
| // If any attachment is missing a filename, throw and exception | ||
| if (hasMissingFileName) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename is missing"); | ||
| } | ||
| } | ||
|
|
||
| private static boolean hasMissingFileNames( | ||
| Map<String, Set<String>> result, | ||
| List<CdsElement> availableAttachmentElements, | ||
| Set<String> dataKeys) { | ||
|
|
||
| return availableAttachmentElements.stream() | ||
| .filter(e -> dataKeys.contains(e.getName())) | ||
| .anyMatch( | ||
| element -> { | ||
| CdsAssociationType assoc = element.getType().as(CdsAssociationType.class); | ||
| String target = assoc.getTarget().getQualifiedName(); | ||
| Set<String> fileNames = result.get(target); | ||
| return fileNames == null || fileNames.isEmpty(); | ||
| }); | ||
| } | ||
|
|
||
| private static Set<String> collectValidDataKeys(List<? extends CdsData> data) { | ||
| return data.stream() | ||
| .flatMap(d -> d.entrySet().stream()) | ||
| .filter(entry -> !isEmpty(entry.getValue())) | ||
| .map(Map.Entry::getKey) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private static boolean isEmpty(Object value) { | ||
| return value == null | ||
| || (value instanceof String s && s.isBlank()) | ||
| || (value instanceof Collection<?> c && c.isEmpty()) | ||
| || (value instanceof Iterable<?> i && !i.iterator().hasNext()); | ||
| } | ||
|
|
||
| private static String requireString(Object value) { | ||
| if (value == null) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename is missing"); | ||
| } | ||
| if (!(value instanceof String s)) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename must be a string"); | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| private AttachmentDataExtractor() { | ||
| // Private constructor to prevent instantiation | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.