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 org.apache.http.entity.mime.content.ContentBody;
31
32
33
34
35
36
37
38
39 public class FormBodyPart {
40
41 private final String name;
42 private final Header header;
43
44 private final ContentBody body;
45
46 public FormBodyPart(final String name, final ContentBody body) {
47 super();
48 if (name == null) {
49 throw new IllegalArgumentException("Name may not be null");
50 }
51 if (body == null) {
52 throw new IllegalArgumentException("Body may not be null");
53 }
54 this.name = name;
55 this.body = body;
56 this.header = new Header();
57
58 generateContentDisp(body);
59 generateContentType(body);
60 generateTransferEncoding(body);
61 }
62
63 public String getName() {
64 return this.name;
65 }
66
67 public ContentBody getBody() {
68 return this.body;
69 }
70
71 public Header getHeader() {
72 return this.header;
73 }
74
75 public void addField(final String name, final String value) {
76 if (name == null) {
77 throw new IllegalArgumentException("Field name may not be null");
78 }
79 this.header.addField(new MinimalField(name, value));
80 }
81
82 protected void generateContentDisp(final ContentBody body) {
83 StringBuilder buffer = new StringBuilder();
84 buffer.append("form-data; name=\"");
85 buffer.append(getName());
86 buffer.append("\"");
87 if (body.getFilename() != null) {
88 buffer.append("; filename=\"");
89 buffer.append(body.getFilename());
90 buffer.append("\"");
91 }
92 addField(MIME.CONTENT_DISPOSITION, buffer.toString());
93 }
94
95 protected void generateContentType(final ContentBody body) {
96 StringBuilder buffer = new StringBuilder();
97 buffer.append(body.getMimeType());
98 if (body.getCharset() != null) {
99 buffer.append("; charset=");
100 buffer.append(body.getCharset());
101 }
102 addField(MIME.CONTENT_TYPE, buffer.toString());
103 }
104
105 protected void generateTransferEncoding(final ContentBody body) {
106 addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding());
107 }
108
109 }