-
Notifications
You must be signed in to change notification settings - Fork 6
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
Draft
samyuktaprabhu
wants to merge
14
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.
+1,996
−53
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e98f840
single commit
samyuktaprabhu 3422412
multi attachments
samyuktaprabhu 4cf1c1d
solve bot reiew comments
samyuktaprabhu 9e94a58
apply formatting
samyuktaprabhu 29f1455
add comments
samyuktaprabhu 1315776
code formatting
samyuktaprabhu 5dfdeb4
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/atta…
samyuktaprabhu 737ff6e
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/atta…
samyuktaprabhu 7dd69c6
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/atta…
samyuktaprabhu 7752cff
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/atta…
samyuktaprabhu 62a0880
apply mvnspotless:apply
samyuktaprabhu 3e560dc
Rename AttachmentDataExtractor to validation package
samyuktaprabhu 59c8fd6
Refactor MediaTypeService to use URLConnection for MIME type resoluti…
samyuktaprabhu 0326206
Refactor AttachmentValidationHelper to streamline MIME type validatio…
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
77 changes: 77 additions & 0 deletions
77
...ap/cds/feature/attachments/handler/applicationservice/helper/media/MediaTypeResolver.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,77 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice.helper.media; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.sap.cds.feature.attachments.handler.applicationservice.helper.validation.AttachmentValidationHelper; | ||
| import com.sap.cds.feature.attachments.handler.common.ApplicationHandlerHelper; | ||
| import com.sap.cds.reflect.CdsAnnotation; | ||
| import com.sap.cds.reflect.CdsAssociationType; | ||
| import com.sap.cds.reflect.CdsEntity; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public final class MediaTypeResolver { | ||
| private static final String CONTENT_ELEMENT = "content"; | ||
| private static final String ACCEPTABLE_MEDIA_TYPES_ANNOTATION = "Core.AcceptableMediaTypes"; | ||
| private static final TypeReference<List<String>> STRING_LIST_TYPE_REF = new TypeReference<>() {}; | ||
| private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| /** | ||
| * Resolves the acceptable media (MIME) types for the given {@link CdsEntity}. | ||
| * | ||
| * <p>The method behaves differently depending on whether the provided entity itself is a media | ||
| * entity or a root entity containing compositions: | ||
| * | ||
| * <p>If no media entities are found (neither the root nor its composition targets), an empty map | ||
| * is returned. | ||
| * | ||
| * @param entity the CDS entity to inspect (root or media entity) | ||
| * @return a map of entity qualified names to their allowed media types; empty if no media | ||
| * entities are found | ||
| */ | ||
| public static Map<String, List<String>> getAcceptableMediaTypesFromEntity(CdsEntity entity) { | ||
| // If this entity is a media entity | ||
| if (ApplicationHandlerHelper.isMediaEntity(entity)) { | ||
| return Map.of(entity.getQualifiedName(), fetchAcceptableMediaTypes(entity)); | ||
| } | ||
|
|
||
| // If it's not a mediaEntity, if it's a root entity | ||
| Map<String, List<String>> result = new HashMap<>(); | ||
| entity | ||
| .elements() | ||
| .filter(e -> e.getType().isAssociation()) | ||
| .map(e -> e.getType().as(CdsAssociationType.class)) | ||
| .filter(CdsAssociationType::isComposition) | ||
| .forEach( | ||
| association -> { | ||
| CdsEntity target = association.getTarget(); | ||
| if (target != null && ApplicationHandlerHelper.isMediaEntity(target)) { | ||
| result.put(target.getQualifiedName(), fetchAcceptableMediaTypes(target)); | ||
| } | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static List<String> fetchAcceptableMediaTypes(CdsEntity entity) { | ||
| return getAcceptableMediaTypesAnnotation(entity) | ||
| .map(CdsAnnotation::getValue) | ||
| .map(value -> objectMapper.convertValue(value, STRING_LIST_TYPE_REF)) | ||
| .orElse(AttachmentValidationHelper.WILDCARD_MEDIA_TYPE); | ||
| } | ||
|
|
||
| public static Optional<CdsAnnotation<Object>> getAcceptableMediaTypesAnnotation( | ||
| CdsEntity entity) { | ||
| return Optional.ofNullable(entity.getElement(CONTENT_ELEMENT)) | ||
| .flatMap(element -> element.findAnnotation(ACCEPTABLE_MEDIA_TYPES_ANNOTATION)); | ||
| } | ||
|
|
||
| private MediaTypeResolver() { | ||
| // to prevent instantiation | ||
| } | ||
| } |
85 changes: 85 additions & 0 deletions
85
...sap/cds/feature/attachments/handler/applicationservice/helper/media/MediaTypeService.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,85 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice.helper.media; | ||
|
|
||
| import com.sap.cds.services.ErrorStatuses; | ||
| import com.sap.cds.services.ServiceException; | ||
| import java.net.FileNameMap; | ||
| import java.net.URLConnection; | ||
| import java.util.Collection; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public final class MediaTypeService { | ||
| private static final Logger logger = LoggerFactory.getLogger(MediaTypeService.class); | ||
| public static final String DEFAULT_MEDIA_TYPE = "application/octet-stream"; | ||
|
|
||
| /** | ||
| * Resolves the MIME type of a file based on its filename (specifically its extension). | ||
| * | ||
| * @param fileName the name of the file (including extension) | ||
| * @return the resolved MIME type, or a default MIME type if it cannot be determined | ||
| * @throws ServiceException if the filename is null or blank | ||
| */ | ||
| public static String resolveMimeType(String fileName) { | ||
| if (fileName == null || fileName.isBlank()) { | ||
| throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename is missing"); | ||
| } | ||
|
|
||
| int lastDotIndex = fileName.lastIndexOf('.'); | ||
| if (lastDotIndex == -1 || lastDotIndex == fileName.length() - 1) { | ||
| return fallbackToDefaultMimeType(fileName); | ||
| } | ||
|
|
||
| FileNameMap fileNameMap = URLConnection.getFileNameMap(); | ||
| String actualMimeType = fileNameMap.getContentTypeFor(fileName); | ||
|
|
||
| if (actualMimeType == null) { | ||
| return fallbackToDefaultMimeType(fileName); | ||
| } | ||
| return actualMimeType; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a given MIME type is allowed based on a collection of acceptable media | ||
| * | ||
| * @param acceptableMediaTypes | ||
| * @param mimeType | ||
| * @return | ||
| */ | ||
| public static boolean isMimeTypeAllowed( | ||
| Collection<String> acceptableMediaTypes, String mimeType) { | ||
| if (mimeType == null) { | ||
| return false; | ||
| } | ||
|
|
||
| if (acceptableMediaTypes == null | ||
| || acceptableMediaTypes.isEmpty() | ||
| || acceptableMediaTypes.contains("*/*")) return true; | ||
|
|
||
| String baseMimeType = mimeType.trim().toLowerCase(); | ||
| Collection<String> normalizedTypes = | ||
| acceptableMediaTypes.stream().map(type -> type.trim().toLowerCase()).toList(); | ||
|
|
||
| return normalizedTypes.stream() | ||
| .anyMatch( | ||
| type -> { | ||
| return type.endsWith("/*") | ||
| ? baseMimeType.startsWith(type.substring(0, type.length() - 1)) | ||
| : baseMimeType.equals(type); | ||
| }); | ||
| } | ||
|
|
||
| private static String fallbackToDefaultMimeType(String fileName) { | ||
| logger.warn( | ||
| "Could not determine mime type for file: {}. Setting mime type to default: {}", | ||
| fileName, | ||
| DEFAULT_MEDIA_TYPE); | ||
| return DEFAULT_MEDIA_TYPE; | ||
| } | ||
|
|
||
| private MediaTypeService() { | ||
| // 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.