View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.entity.mime;
29  
30  import java.io.File;
31  import java.io.InputStream;
32  import java.nio.charset.Charset;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.apache.http.entity.ContentType;
37  import org.apache.http.entity.mime.content.ByteArrayBody;
38  import org.apache.http.entity.mime.content.FileBody;
39  import org.apache.http.entity.mime.content.InputStreamBody;
40  import org.apache.http.entity.mime.content.StringBody;
41  import org.apache.http.util.Args;
42  
43  /**
44   * @since 4.3
45   */
46  public class MultipartEntityBuilder {
47  
48      private boolean lax;
49      private String boundary;
50      private Charset charset;
51      private List<FormBodyPart> bodyParts;
52  
53      public static MultipartEntityBuilder create() {
54          return new MultipartEntityBuilder();
55      }
56  
57      MultipartEntityBuilder() {
58          super();
59      }
60  
61      public MultipartEntityBuilder setLaxMode() {
62          this.lax = true;
63          return this;
64      }
65  
66      public MultipartEntityBuilder setStrictMode() {
67          this.lax = false;
68          return this;
69      }
70  
71      public MultipartEntityBuilder setBoundary(final String boundary) {
72          this.boundary = boundary;
73          return this;
74      }
75  
76      public MultipartEntityBuilder setCharset(final Charset charset) {
77          this.charset = charset;
78          return this;
79      }
80  
81      public MultipartEntityBuilder addTextBody(
82              final String name, final String text, final ContentType contentType) {
83          Args.notNull(name, "Name");
84          Args.notNull(text, "Text");
85          if (this.bodyParts == null) {
86              this.bodyParts = new ArrayList<FormBodyPart>();
87          }
88          this.bodyParts.add(new FormBodyPart(name, new StringBody(text, contentType)));
89          return this;
90      }
91  
92      public MultipartEntityBuilder addTextBody(
93              final String name, final String text) {
94          return addTextBody(name, text, ContentType.DEFAULT_TEXT);
95      }
96  
97      public MultipartEntityBuilder addBinaryBody(
98              final String name, final byte[] b, final ContentType contentType, final String filename) {
99          if (this.bodyParts == null) {
100             this.bodyParts = new ArrayList<FormBodyPart>();
101         }
102         this.bodyParts.add(new FormBodyPart(name, new ByteArrayBody(b, contentType, filename)));
103         return this;
104     }
105 
106     public MultipartEntityBuilder addBinaryBody(
107             final String name, final byte[] b) {
108         return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null);
109     }
110 
111     public MultipartEntityBuilder addBinaryBody(
112             final String name, final File file, final ContentType contentType, final String filename) {
113         if (this.bodyParts == null) {
114             this.bodyParts = new ArrayList<FormBodyPart>();
115         }
116         this.bodyParts.add(
117                 new FormBodyPart(name, new FileBody(file, contentType, filename)));
118         return this;
119     }
120 
121     public MultipartEntityBuilder addBinaryBody(
122             final String name, final File file) {
123         return addBinaryBody(name, file, ContentType.DEFAULT_BINARY, null);
124     }
125 
126     public MultipartEntityBuilder addBinaryBody(
127             final String name, final InputStream stream, final ContentType contentType,
128             final String filename) {
129         if (this.bodyParts == null) {
130             this.bodyParts = new ArrayList<FormBodyPart>();
131         }
132         this.bodyParts.add(
133                 new FormBodyPart(name, new InputStreamBody(stream, contentType, filename)));
134         return this;
135     }
136 
137     public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream) {
138         return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null);
139     }
140 
141     public MultipartEntity build() {
142         final MultipartEntity e = new MultipartEntity(
143                 this.lax ? HttpMultipartMode.BROWSER_COMPATIBLE : HttpMultipartMode.STRICT,
144                 this.boundary, this.charset);
145         if (this.bodyParts != null) {
146             for (final FormBodyPart bp: this.bodyParts) {
147                 e.addPart(bp);
148             }
149         }
150         return e;
151     }
152 
153 }