66import com .sap .cds .CdsData ;
77import com .sap .cds .CdsDataProcessor ;
88import com .sap .cds .CdsDataProcessor .Converter ;
9- import com .sap .cds .CdsDataProcessor .Filter ;
109import com .sap .cds .feature .attachments .generated .cds4j .sap .attachments .Attachments ;
1110import com .sap .cds .feature .attachments .handler .applicationservice .modifyevents .ModifyAttachmentEvent ;
1211import com .sap .cds .feature .attachments .handler .applicationservice .modifyevents .ModifyAttachmentEventFactory ;
1312import com .sap .cds .feature .attachments .handler .applicationservice .readhelper .CountingInputStream ;
1413import com .sap .cds .feature .attachments .handler .common .ApplicationHandlerHelper ;
1514import com .sap .cds .ql .cqn .Path ;
15+ import com .sap .cds .reflect .CdsAnnotation ;
1616import com .sap .cds .reflect .CdsEntity ;
1717import com .sap .cds .services .ErrorStatuses ;
1818import com .sap .cds .services .EventContext ;
1919import com .sap .cds .services .ServiceException ;
2020import java .io .InputStream ;
2121import java .util .List ;
2222import java .util .Map ;
23- import java .util .concurrent .atomic .AtomicReference ;
2423
2524public final class ModifyApplicationHandlerHelper {
2625
@@ -30,11 +29,6 @@ public final class ModifyApplicationHandlerHelper {
3029 /** Effectively unlimited max size when no malware scanner binding is present. */
3130 public static final String UNLIMITED_SIZE = String .valueOf (Long .MAX_VALUE );
3231
33- private static final Filter VALMAX_FILTER =
34- (path , element , type ) ->
35- element .getName ().contentEquals ("content" )
36- && element .findAnnotation ("Validation.Maximum" ).isPresent ();
37-
3832 /**
3933 * Handles attachments for entities.
4034 *
@@ -94,7 +88,7 @@ public static InputStream handleAttachmentForEntity(
9488 Attachments attachment = getExistingAttachment (keys , existingAttachments );
9589 String contentId = (String ) path .target ().values ().get (Attachments .CONTENT_ID );
9690 String contentLength = eventContext .getParameterInfo ().getHeader ("Content-Length" );
97- String maxSizeStr = getValMaxValue (path .target ().entity (), existingAttachments , defaultMaxSize );
91+ String maxSizeStr = getValMaxValue (path .target ().entity (), defaultMaxSize );
9892 eventContext .put (
9993 "attachment.MaxSize" ,
10094 maxSizeStr ); // make max size available in context for error handling later
@@ -125,23 +119,14 @@ public static InputStream handleAttachmentForEntity(
125119 }
126120 }
127121
128- private static String getValMaxValue (
129- CdsEntity entity , List <? extends CdsData > data , String defaultMaxSize ) {
130- AtomicReference <String > annotationValue = new AtomicReference <>();
131- CdsDataProcessor .create ()
132- .addValidator (
133- VALMAX_FILTER ,
134- (path , element , value ) ->
135- element
136- .findAnnotation ("Validation.Maximum" )
137- .ifPresent (
138- annotation -> {
139- if (annotation .getValue () != null && annotation .getValue () != "true" ) {
140- annotationValue .set (annotation .getValue ().toString ());
141- }
142- }))
143- .process (data , entity );
144- return annotationValue .get () == null ? defaultMaxSize : annotationValue .get ();
122+ private static String getValMaxValue (CdsEntity entity , String defaultMaxSize ) {
123+ return entity
124+ .findElement ("content" )
125+ .flatMap (e -> e .findAnnotation ("Validation.Maximum" ))
126+ .map (CdsAnnotation ::getValue )
127+ .filter (v -> !"true" .equals (v .toString ()))
128+ .map (Object ::toString )
129+ .orElse (defaultMaxSize );
145130 }
146131
147132 private static Attachments getExistingAttachment (
0 commit comments