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.FilterWriter;
018: import java.io.IOException;
019: import java.io.Writer;
020:
021: /* ================================================================ */
022: /** Handle a multipart MIME response.
023: *
024: * @author Greg Wilkins
025: * @author Jim Crossley
026: */
027: public class MultiPartWriter extends FilterWriter {
028: /* ------------------------------------------------------------ */
029: private final static String __CRLF = "\015\012";
030: private final static String __DASHDASH = "--";
031:
032: public static String MULTIPART_MIXED = MultiPartOutputStream.MULTIPART_MIXED;
033: public static String MULTIPART_X_MIXED_REPLACE = MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
034:
035: /* ------------------------------------------------------------ */
036: private String boundary;
037:
038: /* ------------------------------------------------------------ */
039: private boolean inPart = false;
040:
041: /* ------------------------------------------------------------ */
042: public MultiPartWriter(Writer out) throws IOException {
043: super (out);
044: boundary = "jetty" + System.identityHashCode(this )
045: + Long.toString(System.currentTimeMillis(), 36);
046:
047: inPart = false;
048: }
049:
050: /* ------------------------------------------------------------ */
051: /** End the current part.
052: * @exception IOException IOException
053: */
054: public void close() throws IOException {
055: if (inPart)
056: out.write(__CRLF);
057: out.write(__DASHDASH);
058: out.write(boundary);
059: out.write(__DASHDASH);
060: out.write(__CRLF);
061: inPart = false;
062: super .close();
063: }
064:
065: /* ------------------------------------------------------------ */
066: public String getBoundary() {
067: return boundary;
068: }
069:
070: /* ------------------------------------------------------------ */
071: /** Start creation of the next Content.
072: */
073: public void startPart(String contentType) throws IOException {
074: if (inPart)
075: out.write(__CRLF);
076: out.write(__DASHDASH);
077: out.write(boundary);
078: out.write(__CRLF);
079: out.write("Content-Type: ");
080: out.write(contentType);
081: out.write(__CRLF);
082: out.write(__CRLF);
083: inPart = true;
084: }
085:
086: /* ------------------------------------------------------------ */
087: /** end creation of the next Content.
088: */
089: public void endPart() throws IOException {
090: if (inPart)
091: out.write(__CRLF);
092: inPart = false;
093: }
094:
095: /* ------------------------------------------------------------ */
096: /** Start creation of the next Content.
097: */
098: public void startPart(String contentType, String[] headers)
099: throws IOException {
100: if (inPart)
101: out.write(__CRLF);
102: out.write(__DASHDASH);
103: out.write(boundary);
104: out.write(__CRLF);
105: out.write("Content-Type: ");
106: out.write(contentType);
107: out.write(__CRLF);
108: for (int i = 0; headers != null && i < headers.length; i++) {
109: out.write(headers[i]);
110: out.write(__CRLF);
111: }
112: out.write(__CRLF);
113: inPart = true;
114: }
115:
116: }
|