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.ContentType;
31 import org.apache.http.entity.mime.content.AbstractContentBody;
32 import org.apache.http.entity.mime.content.ContentBody;
33 import org.apache.http.util.Args;
34
35
36
37
38
39
40
41
42 public class FormBodyPart {
43
44 private final String name;
45 private final Header header;
46 private final ContentBody body;
47
48 FormBodyPart(final String name, final ContentBody body, final Header header) {
49 super();
50 Args.notNull(name, "Name");
51 Args.notNull(body, "Body");
52 this.name = name;
53 this.body = body;
54 this.header = header != null ? header : new Header();
55 }
56
57
58
59
60 @Deprecated
61 public FormBodyPart(final String name, final ContentBody body) {
62 super();
63 Args.notNull(name, "Name");
64 Args.notNull(body, "Body");
65 this.name = name;
66 this.body = body;
67 this.header = new Header();
68
69 generateContentDisp(body);
70 generateContentType(body);
71 generateTransferEncoding(body);
72 }
73
74 public String getName() {
75 return this.name;
76 }
77
78 public ContentBody getBody() {
79 return this.body;
80 }
81
82 public Header getHeader() {
83 return this.header;
84 }
85
86 public void addField(final String name, final String value) {
87 Args.notNull(name, "Field name");
88 this.header.addField(new MinimalField(name, value));
89 }
90
91
92
93
94 @Deprecated
95 protected void generateContentDisp(final ContentBody body) {
96 final StringBuilder buffer = new StringBuilder();
97 buffer.append("form-data; name=\"");
98 buffer.append(getName());
99 buffer.append("\"");
100 if (body.getFilename() != null) {
101 buffer.append("; filename=\"");
102 buffer.append(body.getFilename());
103 buffer.append("\"");
104 }
105 addField(MIME.CONTENT_DISPOSITION, buffer.toString());
106 }
107
108
109
110
111 @Deprecated
112 protected void generateContentType(final ContentBody body) {
113 final ContentType contentType;
114 if (body instanceof AbstractContentBody) {
115 contentType = ((AbstractContentBody) body).getContentType();
116 } else {
117 contentType = null;
118 }
119 if (contentType != null) {
120 addField(MIME.CONTENT_TYPE, contentType.toString());
121 } else {
122 final StringBuilder buffer = new StringBuilder();
123 buffer.append(body.getMimeType());
124 if (body.getCharset() != null) {
125 buffer.append("; charset=");
126 buffer.append(body.getCharset());
127 }
128 addField(MIME.CONTENT_TYPE, buffer.toString());
129 }
130 }
131
132
133
134
135 @Deprecated
136 protected void generateTransferEncoding(final ContentBody body) {
137 addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding());
138 }
139
140 }