Wiki source code of Add an Attachment Validation Step
Last modified by Eleni Cojocariu on 2026/10/05 20:19
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | Ship a jar extension holding one component to add a check of your own to the [[attachment validation chain>>doc:documentation.xs.dev.attachments.validation-api.WebHome]]. | ||
| 2 | |||
| 3 | 1. Implement ##AttachmentValidationStep## as a singleton component with a hint of your own. The wrapper is everything the step is given: ##getSize()##, ##getFileName()## and ##getInputStream()##.((( | ||
| 4 | {{code language="java"}} | ||
| 5 | @Component | ||
| 6 | @Singleton | ||
| 7 | @Named("empty") | ||
| 8 | public class EmptyAttachmentValidationStep implements AttachmentValidationStep | ||
| 9 | { | ||
| 10 | @Override | ||
| 11 | public void validate(AttachmentAccessWrapper wrapper) throws AttachmentValidationException | ||
| 12 | { | ||
| 13 | // Refuse the attachment here. | ||
| 14 | } | ||
| 15 | } | ||
| 16 | {{/code}} | ||
| 17 | ))) | ||
| 18 | 1. Refuse an attachment by throwing an ##AttachmentValidationException## with its [[four payload fields>>doc:documentation.xs.dev.attachments.validation-api.WebHome]] filled in. A bare message gets the reader an untranslated rejection under the wrong status.((( | ||
| 19 | {{code language="java"}} | ||
| 20 | if (wrapper.getSize() == 0) { | ||
| 21 | throw new AttachmentValidationException( | ||
| 22 | String.format("Empty file [%s]", wrapper.getFileName()), | ||
| 23 | Response.Status.BAD_REQUEST.getStatusCode(), | ||
| 24 | "myextension.validation.empty.rejected", | ||
| 25 | List.of(wrapper.getFileName()), | ||
| 26 | "fileuploadisempty"); | ||
| 27 | } | ||
| 28 | {{/code}} | ||
| 29 | ))) | ||
| 30 | 1. Declare the component in the module's ##META-INF/components.txt##, one fully qualified class name per line and nothing else. The step then runs after the shipped ##size## and ##mimetype## steps, in [[an order you do not control>>doc:documentation.xs.dev.attachments.validation-api.WebHome]].((( | ||
| 31 | {{code language="none"}} | ||
| 32 | com.example.attachment.EmptyAttachmentValidationStep | ||
| 33 | {{/code}} | ||
| 34 | ))) | ||
| 35 | 1. Write the message of that translation key in ##ApplicationResources.properties##, at the root of the jar, where XWiki picks it up with no declaration of its own. Numbered placeholders take the parameters passed to the exception, in order.((( | ||
| 36 | {{code language="none"}} | ||
| 37 | myextension.validation.empty.rejected=The file {0} is empty. Please select another one. | ||
| 38 | {{/code}} | ||
| 39 | ))) | ||
| 40 | 1. Install the extension and attach an empty file: the upload is refused and your message is shown in place of the attachment. The same upload sent over REST answers the status you chose and the key for the client to translate.((( | ||
| 41 | {{code language="none"}} | ||
| 42 | HTTP/1.1 400 Bad Request | ||
| 43 | Content-Type: application/json | ||
| 44 | |||
| 45 | {"message": "Empty file [notes.txt]", | ||
| 46 | "translationKey": "myextension.validation.empty.rejected", | ||
| 47 | "translationParameters": ["notes.txt"]} | ||
| 48 | {{/code}} | ||
| 49 | ))) |