View Javadoc

1   /**
2    * This file is part of the equanda project.
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 1.1 (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 http://www.mozilla.org/MPL/
7    *
8    * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9    * ANY KIND, either express or implied. See the License for the specific language governing rights and
10   * limitations under the License.
11   *
12   * Alternatively, the contents of this file may be used under the terms of
13   * either the GNU General Public License Version 2 or later (the "GPL"), or
14   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15   * in which case the provisions of the GPL or the LGPL are applicable instead
16   * of those above. If you wish to allow use of your version of this file only
17   * under the terms of either the GPL or the LGPL, and not to allow others to
18   * use your version of this file under the terms of the MPL, indicate your
19   * decision by deleting the provisions above and replace them with the notice
20   * and other provisions required by the GPL or the LGPL. If you do not delete
21   * the provisions above, a recipient may use your version of this file under
22   * the terms of any one of the MPL, the GPL or the LGPL.
23   */
24  
25  package org.equanda.tapestry5.data;
26  
27  import org.apache.tapestry5.StreamResponse;
28  import org.apache.tapestry5.services.Response;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  /**
35   * Container for a (string) message
36   *
37   * @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
38   */
39  public class BinaryStreamResponse
40      implements StreamResponse
41  {
42      private String mimeType;
43      private String fileName;
44      private byte[] data;
45  
46      public BinaryStreamResponse( byte[] data, String mime )
47      {
48          this.data = data;
49          this.mimeType = mime;
50      }
51  
52      public BinaryStreamResponse( byte[] data, String mime, String fileName )
53      {
54          this.data = data;
55          this.mimeType = mime;
56          this.fileName = fileName;
57      }
58  
59      public String getContentType()
60      {
61          return mimeType;
62      }
63  
64      public InputStream getStream()
65          throws IOException
66      {
67          return new ByteArrayInputStream( data );
68      }
69  
70      public void prepareResponse( Response response )
71      {
72          if ( null != fileName ) response.setHeader( "Content-Disposition", "attachment; filename=" + fileName );
73          if ( null != mimeType ) response.setHeader( "Content-Type", mimeType );
74          response.setHeader( "Expires", "0" );
75          response.setHeader( "Cache-Control", "must-revalidate, post-check=0, pre-check=0" );
76          response.setHeader( "Pragma", "public" );
77      }
78  }