| 1 | package fr.sii.ogham.email.builder; | |
| 2 | ||
| 3 | import fr.sii.ogham.core.builder.Builder; | |
| 4 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 5 | import fr.sii.ogham.core.exception.builder.BuildException; | |
| 6 | import fr.sii.ogham.core.fluent.AbstractParent; | |
| 7 | import fr.sii.ogham.core.id.generator.IdGenerator; | |
| 8 | import fr.sii.ogham.email.message.Email; | |
| 9 | import fr.sii.ogham.html.inliner.EveryImageInliner; | |
| 10 | import fr.sii.ogham.html.inliner.ImageInliner; | |
| 11 | import fr.sii.ogham.html.inliner.impl.jsoup.JsoupAttachImageInliner; | |
| 12 | import fr.sii.ogham.html.inliner.impl.regexp.RegexAttachBackgroudImageInliner; | |
| 13 | ||
| 14 | /** | |
| 15 | * Configures how attachment of images is handled. | |
| 16 | * | |
| 17 | * Image defined in a html must be referenced by a | |
| 18 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID (or | |
| 19 | * CID)</a> if the image is attached to the email. | |
| 20 | * | |
| 21 | * For example, if your template contains the following HTML code: | |
| 22 | * | |
| 23 | * <pre> | |
| 24 | * {@code | |
| 25 | * <img src="classpath:/foo.png" data-inline-image="attach" /> | |
| 26 | * } | |
| 27 | * </pre> | |
| 28 | * | |
| 29 | * Then the image will be loaded from the classpath and attached to the email. | |
| 30 | * The src attribute will be replaced by the Content-ID. | |
| 31 | * | |
| 32 | * The Content-ID is generated using an {@link IdGenerator} that is configured | |
| 33 | * through the {@link #cid()} method. | |
| 34 | * | |
| 35 | * <p> | |
| 36 | * In the same way, if your template contains the following code: | |
| 37 | * | |
| 38 | * <pre> | |
| 39 | * <code> | |
| 40 | * <style> | |
| 41 | * .some-class { | |
| 42 | * background: url('classpath:/foo.png'); | |
| 43 | * --inline-image: attach; | |
| 44 | * } | |
| 45 | * </style> | |
| 46 | * </code> | |
| 47 | * </pre> | |
| 48 | * | |
| 49 | * Or directly on {@code style} attribute: | |
| 50 | * | |
| 51 | * <pre> | |
| 52 | * {@code | |
| 53 | * <div style="background: url('classpath:/foo.png'); --inline-image: attach;"></div> | |
| 54 | * } | |
| 55 | * </pre> | |
| 56 | * | |
| 57 | * Then the image will be loaded from the classpath and attached to the email. | |
| 58 | * The url will be replaced by the Content-ID. | |
| 59 | * | |
| 60 | * @author Aurélien Baudet | |
| 61 | * | |
| 62 | */ | |
| 63 | public class AttachImageBuilder extends AbstractParent<ImageInliningBuilder> implements Builder<ImageInliner> { | |
| 64 | private final BuildContext buildContext; | |
| 65 | private CidBuilder cidBuilder; | |
| 66 | ||
| 67 | /** | |
| 68 | * Initializes with the parent (used when calling {@link #and()} method for | |
| 69 | * fluent chaining). | |
| 70 | * | |
| 71 | * @param parent | |
| 72 | * the parent builder | |
| 73 | * @param buildContext | |
| 74 | * for registering instances and property evaluation | |
| 75 | */ | |
| 76 | public AttachImageBuilder(ImageInliningBuilder parent, BuildContext buildContext) { | |
| 77 | super(parent); | |
| 78 | this.buildContext = buildContext; | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Configures how images are attached to {@link Email}s. | |
| 83 | * | |
| 84 | * Image defined in a html must be referenced by a | |
| 85 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
| 86 | * (or CID)</a> if the image is attached to the email. You can define how | |
| 87 | * CIDs are generated. | |
| 88 | * | |
| 89 | * @return the builder to configure CID generation | |
| 90 | */ | |
| 91 | public CidBuilder cid() { | |
| 92 |
1
1. cid : negated conditional → RUN_ERROR |
if (cidBuilder == null) { |
| 93 | cidBuilder = new CidBuilder(this, buildContext); | |
| 94 | } | |
| 95 |
1
1. cid : replaced return value with null for fr/sii/ogham/email/builder/AttachImageBuilder::cid → RUN_ERROR |
return cidBuilder; |
| 96 | } | |
| 97 | ||
| 98 | @Override | |
| 99 | public ImageInliner build() { | |
| 100 |
1
1. build : negated conditional → RUN_ERROR |
if (cidBuilder == null) { |
| 101 | throw new BuildException("Can't build inliner that attaches images because no identifier generator configured"); | |
| 102 | } | |
| 103 | IdGenerator idGenerator = cidBuilder.build(); | |
| 104 |
1
1. build : negated conditional → RUN_ERROR |
if (idGenerator == null) { |
| 105 | throw new BuildException("Can't build inliner that attaches images because no identifier generator configured"); | |
| 106 | } | |
| 107 | EveryImageInliner inliner = buildContext.register(new EveryImageInliner()); | |
| 108 | inliner.addInliner(buildContext.register(new JsoupAttachImageInliner(idGenerator))); | |
| 109 | inliner.addInliner(buildContext.register(new RegexAttachBackgroudImageInliner(idGenerator))); | |
| 110 |
1
1. build : replaced return value with null for fr/sii/ogham/email/builder/AttachImageBuilder::build → RUN_ERROR |
return inliner; |
| 111 | } | |
| 112 | } | |
Mutations | ||
| 92 |
1.1 |
|
| 95 |
1.1 |
|
| 100 |
1.1 |
|
| 104 |
1.1 |
|
| 110 |
1.1 |