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.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.nio.charset.Charset;
34 import java.util.Random;
35
36 import org.apache.http.Header;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.entity.mime.content.ContentBody;
39
40
41
42
43
44
45
46
47 @Deprecated
48 public class MultipartEntity implements HttpEntity {
49
50
51
52
53 private final static char[] MULTIPART_CHARS =
54 "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
55 .toCharArray();
56
57 private final MultipartEntityBuilder builder;
58 private volatile MultipartFormEntity entity;
59
60
61
62
63
64
65
66 public MultipartEntity(
67 final HttpMultipartMode mode,
68 final String boundary,
69 final Charset charset) {
70 super();
71 this.builder = new MultipartEntityBuilder()
72 .setMode(mode)
73 .setCharset(charset != null ? charset : MIME.DEFAULT_CHARSET)
74 .setBoundary(boundary);
75 this.entity = null;
76 }
77
78
79
80
81
82
83 public MultipartEntity(final HttpMultipartMode mode) {
84 this(mode, null, null);
85 }
86
87
88
89
90 public MultipartEntity() {
91 this(HttpMultipartMode.STRICT, null, null);
92 }
93
94 protected String generateContentType(
95 final String boundary,
96 final Charset charset) {
97 final StringBuilder buffer = new StringBuilder();
98 buffer.append("multipart/form-data; boundary=");
99 buffer.append(boundary);
100 if (charset != null) {
101 buffer.append("; charset=");
102 buffer.append(charset.name());
103 }
104 return buffer.toString();
105 }
106
107 protected String generateBoundary() {
108 final StringBuilder buffer = new StringBuilder();
109 final Random rand = new Random();
110 final int count = rand.nextInt(11) + 30;
111 for (int i = 0; i < count; i++) {
112 buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
113 }
114 return buffer.toString();
115 }
116
117 private MultipartFormEntity getEntity() {
118 if (this.entity == null) {
119 this.entity = this.builder.buildEntity();
120 }
121 return this.entity;
122 }
123
124 public void addPart(final FormBodyPart bodyPart) {
125 this.builder.addPart(bodyPart);
126 this.entity = null;
127 }
128
129 public void addPart(final String name, final ContentBody contentBody) {
130 addPart(new FormBodyPart(name, contentBody));
131 }
132
133 @Override
134 public boolean isRepeatable() {
135 return getEntity().isRepeatable();
136 }
137
138 @Override
139 public boolean isChunked() {
140 return getEntity().isChunked();
141 }
142
143 @Override
144 public boolean isStreaming() {
145 return getEntity().isStreaming();
146 }
147
148 @Override
149 public long getContentLength() {
150 return getEntity().getContentLength();
151 }
152
153 @Override
154 public Header getContentType() {
155 return getEntity().getContentType();
156 }
157
158 @Override
159 public Header getContentEncoding() {
160 return getEntity().getContentEncoding();
161 }
162
163 @Override
164 public void consumeContent()
165 throws IOException, UnsupportedOperationException{
166 if (isStreaming()) {
167 throw new UnsupportedOperationException(
168 "Streaming entity does not implement #consumeContent()");
169 }
170 }
171
172 @Override
173 public InputStream getContent() throws IOException, UnsupportedOperationException {
174 throw new UnsupportedOperationException(
175 "Multipart form entity does not implement #getContent()");
176 }
177
178 @Override
179 public void writeTo(final OutputStream outStream) throws IOException {
180 getEntity().writeTo(outStream);
181 }
182
183 }