|
18 | 18 | import modelengine.fit.http.entity.FileEntity; |
19 | 19 | import modelengine.fit.http.entity.NamedEntity; |
20 | 20 | import modelengine.fit.http.entity.PartitionedEntity; |
| 21 | +import modelengine.fit.http.entity.TextEntity; |
21 | 22 | import modelengine.fit.http.entity.support.DefaultNamedEntity; |
22 | 23 | import modelengine.fit.http.entity.support.DefaultPartitionedEntity; |
23 | 24 | import modelengine.fit.http.entity.support.DefaultTextEntity; |
@@ -82,7 +83,111 @@ public class MultiPartEntitySerializer implements EntitySerializer<PartitionedEn |
82 | 83 |
|
83 | 84 | @Override |
84 | 85 | public void serializeEntity(@Nonnull PartitionedEntity entity, Charset charset, OutputStream out) { |
85 | | - throw new EntityWriteException("Unsupported to serialize entity of Content-Type 'multipart/*'."); |
| 86 | + String boundary = this.getBoundary(entity); |
| 87 | + try { |
| 88 | + for (NamedEntity namedEntity : entity.entities()) { |
| 89 | + this.writeBoundary(out, boundary, charset, false); |
| 90 | + this.writeHeaders(out, namedEntity, charset); |
| 91 | + this.writeEntityContent(out, namedEntity, charset); |
| 92 | + } |
| 93 | + this.writeBoundary(out, boundary, charset, true); |
| 94 | + } catch (IOException e) { |
| 95 | + throw new EntityWriteException("Failed to serialize entity of Content-Type 'multipart/*'.", e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 获取 boundary 分隔符。 |
| 101 | + * |
| 102 | + * @param entity 表示分块的消息体数据的 {@link PartitionedEntity}。 |
| 103 | + * @return 表示 boundary 分隔符的 {@link String}。 |
| 104 | + */ |
| 105 | + private String getBoundary(PartitionedEntity entity) { |
| 106 | + String boundary = entity.belongTo() |
| 107 | + .contentType() |
| 108 | + .flatMap(ContentType::boundary) |
| 109 | + .orElseThrow(() -> new EntityWriteException("The boundary is not present in Content-Type.")); |
| 110 | + return BOUNDARY_SURROUND + boundary; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 写入分隔符。 |
| 115 | + * |
| 116 | + * @param out 表示输出流的 {@link OutputStream}。 |
| 117 | + * @param boundary 表示 boundary 分隔符的 {@link String}。 |
| 118 | + * @param charset 表示字符集的 {@link Charset}。 |
| 119 | + * @param isEnd 表示是否是终止分隔符的 {@code boolean}。 |
| 120 | + * @throws IOException 当发生 I/O 异常时。 |
| 121 | + */ |
| 122 | + private void writeBoundary(OutputStream out, String boundary, Charset charset, boolean isEnd) throws IOException { |
| 123 | + out.write(BOUNDARY_SURROUND.getBytes(charset)); |
| 124 | + out.write(boundary.getBytes(charset)); |
| 125 | + if (isEnd) { |
| 126 | + out.write(BOUNDARY_SURROUND.getBytes(charset)); |
| 127 | + } |
| 128 | + out.write(CR); |
| 129 | + out.write(LF); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * 写入消息头。 |
| 134 | + * |
| 135 | + * @param out 表示输出流的 {@link OutputStream}。 |
| 136 | + * @param namedEntity 表示带名字的消息体数据的 {@link NamedEntity}。 |
| 137 | + * @param charset 表示字符集的 {@link Charset}。 |
| 138 | + * @throws IOException 当发生 I/O 异常时。 |
| 139 | + */ |
| 140 | + private void writeHeaders(OutputStream out, NamedEntity namedEntity, Charset charset) throws IOException { |
| 141 | + // Write Content-Disposition header |
| 142 | + StringBuilder disposition = new StringBuilder("Content-Disposition: form-data"); |
| 143 | + if (!StringUtils.isEmpty(namedEntity.name())) { |
| 144 | + disposition.append("; name=\"").append(namedEntity.name()).append("\""); |
| 145 | + } |
| 146 | + if (namedEntity.isFile()) { |
| 147 | + FileEntity fileEntity = namedEntity.asFile(); |
| 148 | + disposition.append("; filename=\"").append(fileEntity.filename()).append("\""); |
| 149 | + } |
| 150 | + out.write(disposition.toString().getBytes(charset)); |
| 151 | + out.write(CR); |
| 152 | + out.write(LF); |
| 153 | + |
| 154 | + // Write Content-Type header if it's a file |
| 155 | + if (namedEntity.isFile()) { |
| 156 | + Entity innerEntity = namedEntity.entity(); |
| 157 | + String contentType = "Content-Type: " + innerEntity.resolvedMimeType().value(); |
| 158 | + out.write(contentType.getBytes(charset)); |
| 159 | + out.write(CR); |
| 160 | + out.write(LF); |
| 161 | + } |
| 162 | + |
| 163 | + // Write empty line |
| 164 | + out.write(CR); |
| 165 | + out.write(LF); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * 写入实体内容。 |
| 170 | + * |
| 171 | + * @param out 表示输出流的 {@link OutputStream}。 |
| 172 | + * @param namedEntity 表示带名字的消息体数据的 {@link NamedEntity}。 |
| 173 | + * @param charset 表示字符集的 {@link Charset}。 |
| 174 | + * @throws IOException 当发生 I/O 异常时。 |
| 175 | + */ |
| 176 | + private void writeEntityContent(OutputStream out, NamedEntity namedEntity, Charset charset) throws IOException { |
| 177 | + Entity innerEntity = namedEntity.entity(); |
| 178 | + if (namedEntity.isText()) { |
| 179 | + TextEntity textEntity = cast(innerEntity); |
| 180 | + out.write(textEntity.content().getBytes(charset)); |
| 181 | + } else if (namedEntity.isFile()) { |
| 182 | + FileEntity fileEntity = cast(innerEntity); |
| 183 | + byte[] buffer = new byte[8192]; |
| 184 | + int bytesRead; |
| 185 | + while ((bytesRead = fileEntity.read(buffer)) != -1) { |
| 186 | + out.write(buffer, 0, bytesRead); |
| 187 | + } |
| 188 | + } |
| 189 | + out.write(CR); |
| 190 | + out.write(LF); |
86 | 191 | } |
87 | 192 |
|
88 | 193 | @Override |
|
0 commit comments