1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.entity.mime;
29
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.charset.Charset;
33 import java.nio.charset.StandardCharsets;
34 import java.util.List;
35
36 import org.apache.hc.core5.http.NameValuePair;
37 import org.apache.hc.core5.net.PercentCodec;
38
39 class HttpRFC7578Multipart extends AbstractMultipartFormat {
40
41 private final List<MultipartPart> parts;
42
43
44
45
46
47
48
49
50
51
52 public HttpRFC7578Multipart(
53 final Charset charset,
54 final String boundary,
55 final List<MultipartPart> parts,
56 final String preamble,
57 final String epilogue) {
58 super(charset, boundary, preamble, epilogue);
59 this.parts = parts;
60 }
61
62
63
64
65
66
67
68
69 public HttpRFC7578Multipart(
70 final Charset charset,
71 final String boundary,
72 final List<MultipartPart> parts) {
73 this(charset,boundary,parts,null, null);
74 }
75
76 @Override
77 public List<MultipartPart> getParts() {
78 return parts;
79 }
80
81 @Override
82 protected void formatMultipartHeader(final MultipartPart part, final OutputStream out) throws IOException {
83 for (final MimeField field: part.getHeader()) {
84 if (MimeConsts.CONTENT_DISPOSITION.equalsIgnoreCase(field.getName())) {
85 writeBytes(field.getName(), charset, out);
86 writeBytes(FIELD_SEP, out);
87 writeBytes(field.getValue(), out);
88 final List<NameValuePair> parameters = field.getParameters();
89 for (int i = 0; i < parameters.size(); i++) {
90 final NameValuePair parameter = parameters.get(i);
91 final String name = parameter.getName();
92 final String value = parameter.getValue();
93 writeBytes("; ", out);
94 writeBytes(name, out);
95 writeBytes("=\"", out);
96 if (value != null) {
97 if (name.equalsIgnoreCase(MimeConsts.FIELD_PARAM_FILENAME) ||
98 name.equalsIgnoreCase(MimeConsts.FIELD_PARAM_FILENAME_START)) {
99 final String encodedValue = name.equalsIgnoreCase(MimeConsts.FIELD_PARAM_FILENAME_START) ?
100 "UTF-8''" + PercentCodec.RFC5987.encode(value) : PercentCodec.RFC5987.encode(value);
101 final byte[] encodedBytes = encodedValue.getBytes(StandardCharsets.US_ASCII);
102 out.write(encodedBytes);
103 } else {
104 writeBytes(value, out);
105 }
106 }
107 writeBytes("\"", out);
108 }
109 writeBytes(CR_LF, out);
110 } else {
111 writeField(field, charset, out);
112 }
113 }
114 }
115
116 }