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.http.entity.mime;
29
30 import java.util.List;
31
32 import org.apache.http.entity.ContentType;
33 import org.apache.http.entity.mime.content.AbstractContentBody;
34 import org.apache.http.entity.mime.content.ContentBody;
35 import org.apache.http.util.Args;
36 import org.apache.http.util.Asserts;
37
38
39
40
41
42
43 public class FormBodyPartBuilder {
44
45 private String name;
46 private ContentBody body;
47 private final Header header;
48
49 public static FormBodyPartBuilder create(final String name, final ContentBody body) {
50 return new FormBodyPartBuilder(name, body);
51 }
52
53 public static FormBodyPartBuilder create() {
54 return new FormBodyPartBuilder();
55 }
56
57 FormBodyPartBuilder(final String name, final ContentBody body) {
58 this();
59 this.name = name;
60 this.body = body;
61 }
62
63 FormBodyPartBuilder() {
64 this.header = new Header();
65 }
66
67 public FormBodyPartBuilder setName(final String name) {
68 this.name = name;
69 return this;
70 }
71
72 public FormBodyPartBuilder setBody(final ContentBody body) {
73 this.body = body;
74 return this;
75 }
76
77 public FormBodyPartBuilder addField(final String name, final String value) {
78 Args.notNull(name, "Field name");
79 this.header.addField(new MinimalField(name, value));
80 return this;
81 }
82
83 public FormBodyPartBuilder setField(final String name, final String value) {
84 Args.notNull(name, "Field name");
85 this.header.setField(new MinimalField(name, value));
86 return this;
87 }
88
89 public FormBodyPartBuilder removeFields(final String name) {
90 Args.notNull(name, "Field name");
91 this.header.removeFields(name);
92 return this;
93 }
94
95 public FormBodyPart build() {
96 Asserts.notBlank(this.name, "Name");
97 Asserts.notNull(this.body, "Content body");
98 final Headereader.html#Header">Header headerCopy = new Header();
99 final List<MinimalField> fields = this.header.getFields();
100 for (final MinimalField field: fields) {
101 headerCopy.addField(field);
102 }
103 if (headerCopy.getField(MIME.CONTENT_DISPOSITION) == null) {
104 final StringBuilder buffer = new StringBuilder();
105 buffer.append("form-data; name=\"");
106 buffer.append(encodeForHeader(this.name));
107 buffer.append("\"");
108 if (this.body.getFilename() != null) {
109 buffer.append("; filename=\"");
110 buffer.append(encodeForHeader(this.body.getFilename()));
111 buffer.append("\"");
112 }
113 headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, buffer.toString()));
114 }
115 if (headerCopy.getField(MIME.CONTENT_TYPE) == null) {
116 final ContentType contentType;
117 if (body instanceof AbstractContentBody) {
118 contentType = ((AbstractContentBody) body).getContentType();
119 } else {
120 contentType = null;
121 }
122 if (contentType != null) {
123 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, contentType.toString()));
124 } else {
125 final StringBuilder buffer = new StringBuilder();
126 buffer.append(this.body.getMimeType());
127 if (this.body.getCharset() != null) {
128 buffer.append("; charset=");
129 buffer.append(this.body.getCharset());
130 }
131 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, buffer.toString()));
132 }
133 }
134 if (headerCopy.getField(MIME.CONTENT_TRANSFER_ENC) == null) {
135
136 headerCopy.addField(new MinimalField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()));
137 }
138 return new FormBodyPart(this.name, this.body, headerCopy);
139 }
140
141 private static String encodeForHeader(final String headerName) {
142 if (headerName == null) {
143 return null;
144 }
145 final StringBuilder sb = new StringBuilder();
146 for (int i = 0; i < headerName.length(); i++) {
147 final char x = headerName.charAt(i);
148 if (x == '"' || x == '\\' || x == '\r') {
149 sb.append("\\");
150 }
151 sb.append(x);
152 }
153 return sb.toString();
154 }
155
156 }