View Javadoc

1   // ========================================================================
2   // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.util;
16  
17  import java.io.FilterWriter;
18  import java.io.IOException;
19  import java.io.Writer;
20  
21  
22  /* ================================================================ */
23  /** Handle a multipart MIME response.
24   *
25   * @author Greg Wilkins
26   * @author Jim Crossley
27  */
28  public class MultiPartWriter extends FilterWriter
29  {
30      /* ------------------------------------------------------------ */
31      private final static String __CRLF="\015\012";
32      private final static String __DASHDASH="--";
33      
34      public static String MULTIPART_MIXED=MultiPartOutputStream.MULTIPART_MIXED;
35      public static String MULTIPART_X_MIXED_REPLACE=MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
36      
37      /* ------------------------------------------------------------ */
38      private String boundary;
39  
40      /* ------------------------------------------------------------ */
41      private boolean inPart=false;    
42      
43      /* ------------------------------------------------------------ */
44      public MultiPartWriter(Writer out)
45           throws IOException
46      {
47          super(out);
48          boundary = "jetty"+System.identityHashCode(this)+
49          Long.toString(System.currentTimeMillis(),36);
50          
51          inPart=false;
52      }
53  
54      /* ------------------------------------------------------------ */
55      /** End the current part.
56       * @exception IOException IOException
57       */
58      public void close()
59           throws IOException
60      {
61          if (inPart)
62              out.write(__CRLF);
63          out.write(__DASHDASH);
64          out.write(boundary);
65          out.write(__DASHDASH);
66          out.write(__CRLF);
67          inPart=false;
68          super.close();
69      }
70      
71      /* ------------------------------------------------------------ */
72      public String getBoundary()
73      {
74          return boundary;
75      }
76      
77      /* ------------------------------------------------------------ */
78      /** Start creation of the next Content.
79       */
80      public void startPart(String contentType)
81           throws IOException
82      {
83          if (inPart)
84              out.write(__CRLF);
85          out.write(__DASHDASH);
86          out.write(boundary);
87          out.write(__CRLF);
88          out.write("Content-Type: ");
89          out.write(contentType);
90          out.write(__CRLF);
91          out.write(__CRLF);
92          inPart=true;
93      }
94      
95      /* ------------------------------------------------------------ */
96      /** end creation of the next Content.
97       */
98      public void endPart()
99           throws IOException
100     {
101         if (inPart)
102             out.write(__CRLF);
103         inPart=false;
104     }
105         
106     /* ------------------------------------------------------------ */
107     /** Start creation of the next Content.
108      */
109     public void startPart(String contentType, String[] headers)
110          throws IOException
111     {
112         if (inPart)
113             out.write(__CRLF);
114         out.write(__DASHDASH);
115         out.write(boundary);
116         out.write(__CRLF);
117         out.write("Content-Type: ");
118         out.write(contentType);
119         out.write(__CRLF);
120         for (int i=0;headers!=null && i<headers.length;i++)
121         {
122             out.write(headers[i]);
123             out.write(__CRLF);
124         }
125         out.write(__CRLF);
126         inPart=true;
127     }
128     
129 }
130 
131 
132 
133