|
54 | 54 | @ServiceName(value = "*", type = ApplicationService.class) |
55 | 55 | public class ReadAttachmentsHandler implements EventHandler { |
56 | 56 |
|
57 | | - private static final Logger logger = LoggerFactory.getLogger(ReadAttachmentsHandler.class); |
58 | | - |
59 | | - private final AttachmentService attachmentService; |
60 | | - private final AttachmentStatusValidator statusValidator; |
61 | | - private final AsyncMalwareScanExecutor scanExecutor; |
62 | | - |
63 | | - public ReadAttachmentsHandler( |
64 | | - AttachmentService attachmentService, |
65 | | - AttachmentStatusValidator statusValidator, |
66 | | - AsyncMalwareScanExecutor scanExecutor) { |
67 | | - this.attachmentService = |
68 | | - requireNonNull(attachmentService, "attachmentService must not be null"); |
69 | | - this.statusValidator = requireNonNull(statusValidator, "statusValidator must not be null"); |
70 | | - this.scanExecutor = requireNonNull(scanExecutor, "scanExecutor must not be null"); |
| 57 | + private static final Logger logger = LoggerFactory.getLogger(ReadAttachmentsHandler.class); |
| 58 | + |
| 59 | + private final AttachmentService attachmentService; |
| 60 | + private final AttachmentStatusValidator statusValidator; |
| 61 | + private final AsyncMalwareScanExecutor scanExecutor; |
| 62 | + |
| 63 | + public ReadAttachmentsHandler( |
| 64 | + AttachmentService attachmentService, |
| 65 | + AttachmentStatusValidator statusValidator, |
| 66 | + AsyncMalwareScanExecutor scanExecutor) { |
| 67 | + this.attachmentService = |
| 68 | + requireNonNull(attachmentService, "attachmentService must not be null"); |
| 69 | + this.statusValidator = requireNonNull(statusValidator, "statusValidator must not be null"); |
| 70 | + this.scanExecutor = requireNonNull(scanExecutor, "scanExecutor must not be null"); |
| 71 | + } |
| 72 | + |
| 73 | + @Before |
| 74 | + @HandlerOrder(HandlerOrder.EARLY) |
| 75 | + void processBefore(CdsReadEventContext context) { |
| 76 | + logger.debug("Processing before {} for entity {}.", context.getEvent(), context.getTarget()); |
| 77 | + |
| 78 | + CdsModel cdsModel = context.getModel(); |
| 79 | + List<String> fieldNames = |
| 80 | + getAttachmentAssociations(cdsModel, context.getTarget(), "", new ArrayList<>()); |
| 81 | + if (!fieldNames.isEmpty()) { |
| 82 | + CqnSelect resultCqn = CQL.copy(context.getCqn(), new BeforeReadItemsModifier(fieldNames)); |
| 83 | + context.setCqn(resultCqn); |
71 | 84 | } |
72 | | - |
73 | | - @Before |
74 | | - @HandlerOrder(HandlerOrder.EARLY) |
75 | | - void processBefore(CdsReadEventContext context) { |
76 | | - logger.debug("Processing before {} for entity {}.", context.getEvent(), context.getTarget()); |
77 | | - |
78 | | - CdsModel cdsModel = context.getModel(); |
79 | | - List<String> fieldNames = |
80 | | - getAttachmentAssociations(cdsModel, context.getTarget(), "", new ArrayList<>()); |
81 | | - if (!fieldNames.isEmpty()) { |
82 | | - CqnSelect resultCqn = CQL.copy(context.getCqn(), new BeforeReadItemsModifier(fieldNames)); |
83 | | - context.setCqn(resultCqn); |
84 | | - } |
| 85 | + } |
| 86 | + |
| 87 | + @After |
| 88 | + @HandlerOrder(HandlerOrder.EARLY) |
| 89 | + void processAfter(CdsReadEventContext context, List<CdsData> data) { |
| 90 | + if (ApplicationHandlerHelper.containsContentField(context.getTarget(), data)) { |
| 91 | + logger.debug( |
| 92 | + "Processing after {} event for entity {}", context.getEvent(), context.getTarget()); |
| 93 | + |
| 94 | + Converter converter = |
| 95 | + (path, element, value) -> { |
| 96 | + Attachments attachment = Attachments.of(path.target().values()); |
| 97 | + InputStream content = attachment.getContent(); |
| 98 | + if (nonNull(attachment.getContentId())) { |
| 99 | + verifyStatus(path, attachment); |
| 100 | + Supplier<InputStream> supplier = |
| 101 | + nonNull(content) |
| 102 | + ? () -> content |
| 103 | + : () -> attachmentService.readAttachment(attachment.getContentId()); |
| 104 | + return new LazyProxyInputStream(supplier, statusValidator, attachment.getStatus()); |
| 105 | + } else { |
| 106 | + return value; |
| 107 | + } |
| 108 | + }; |
| 109 | + CdsDataProcessor.create() |
| 110 | + .addConverter(ApplicationHandlerHelper.MEDIA_CONTENT_FILTER, converter) |
| 111 | + .process(data, context.getTarget()); |
85 | 112 | } |
| 113 | + } |
86 | 114 |
|
87 | | - @After |
88 | | - @HandlerOrder(HandlerOrder.EARLY) |
89 | | - void processAfter(CdsReadEventContext context, List<CdsData> data) { |
90 | | - if (ApplicationHandlerHelper.containsContentField(context.getTarget(), data)) { |
91 | | - logger.debug( |
92 | | - "Processing after {} event for entity {}", context.getEvent(), context.getTarget()); |
93 | | - |
94 | | - Converter converter = |
95 | | - (path, element, value) -> { |
96 | | - Attachments attachment = Attachments.of(path.target().values()); |
97 | | - InputStream content = attachment.getContent(); |
98 | | - if (nonNull(attachment.getContentId())) { |
99 | | - verifyStatus(path, attachment); |
100 | | - Supplier<InputStream> supplier = |
101 | | - nonNull(content) |
102 | | - ? () -> content |
103 | | - : () -> attachmentService.readAttachment(attachment.getContentId()); |
104 | | - return new LazyProxyInputStream(supplier, statusValidator, attachment.getStatus()); |
105 | | - } else { |
106 | | - return value; |
107 | | - } |
108 | | - }; |
109 | | - CdsDataProcessor.create() |
110 | | - .addConverter(ApplicationHandlerHelper.MEDIA_CONTENT_FILTER, converter) |
111 | | - .process(data, context.getTarget()); |
112 | | - } |
| 115 | + private List<String> getAttachmentAssociations( |
| 116 | + CdsModel model, CdsEntity entity, String associationName, List<String> processedEntities) { |
| 117 | + List<String> associationNames = new ArrayList<>(); |
| 118 | + if (ApplicationHandlerHelper.isMediaEntity(entity)) { |
| 119 | + associationNames.add(associationName); |
113 | 120 | } |
114 | 121 |
|
115 | | - private List<String> getAttachmentAssociations( |
116 | | - CdsModel model, CdsEntity entity, String associationName, List<String> processedEntities) { |
117 | | - List<String> associationNames = new ArrayList<>(); |
118 | | - if (ApplicationHandlerHelper.isMediaEntity(entity)) { |
119 | | - associationNames.add(associationName); |
120 | | - } |
121 | | - |
122 | | - Map<String, CdsEntity> annotatedEntities = |
123 | | - entity |
124 | | - .associations() |
125 | | - .collect( |
126 | | - Collectors.toMap( |
127 | | - CdsElementDefinition::getName, |
128 | | - element -> element.getType().as(CdsAssociationType.class).getTarget())); |
129 | | - |
130 | | - if (annotatedEntities.isEmpty()) { |
131 | | - return associationNames; |
132 | | - } |
133 | | - |
134 | | - for (Entry<String, CdsEntity> associatedElement : annotatedEntities.entrySet()) { |
135 | | - if (!associationNames.contains(associatedElement.getKey()) |
136 | | - && !processedEntities.contains(associatedElement.getKey()) |
137 | | - && !Drafts.SIBLING_ENTITY.equals(associatedElement.getKey())) { |
138 | | - processedEntities.add(associatedElement.getKey()); |
139 | | - List<String> result = |
140 | | - getAttachmentAssociations( |
141 | | - model, associatedElement.getValue(), associatedElement.getKey(), processedEntities); |
142 | | - associationNames.addAll(result); |
143 | | - } |
144 | | - } |
145 | | - return associationNames; |
146 | | - } |
| 122 | + Map<String, CdsEntity> annotatedEntities = |
| 123 | + entity |
| 124 | + .associations() |
| 125 | + .collect( |
| 126 | + Collectors.toMap( |
| 127 | + CdsElementDefinition::getName, |
| 128 | + element -> element.getType().as(CdsAssociationType.class).getTarget())); |
147 | 129 |
|
148 | | - private void verifyStatus(Path path, Attachments attachment) { |
149 | | - if (areKeysEmpty(path.target().keys())) { |
150 | | - String currentStatus = attachment.getStatus(); |
151 | | - logger.debug( |
152 | | - "In verify status for content id {} and status {}", |
153 | | - attachment.getContentId(), |
154 | | - currentStatus); |
155 | | - if (StatusCode.UNSCANNED.equals(currentStatus) |
156 | | - || StatusCode.SCANNING.equals(currentStatus) |
157 | | - || currentStatus == null) { |
158 | | - logger.debug( |
159 | | - "Scanning content with ID {} for malware, has current status {}", |
160 | | - attachment.getContentId(), |
161 | | - currentStatus); |
162 | | - scanExecutor.scanAsync(path.target().entity(), attachment.getContentId()); |
163 | | - } |
164 | | - statusValidator.verifyStatus(attachment.getStatus()); |
165 | | - } |
| 130 | + if (annotatedEntities.isEmpty()) { |
| 131 | + return associationNames; |
166 | 132 | } |
167 | 133 |
|
168 | | - private boolean areKeysEmpty(Map<String, Object> keys) { |
169 | | - return keys.values().stream().allMatch(Objects::isNull); |
| 134 | + for (Entry<String, CdsEntity> associatedElement : annotatedEntities.entrySet()) { |
| 135 | + if (!associationNames.contains(associatedElement.getKey()) |
| 136 | + && !processedEntities.contains(associatedElement.getKey()) |
| 137 | + && !Drafts.SIBLING_ENTITY.equals(associatedElement.getKey())) { |
| 138 | + processedEntities.add(associatedElement.getKey()); |
| 139 | + List<String> result = |
| 140 | + getAttachmentAssociations( |
| 141 | + model, associatedElement.getValue(), associatedElement.getKey(), processedEntities); |
| 142 | + associationNames.addAll(result); |
| 143 | + } |
| 144 | + } |
| 145 | + return associationNames; |
| 146 | + } |
| 147 | + |
| 148 | + private void verifyStatus(Path path, Attachments attachment) { |
| 149 | + if (areKeysEmpty(path.target().keys())) { |
| 150 | + String currentStatus = attachment.getStatus(); |
| 151 | + logger.debug( |
| 152 | + "In verify status for content id {} and status {}", |
| 153 | + attachment.getContentId(), |
| 154 | + currentStatus); |
| 155 | + if (StatusCode.UNSCANNED.equals(currentStatus) |
| 156 | + || StatusCode.SCANNING.equals(currentStatus) |
| 157 | + || currentStatus == null) { |
| 158 | + logger.debug( |
| 159 | + "Scanning content with ID {} for malware, has current status {}", |
| 160 | + attachment.getContentId(), |
| 161 | + currentStatus); |
| 162 | + scanExecutor.scanAsync(path.target().entity(), attachment.getContentId()); |
| 163 | + } |
| 164 | + statusValidator.verifyStatus(attachment.getStatus()); |
170 | 165 | } |
| 166 | + } |
| 167 | + |
| 168 | + private boolean areKeysEmpty(Map<String, Object> keys) { |
| 169 | + return keys.values().stream().allMatch(Objects::isNull); |
| 170 | + } |
171 | 171 | } |
0 commit comments