1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
21 import java.io.FilterWriter;
22 import java.io.IOException;
23 import java.io.Writer;
24
25
26 /* ================================================================ */
27 /** Handle a multipart MIME response.
28 *
29 *
30 *
31 */
32 public class MultiPartWriter extends FilterWriter
33 {
34 /* ------------------------------------------------------------ */
35 private final static String __CRLF="\015\012";
36 private final static String __DASHDASH="--";
37
38 public static final String MULTIPART_MIXED=MultiPartOutputStream.MULTIPART_MIXED;
39 public static final String MULTIPART_X_MIXED_REPLACE=MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
40
41 /* ------------------------------------------------------------ */
42 private String boundary;
43
44 /* ------------------------------------------------------------ */
45 private boolean inPart=false;
46
47 /* ------------------------------------------------------------ */
48 public MultiPartWriter(Writer out)
49 throws IOException
50 {
51 super(out);
52 boundary = "jetty"+System.identityHashCode(this)+
53 Long.toString(System.currentTimeMillis(),36);
54
55 inPart=false;
56 }
57
58 /* ------------------------------------------------------------ */
59 /** End the current part.
60 * @exception IOException IOException
61 */
62 @Override
63 public void close()
64 throws IOException
65 {
66 try
67 {
68 if (inPart)
69 out.write(__CRLF);
70 out.write(__DASHDASH);
71 out.write(boundary);
72 out.write(__DASHDASH);
73 out.write(__CRLF);
74 inPart=false;
75 }
76 finally
77 {
78 super.close();
79 }
80 }
81
82 /* ------------------------------------------------------------ */
83 public String getBoundary()
84 {
85 return boundary;
86 }
87
88 /* ------------------------------------------------------------ */
89 /** Start creation of the next Content.
90 * @param contentType the content type
91 * @throws IOException if unable to write the part
92 */
93 public void startPart(String contentType)
94 throws IOException
95 {
96 if (inPart)
97 out.write(__CRLF);
98 out.write(__DASHDASH);
99 out.write(boundary);
100 out.write(__CRLF);
101 out.write("Content-Type: ");
102 out.write(contentType);
103 out.write(__CRLF);
104 out.write(__CRLF);
105 inPart=true;
106 }
107
108 /* ------------------------------------------------------------ */
109 /** end creation of the next Content.
110 * @throws IOException if unable to write the part
111 */
112 public void endPart()
113 throws IOException
114 {
115 if (inPart)
116 out.write(__CRLF);
117 inPart=false;
118 }
119
120 /* ------------------------------------------------------------ */
121 /** Start creation of the next Content.
122 * @param contentType the content type of the part
123 * @param headers the part headers
124 * @throws IOException if unable to write the part
125 */
126 public void startPart(String contentType, String[] headers)
127 throws IOException
128 {
129 if (inPart)
130 out.write(__CRLF);
131 out.write(__DASHDASH);
132 out.write(boundary);
133 out.write(__CRLF);
134 out.write("Content-Type: ");
135 out.write(contentType);
136 out.write(__CRLF);
137 for (int i=0;headers!=null && i<headers.length;i++)
138 {
139 out.write(headers[i]);
140 out.write(__CRLF);
141 }
142 out.write(__CRLF);
143 inPart=true;
144 }
145
146 }
147
148
149
150