Hi Daniel,

What exactly is the issue with your code? We use something similar (and content 
type "application/pdf"); see below.

I'm sure you will take care of it when the code works, but your implementation 
is extremely dangerous :-)

Best,
Thilo

public abstract class AbstractStreamResponse implements StreamResponse
{
    private String contentType;
    private String filename;

    public AbstractStreamResponse(String contentType)
    {
        this.contentType = contentType;
    }

    public void setFilename(String filename)
    {
        this.filename = filename;
    }

    @Override
    public String getContentType()
    {
        return contentType;
    }

    @Override
    public void prepareResponse(Response response)
    {
        if(!Strings.isNullOrEmpty(filename)) {
            try {
                String encodedFilename = URLEncoder.encode(filename, "UTF-8");
                response.setHeader("Content-Disposition", 
String.format("attachment; filename=%s", encodedFilename));
            } catch (UnsupportedEncodingException e) {
                // skip
            }
        }
    }
}



________________________________________
From: Poggenpohl, Daniel <daniel.poggenp...@isst.fraunhofer.de>
Sent: Thursday, February 19, 2015 15:06
To: Tapestry users
Subject: AW: PDF Viewer component

Hi,

a little bit more information (read: code)...

I know the code is imperfect but this was just a quick and dirty setup to try 
inline document viewing.

My Viewer component:

public class Viewer {

        @Parameter(required=true)
        @Property
        String documentUrl;

        @Inject
        Logger logger;

        public StreamResponse showDocument() {
                if ("".equals(documentUrl)) {
                        return null;
                }
                File pdf = new File(documentUrl);
                return new InlineViewingStreamResponse (pdf);
        }

My StreamResponse implementation:

public class InlineViewingStreamResponse implements StreamResponse {

        File file;

        public FileAttachment(File file) throws FileNotFoundException {
                if (file != null && file.exists() && file.isFile()) {
                        this.file = file;
                } else {
                        throw new FileNotFoundException();
                }
        }

        @Override
        public String getContentType() {
                try {
                        return Files.probeContentType(file.toPath());
                } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                }
        }

        @Override
        public InputStream getStream() throws IOException {
                return new BufferedInputStream(new FileInputStream(file));
        }

        @Override
        public void prepareResponse(Response response) {
                response.setHeader("Content-Disposition", "inline; filename=" + 
file.getName());
        }

Regards,
Daniel P.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to