KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > MultiPartResponse


1 // ========================================================================
2
// $Id: MultiPartResponse.java,v 1.12 2006/04/04 22:28:02 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.mortbay.log.LogFactory;
23 import org.mortbay.util.StringUtil;
24
25 /* ================================================================ */
26 /** Handle a multipart MIME response.
27  *
28  * @version $Id: MultiPartResponse.java,v 1.12 2006/04/04 22:28:02 gregwilkins Exp $
29  * @author Greg Wilkins
30  * @author Jim Crossley
31 */

32 public class MultiPartResponse
33 {
34     private static Log log = LogFactory.getLog(MultiPartResponse.class);
35     
36     /* ------------------------------------------------------------ */
37     private static byte[] __CRLF;
38     private static byte[] __DASHDASH;
39     static
40     {
41         try
42         {
43             __CRLF="\015\012".getBytes(StringUtil.__ISO_8859_1);
44             __DASHDASH="--".getBytes(StringUtil.__ISO_8859_1);
45         }
46         catch (Exception JavaDoc e) {log.fatal(e); System.exit(1);}
47     }
48     
49     /* ------------------------------------------------------------ */
50     private String JavaDoc boundary;
51     private byte[] boundaryBytes;
52
53     /* ------------------------------------------------------------ */
54     private MultiPartResponse()
55     {
56         try
57         {
58             boundary = "jetty"+System.identityHashCode(this)+
59                 Long.toString(System.currentTimeMillis(),36);
60             boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1);
61         }
62         catch (Exception JavaDoc e)
63         {
64             log.fatal(e); System.exit(1);
65         }
66     }
67     
68     /* ------------------------------------------------------------ */
69     public String JavaDoc getBoundary()
70     {
71         return boundary;
72     }
73     
74     /* ------------------------------------------------------------ */
75     /** PrintWriter to write content too.
76      */

77     private OutputStream JavaDoc out = null;
78     public OutputStream JavaDoc getOut() {return out;}
79
80     /* ------------------------------------------------------------ */
81     private boolean inPart=false;
82     
83     /* ------------------------------------------------------------ */
84     public MultiPartResponse(OutputStream JavaDoc out)
85          throws IOException JavaDoc
86     {
87         this();
88         this.out=out;
89         inPart=false;
90     }
91     
92     /* ------------------------------------------------------------ */
93     /** MultiPartResponse constructor.
94      */

95     public MultiPartResponse(HttpResponse response)
96          throws IOException JavaDoc
97     {
98         this();
99         response.setField(HttpFields.__ContentType,"multipart/mixed;boundary="+boundary);
100         out=response.getOutputStream();
101         inPart=false;
102     }
103
104     /* ------------------------------------------------------------ */
105     /** Start creation of the next Content.
106      */

107     public void startPart(String JavaDoc contentType)
108          throws IOException JavaDoc
109     {
110         if (inPart)
111             out.write(__CRLF);
112         inPart=true;
113         out.write(__DASHDASH);
114         out.write(boundaryBytes);
115         out.write(__CRLF);
116         out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
117         out.write(__CRLF);
118         out.write(__CRLF);
119     }
120     
121     /* ------------------------------------------------------------ */
122     /** Start creation of the next Content.
123      */

124     public void startPart(String JavaDoc contentType, String JavaDoc[] headers)
125          throws IOException JavaDoc
126     {
127         if (inPart)
128             out.write(__CRLF);
129         inPart=true;
130         out.write(__DASHDASH);
131         out.write(boundaryBytes);
132         out.write(__CRLF);
133         out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
134         out.write(__CRLF);
135         for (int i=0;headers!=null && i<headers.length;i++)
136         {
137             out.write(headers[i].getBytes(StringUtil.__ISO_8859_1));
138             out.write(__CRLF);
139         }
140         out.write(__CRLF);
141     }
142         
143     /* ------------------------------------------------------------ */
144     /** End the current part.
145      * @exception IOException IOException
146      */

147     public void close()
148          throws IOException JavaDoc
149     {
150         if (inPart)
151             out.write(__CRLF);
152         out.write(__DASHDASH);
153         out.write(boundaryBytes);
154         out.write(__DASHDASH);
155         out.write(__CRLF);
156         inPart=false;
157     }
158     
159 };
160
161
162
163
164
Popular Tags