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.util.ArrayList;
31 import java.util.List;
32
33 import org.apache.hc.core5.http.ContentType;
34 import org.apache.hc.core5.http.NameValuePair;
35 import org.apache.hc.core5.http.message.BasicNameValuePair;
36 import org.apache.hc.core5.util.Args;
37 import org.apache.hc.core5.util.Asserts;
38
39
40
41
42
43
44 public class FormBodyPartBuilder {
45
46 private String name;
47 private ContentBody body;
48 private final Header header;
49
50 public static FormBodyPartBuilder create(final String name, final ContentBody body) {
51 return new FormBodyPartBuilder(name, body);
52 }
53
54 public static FormBodyPartBuilder create() {
55 return new FormBodyPartBuilder();
56 }
57
58 FormBodyPartBuilder(final String name, final ContentBody body) {
59 this();
60 this.name = name;
61 this.body = body;
62 }
63
64 FormBodyPartBuilder() {
65 this.header = new Header();
66 }
67
68 public FormBodyPartBuilder setName(final String name) {
69 this.name = name;
70 return this;
71 }
72
73 public FormBodyPartBuilder setBody(final ContentBody body) {
74 this.body = body;
75 return this;
76 }
77
78
79
80
81 public FormBodyPartBuilder addField(final String name, final String value, final List<NameValuePair> parameters) {
82 Args.notNull(name, "Field name");
83 this.header.addField(new MimeField(name, value, parameters));
84 return this;
85 }
86
87 public FormBodyPartBuilder addField(final String name, final String value) {
88 Args.notNull(name, "Field name");
89 this.header.addField(new MimeField(name, value));
90 return this;
91 }
92
93 public FormBodyPartBuilder setField(final String name, final String value) {
94 Args.notNull(name, "Field name");
95 this.header.setField(new MimeField(name, value));
96 return this;
97 }
98
99 public FormBodyPartBuilder removeFields(final String name) {
100 Args.notNull(name, "Field name");
101 this.header.removeFields(name);
102 return this;
103 }
104
105 public FormBodyPart build() {
106 Asserts.notBlank(this.name, "Name");
107 Asserts.notNull(this.body, "Content body");
108 final Headerttp/entity/mime/Header.html#Header">Header headerCopy = new Header();
109 final List<MimeField> fields = this.header.getFields();
110 for (final MimeField field: fields) {
111 headerCopy.addField(field);
112 }
113 if (headerCopy.getField(MimeConsts.CONTENT_DISPOSITION) == null) {
114 final List<NameValuePair> fieldParameters = new ArrayList<>();
115 fieldParameters.add(new BasicNameValuePair(MimeConsts.FIELD_PARAM_NAME, this.name));
116 if (this.body.getFilename() != null) {
117 fieldParameters.add(new BasicNameValuePair(MimeConsts.FIELD_PARAM_FILENAME, this.body.getFilename()));
118 }
119 headerCopy.addField(new MimeField(MimeConsts.CONTENT_DISPOSITION, "form-data", fieldParameters));
120 }
121 if (headerCopy.getField(MimeConsts.CONTENT_TYPE) == null) {
122 final ContentType contentType;
123 if (body instanceof AbstractContentBody) {
124 contentType = ((AbstractContentBody) body).getContentType();
125 } else {
126 contentType = null;
127 }
128 if (contentType != null) {
129 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, contentType.toString()));
130 } else {
131 final StringBuilder buffer = new StringBuilder();
132 buffer.append(this.body.getMimeType());
133 if (this.body.getCharset() != null) {
134 buffer.append("; charset=");
135 buffer.append(this.body.getCharset());
136 }
137 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, buffer.toString()));
138 }
139 }
140 return new FormBodyPart(this.name, this.body, headerCopy);
141 }
142
143 }