View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.io.FilterWriter;
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  
26  /* ================================================================ */
27  /** Handle a multipart MIME response.
28   *
29   * 
30   * 
31  */
32  public class MultiPartWriter extends FilterWriter
33  {
34      /* ------------------------------------------------------------ */
35      private final static String __CRLF="\015\012";
36      private final static String __DASHDASH="--";
37      
38      public static String MULTIPART_MIXED=MultiPartOutputStream.MULTIPART_MIXED;
39      public static String MULTIPART_X_MIXED_REPLACE=MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
40      
41      /* ------------------------------------------------------------ */
42      private String boundary;
43  
44      /* ------------------------------------------------------------ */
45      private boolean inPart=false;    
46      
47      /* ------------------------------------------------------------ */
48      public MultiPartWriter(Writer out)
49           throws IOException
50      {
51          super(out);
52          boundary = "jetty"+System.identityHashCode(this)+
53          Long.toString(System.currentTimeMillis(),36);
54          
55          inPart=false;
56      }
57  
58      /* ------------------------------------------------------------ */
59      /** End the current part.
60       * @exception IOException IOException
61       */
62      @Override
63      public void close()
64           throws IOException
65      {
66          if (inPart)
67              out.write(__CRLF);
68          out.write(__DASHDASH);
69          out.write(boundary);
70          out.write(__DASHDASH);
71          out.write(__CRLF);
72          inPart=false;
73          super.close();
74      }
75      
76      /* ------------------------------------------------------------ */
77      public String getBoundary()
78      {
79          return boundary;
80      }
81      
82      /* ------------------------------------------------------------ */
83      /** Start creation of the next Content.
84       */
85      public void startPart(String contentType)
86           throws IOException
87      {
88          if (inPart)
89              out.write(__CRLF);
90          out.write(__DASHDASH);
91          out.write(boundary);
92          out.write(__CRLF);
93          out.write("Content-Type: ");
94          out.write(contentType);
95          out.write(__CRLF);
96          out.write(__CRLF);
97          inPart=true;
98      }
99      
100     /* ------------------------------------------------------------ */
101     /** end creation of the next Content.
102      */
103     public void endPart()
104          throws IOException
105     {
106         if (inPart)
107             out.write(__CRLF);
108         inPart=false;
109     }
110         
111     /* ------------------------------------------------------------ */
112     /** Start creation of the next Content.
113      */
114     public void startPart(String contentType, String[] headers)
115          throws IOException
116     {
117         if (inPart)
118             out.write(__CRLF);
119         out.write(__DASHDASH);
120         out.write(boundary);
121         out.write(__CRLF);
122         out.write("Content-Type: ");
123         out.write(contentType);
124         out.write(__CRLF);
125         for (int i=0;headers!=null && i<headers.length;i++)
126         {
127             out.write(headers[i]);
128             out.write(__CRLF);
129         }
130         out.write(__CRLF);
131         inPart=true;
132     }
133     
134 }
135 
136 
137 
138