001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2009 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.utils;
016    
017    import java.io.IOException;
018    import java.io.InputStream;
019    
020    import org.apache.tapestry5.StreamResponse;
021    import org.apache.tapestry5.services.Response;
022    
023    /**
024     * stolen from Tapestry wiki page.
025     *
026     * @version $Id: InlineStreamResponse.java 594 2009-12-05 15:17:26Z mlusetti $
027     */
028    public class InlineStreamResponse implements StreamResponse
029    {
030            private InputStream is = null;
031            protected String contentType = "text/plain";// this is the default
032            protected String extension = "txt";
033            protected String filename = "default";
034    
035            public InlineStreamResponse(InputStream is, String... args)
036            {
037                    this.is = is;
038                    if (args != null)
039                    {
040                            this.filename = args[0];
041                    }
042            }
043    
044            public String getContentType()
045            {
046                    return contentType;
047            }
048    
049            public InputStream getStream() throws IOException
050            {
051                    return is;
052            }
053    
054            public void prepareResponse(Response arg0)
055            {
056                    arg0.setHeader("Content-Disposition", "inline; filename=" + filename
057                                    + ((extension == null) ? "" : ("." + extension)));
058            }
059    }