001: // ========================================================================
002: // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.io.FilterOutputStream;
018: import java.io.IOException;
019: import java.io.OutputStream;
020:
021: /* ================================================================ */
022: /** Handle a multipart MIME response.
023: *
024: * @author Greg Wilkins
025: * @author Jim Crossley
026: */
027: public class MultiPartOutputStream extends FilterOutputStream {
028: /* ------------------------------------------------------------ */
029: private static byte[] __CRLF;
030: private static byte[] __DASHDASH;
031:
032: public static String MULTIPART_MIXED = "multipart/mixed";
033: public static String MULTIPART_X_MIXED_REPLACE = "multipart/x-mixed-replace";
034: static {
035: try {
036: __CRLF = "\015\012".getBytes(StringUtil.__ISO_8859_1);
037: __DASHDASH = "--".getBytes(StringUtil.__ISO_8859_1);
038: } catch (Exception e) {
039: e.printStackTrace();
040: System.exit(1);
041: }
042: }
043:
044: /* ------------------------------------------------------------ */
045: private String boundary;
046: private byte[] boundaryBytes;
047:
048: /* ------------------------------------------------------------ */
049: private boolean inPart = false;
050:
051: /* ------------------------------------------------------------ */
052: public MultiPartOutputStream(OutputStream out) throws IOException {
053: super (out);
054: try {
055: boundary = "jetty" + System.identityHashCode(this )
056: + Long.toString(System.currentTimeMillis(), 36);
057: boundaryBytes = boundary.getBytes(StringUtil.__ISO_8859_1);
058: } catch (Exception e) {
059: e.printStackTrace();
060: System.exit(1);
061: }
062:
063: inPart = false;
064: }
065:
066: /* ------------------------------------------------------------ */
067: /** End the current part.
068: * @exception IOException IOException
069: */
070: public void close() throws IOException {
071: if (inPart)
072: out.write(__CRLF);
073: out.write(__DASHDASH);
074: out.write(boundaryBytes);
075: out.write(__DASHDASH);
076: out.write(__CRLF);
077: inPart = false;
078: super .close();
079: }
080:
081: /* ------------------------------------------------------------ */
082: public String getBoundary() {
083: return boundary;
084: }
085:
086: public OutputStream getOut() {
087: return out;
088: }
089:
090: /* ------------------------------------------------------------ */
091: /** Start creation of the next Content.
092: */
093: public void startPart(String contentType) throws IOException {
094: if (inPart)
095: out.write(__CRLF);
096: inPart = true;
097: out.write(__DASHDASH);
098: out.write(boundaryBytes);
099: out.write(__CRLF);
100: out.write(("Content-Type: " + contentType)
101: .getBytes(StringUtil.__ISO_8859_1));
102: out.write(__CRLF);
103: out.write(__CRLF);
104: }
105:
106: /* ------------------------------------------------------------ */
107: /** Start creation of the next Content.
108: */
109: public void startPart(String contentType, String[] headers)
110: throws IOException {
111: if (inPart)
112: out.write(__CRLF);
113: inPart = true;
114: out.write(__DASHDASH);
115: out.write(boundaryBytes);
116: out.write(__CRLF);
117: out.write(("Content-Type: " + contentType)
118: .getBytes(StringUtil.__ISO_8859_1));
119: out.write(__CRLF);
120: for (int i = 0; headers != null && i < headers.length; i++) {
121: out.write(headers[i].getBytes(StringUtil.__ISO_8859_1));
122: out.write(__CRLF);
123: }
124: out.write(__CRLF);
125: }
126:
127: }
|