|
| 1 | +package org.telegram.telegrambots.client.jetty; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.eclipse.jetty.client.*; |
| 6 | +import org.eclipse.jetty.http.HttpFields; |
| 7 | +import org.eclipse.jetty.http.MultiPart; |
| 8 | +import org.telegram.telegrambots.meta.api.objects.InputFile; |
| 9 | +import org.telegram.telegrambots.meta.api.objects.media.InputMedia; |
| 10 | +import org.telegram.telegrambots.meta.api.objects.media.paid.InputPaidMedia; |
| 11 | +import org.telegram.telegrambots.meta.api.objects.stickers.InputSticker; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Valeriy Kucherenko |
| 18 | + * @since 05.09.2024 |
| 19 | + */ |
| 20 | +public class JettyMultipartBuilder { |
| 21 | + private final MultiPartRequestContent multiPart = new MultiPartRequestContent(); |
| 22 | + private final ObjectMapper mapper; |
| 23 | + |
| 24 | + public JettyMultipartBuilder(ObjectMapper mapper) { |
| 25 | + this.mapper = mapper; |
| 26 | + } |
| 27 | + |
| 28 | + public MultiPartRequestContent build() { |
| 29 | + multiPart.close(); |
| 30 | + return multiPart; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Add field to the builder if value is not null |
| 35 | + * @param fieldName the field name to add to the multipart |
| 36 | + * @param value the nullable value to add |
| 37 | + * @return the builder |
| 38 | + */ |
| 39 | + public JettyMultipartBuilder addPart(String fieldName, String value) { |
| 40 | + if (value != null) { |
| 41 | + multiPart.addPart(new MultiPart.ContentSourcePart(fieldName, null, HttpFields.EMPTY, new StringRequestContent(value))); |
| 42 | + } |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Add field to the builder if value is not null. The value is converted using toString() |
| 48 | + * @param fieldName the field name to add to the multipart |
| 49 | + * @param value the nullable value to add |
| 50 | + * @return the builder |
| 51 | + */ |
| 52 | + public JettyMultipartBuilder addPart(String fieldName, Object value) { |
| 53 | + if (value != null) { |
| 54 | + this.addPart(fieldName, value.toString()); |
| 55 | + } |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Add field to the builder if value is not null. The value is converted using ObjectMapper.writeValueAsString() |
| 61 | + * @param fieldName the field name to add to the multipart |
| 62 | + * @param value the nullable value to add |
| 63 | + * @return the builder |
| 64 | + */ |
| 65 | + public JettyMultipartBuilder addJsonPart(String fieldName, Object value) throws JsonProcessingException { |
| 66 | + if (value != null) { |
| 67 | + this.addPart(fieldName, mapper.writeValueAsString(value)); |
| 68 | + } |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public JettyMultipartBuilder addInputFile(String fileField, InputFile file, boolean addField) throws IOException { |
| 73 | + if (file == null) { |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + if (file.isNew()) { |
| 78 | + Request.Content body = null; |
| 79 | + if (file.getNewMediaFile() != null) { |
| 80 | + body = new PathRequestContent("application/octet-stream", file.getNewMediaFile().toPath()); |
| 81 | + } else if (file.getNewMediaStream() != null) { |
| 82 | + body = new InputStreamRequestContent("application/octet-stream", file.getNewMediaStream()); |
| 83 | + } |
| 84 | + if (body != null) { |
| 85 | + multiPart.addPart(new MultiPart.ContentSourcePart(file.getMediaName(), file.getMediaName(), HttpFields.EMPTY, body)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (addField) { |
| 90 | + this.addPart(fileField, file.getAttachName()); |
| 91 | + } |
| 92 | + |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public JettyMultipartBuilder addMedia(InputMedia media) throws IOException { |
| 97 | + if (media == null) { |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + if (media.isNewMedia()) { |
| 102 | + Request.Content body = null; |
| 103 | + if (media.getNewMediaFile() != null) { |
| 104 | + body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath()); |
| 105 | + } else if (media.getNewMediaStream() != null) { |
| 106 | + body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream()); |
| 107 | + } |
| 108 | + if (body != null) { |
| 109 | + multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public JettyMultipartBuilder addMedia(InputPaidMedia media) throws IOException { |
| 117 | + if (media == null) { |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + if (media.isNewMedia()) { |
| 122 | + Request.Content body = null; |
| 123 | + if (media.getNewMediaFile() != null) { |
| 124 | + body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath()); |
| 125 | + } else if (media.getNewMediaStream() != null) { |
| 126 | + body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream()); |
| 127 | + } |
| 128 | + if (body != null) { |
| 129 | + multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public JettyMultipartBuilder addInputStickers(String stickersField, List<InputSticker> stickers) throws IOException { |
| 137 | + for (InputSticker sticker : stickers) { |
| 138 | + addInputFile(null, sticker.getSticker(), false); |
| 139 | + } |
| 140 | + |
| 141 | + addJsonPart(stickersField, stickers); |
| 142 | + |
| 143 | + return this; |
| 144 | + } |
| 145 | +} |
0 commit comments