1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.transport.http.multipart; |
11 | |
|
12 | |
import java.io.FilterOutputStream; |
13 | |
import java.io.IOException; |
14 | |
import java.io.OutputStream; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class MultiPartOutputStream extends FilterOutputStream |
23 | |
{ |
24 | |
|
25 | |
private static byte[] __CRLF; |
26 | |
private static byte[] __DASHDASH; |
27 | |
private String encoding; |
28 | |
|
29 | |
|
30 | |
|
31 | |
private String boundary; |
32 | |
private byte[] boundaryBytes; |
33 | |
|
34 | |
|
35 | 0 | private boolean inPart=false; |
36 | |
|
37 | |
|
38 | |
public MultiPartOutputStream(OutputStream out, String encoding) |
39 | |
throws IOException |
40 | |
{ |
41 | 0 | super(out); |
42 | 0 | this.encoding = encoding; |
43 | |
|
44 | 0 | __CRLF="\015\012".getBytes(encoding); |
45 | 0 | __DASHDASH="--".getBytes(encoding); |
46 | |
|
47 | 0 | boundary = "mule"+System.identityHashCode(this)+ |
48 | |
Long.toString(System.currentTimeMillis(),36); |
49 | 0 | boundaryBytes=boundary.getBytes(encoding); |
50 | |
|
51 | 0 | inPart=false; |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void close() |
61 | |
throws IOException |
62 | |
{ |
63 | 0 | if (inPart) |
64 | 0 | out.write(__CRLF); |
65 | 0 | out.write(__DASHDASH); |
66 | 0 | out.write(boundaryBytes); |
67 | 0 | out.write(__DASHDASH); |
68 | 0 | out.write(__CRLF); |
69 | 0 | inPart=false; |
70 | 0 | super.close(); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
public String getBoundary() |
75 | |
{ |
76 | 0 | return boundary; |
77 | |
} |
78 | |
|
79 | 0 | public OutputStream getOut() {return out;} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void startPart(String contentType) |
85 | |
throws IOException |
86 | |
{ |
87 | 0 | if (inPart) |
88 | 0 | out.write(__CRLF); |
89 | 0 | inPart=true; |
90 | 0 | out.write(__DASHDASH); |
91 | 0 | out.write(boundaryBytes); |
92 | 0 | out.write(__CRLF); |
93 | 0 | out.write(("Content-Type: "+contentType).getBytes(encoding)); |
94 | 0 | out.write(__CRLF); |
95 | 0 | out.write(__CRLF); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void startPart(String contentType, String[] headers) |
102 | |
throws IOException |
103 | |
{ |
104 | 0 | if (inPart) |
105 | 0 | out.write(__CRLF); |
106 | 0 | inPart=true; |
107 | 0 | out.write(__DASHDASH); |
108 | 0 | out.write(boundaryBytes); |
109 | 0 | out.write(__CRLF); |
110 | 0 | out.write(("Content-Type: "+contentType).getBytes(encoding)); |
111 | 0 | out.write(__CRLF); |
112 | 0 | for (int i=0;headers!=null && i<headers.length;i++) |
113 | |
{ |
114 | 0 | out.write(headers[i].getBytes(encoding)); |
115 | 0 | out.write(__CRLF); |
116 | |
} |
117 | 0 | out.write(__CRLF); |
118 | 0 | } |
119 | |
|
120 | |
} |