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.hc.client5.http.entity.mime;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.OutputStream;
33  import java.nio.ByteBuffer;
34  import java.nio.CharBuffer;
35  import java.nio.charset.Charset;
36  import java.nio.charset.StandardCharsets;
37  import java.util.List;
38  
39  import org.apache.hc.core5.util.Args;
40  import org.apache.hc.core5.util.ByteArrayBuffer;
41  
42  /**
43   * HttpMultipart represents a collection of MIME multipart encoded content bodies.
44   *
45   * @since 4.3
46   */
47  abstract class AbstractMultipartFormat {
48  
49      static ByteArrayBuffer encode(
50              final Charset charset, final String string) {
51          final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
52          final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
53          bab.append(encoded.array(), encoded.arrayOffset() + encoded.position(), encoded.remaining());
54          return bab;
55      }
56  
57      static void writeBytes(
58              final ByteArrayBuffer b, final OutputStream out) throws IOException {
59          out.write(b.array(), 0, b.length());
60      }
61  
62      static void writeBytes(
63              final String s, final Charset charset, final OutputStream out) throws IOException {
64          final ByteArrayBuffer b = encode(charset, s);
65          writeBytes(b, out);
66      }
67  
68      static void writeBytes(
69              final String s, final OutputStream out) throws IOException {
70          final ByteArrayBuffer b = encode(StandardCharsets.ISO_8859_1, s);
71          writeBytes(b, out);
72      }
73  
74      static void writeField(
75              final MimeField field, final OutputStream out) throws IOException {
76          writeBytes(field.getName(), out);
77          writeBytes(FIELD_SEP, out);
78          writeBytes(field.getBody(), out);
79          writeBytes(CR_LF, out);
80      }
81  
82      static void writeField(
83              final MimeField field, final Charset charset, final OutputStream out) throws IOException {
84          writeBytes(field.getName(), charset, out);
85          writeBytes(FIELD_SEP, out);
86          writeBytes(field.getBody(), charset, out);
87          writeBytes(CR_LF, out);
88      }
89  
90      static final ByteArrayBuffer FIELD_SEP = encode(StandardCharsets.ISO_8859_1, ": ");
91      static final ByteArrayBuffer CR_LF = encode(StandardCharsets.ISO_8859_1, "\r\n");
92      static final ByteArrayBuffer TWO_HYPHENS = encode(StandardCharsets.ISO_8859_1, "--");
93  
94      final Charset charset;
95      final String boundary;
96  
97      /**
98       * Creates an instance with the specified settings.
99       *
100      * @param charset the character set to use. May be {@code null}, in which case {@link StandardCharsets#ISO_8859_1} is used.
101      * @param boundary to use  - must not be {@code null}
102      * @throws IllegalArgumentException if charset is null or boundary is null
103      */
104     public AbstractMultipartFormat(final Charset charset, final String boundary) {
105         super();
106         Args.notNull(boundary, "Multipart boundary");
107         this.charset = charset != null ? charset : StandardCharsets.ISO_8859_1;
108         this.boundary = boundary;
109     }
110 
111     public AbstractMultipartFormat(final String boundary) {
112         this(null, boundary);
113     }
114 
115     public abstract List<MultipartPart> getParts();
116 
117     void doWriteTo(
118         final OutputStream out,
119         final boolean writeContent) throws IOException {
120 
121         final ByteArrayBuffer boundaryEncoded = encode(this.charset, this.boundary);
122         for (final MultipartPart part: getParts()) {
123             writeBytes(TWO_HYPHENS, out);
124             writeBytes(boundaryEncoded, out);
125             writeBytes(CR_LF, out);
126 
127             formatMultipartHeader(part, out);
128 
129             writeBytes(CR_LF, out);
130 
131             if (writeContent) {
132                 part.getBody().writeTo(out);
133             }
134             writeBytes(CR_LF, out);
135         }
136         writeBytes(TWO_HYPHENS, out);
137         writeBytes(boundaryEncoded, out);
138         writeBytes(TWO_HYPHENS, out);
139         writeBytes(CR_LF, out);
140     }
141 
142     /**
143       * Write the multipart header fields; depends on the style.
144       */
145     protected abstract void formatMultipartHeader(
146         final MultipartPart part,
147         final OutputStream out) throws IOException;
148 
149     /**
150      * Writes out the content in the multipart/form encoding. This method
151      * produces slightly different formatting depending on its compatibility
152      * mode.
153      */
154     public void writeTo(final OutputStream out) throws IOException {
155         doWriteTo(out, true);
156     }
157 
158     /**
159      * Determines the total length of the multipart content (content length of
160      * individual parts plus that of extra elements required to delimit the parts
161      * from one another). If any of the @{link BodyPart}s contained in this object
162      * is of a streaming entity of unknown length the total length is also unknown.
163      * <p>
164      * This method buffers only a small amount of data in order to determine the
165      * total length of the entire entity. The content of individual parts is not
166      * buffered.
167      * </p>
168      *
169      * @return total length of the multipart entity if known, {@code -1}
170      *   otherwise.
171      */
172     public long getTotalLength() {
173         long contentLen = 0;
174         for (final MultipartPart part: getParts()) {
175             final ContentBody body = part.getBody();
176             final long len = body.getContentLength();
177             if (len >= 0) {
178                 contentLen += len;
179             } else {
180                 return -1;
181             }
182         }
183         final ByteArrayOutputStream out = new ByteArrayOutputStream();
184         try {
185             doWriteTo(out, false);
186             final byte[] extra = out.toByteArray();
187             return contentLen + extra.length;
188         } catch (final IOException ex) {
189             // Should never happen
190             return -1;
191         }
192     }
193 
194 }