Project

General

Profile

Download (4.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
* Copyright  EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.view;
10

    
11
import java.io.File;
12
import java.io.FileInputStream;
13
import java.io.IOException;
14
import java.io.InputStreamReader;
15
import java.util.Locale;
16
import java.util.Map;
17

    
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20

    
21
import org.apache.commons.io.IOUtils;
22
import org.springframework.web.servlet.View;
23

    
24
/**
25
 * This view will return a simple HTTP file download,
26
 * please specifiy content type, file name, suffix of
27
 * file name and the encoding of the file.
28
 *
29
 *
30
 * @author a.oppermann
31
 * @since 20.03.2014
32
 *
33
 */
34
public class FileDownloadView implements View{
35

    
36
    private String contentType;
37

    
38
    private String fileName;
39

    
40
    private String suffix;
41

    
42
    private String encoding;
43

    
44

    
45

    
46
    /**
47
     * This view will render a simple HTTP file download
48
     *
49
     * <p>
50
     *
51
     * Please make sure you set contentType, fileName, suffix and encoding via setter-methods.
52
     * Otherwise you'll run into an exception.
53
     */
54
    public FileDownloadView(){
55
      //empty constructor
56
    }
57

    
58
    /**
59
     * This view will render a simple HTTP file download with default
60
     * settings for content type (application/octet-stream) and encoding (no encoding).
61
     *
62
     * @param fileName specifiy the name of the file you want to save
63
     * @param suffix file ending like "txt" or "pdf"
64
     */
65
    public FileDownloadView(String fileName, String suffix){
66
        this.contentType = "application/octet-stream";
67
        this.fileName = fileName;
68
        this.suffix = suffix;
69
        this.encoding = null;
70
    }
71

    
72

    
73
    /**
74
     * This view will render a simple HTTP file download.
75
     *
76
     * @param contentType like "text/csv" or "application/pdf"
77
     * @param fileName specifiy the name of the file you want to save
78
     * @param suffix file ending like "txt" or "pdf"
79
     * @param encoding charset like "utf-8"
80
     */
81
    public FileDownloadView(String contentType, String fileName, String suffix, String encoding){
82
        this.contentType = contentType;
83
        this.fileName = fileName;
84
        this.suffix = suffix;
85
        this.encoding = encoding;
86
    }
87

    
88
    @Override
89
    public void render(Map<String, ?> model, HttpServletRequest arg1, HttpServletResponse response) throws Exception {
90
        response.setContentType(getContentType() + (isBinaryData() ? "" : "; charset="+getEncoding().toLowerCase(Locale.ENGLISH)));
91
        if(!isBinaryData()){
92
            response.setCharacterEncoding(getEncoding().toUpperCase(Locale.ENGLISH));
93
        }
94
        response.setHeader("Content-Disposition", "attachment; filename=\""+getFileName()+"."+getSuffix()+"\"");
95

    
96
        //set File
97
        if(model.containsKey("file")){
98
            Object object = model.get("file");
99
            if(object instanceof File){
100
                try{
101
                    File file = (File) object;
102
                    FileInputStream fis = new FileInputStream(file);
103
                    if(!isBinaryData()){
104
                        InputStreamReader isr = new InputStreamReader(fis, getEncoding().toUpperCase(Locale.ENGLISH));
105
                        IOUtils.copy(isr, response.getWriter());
106
                    } else {
107
                        IOUtils.copy(fis, response.getOutputStream());
108
                    }
109
                    response.flushBuffer();
110
                }catch(IOException e){
111
                    throw new IOException("IOERROR writing file to outputstream \n" + e);
112
                }
113
            }
114
        }
115
    }
116

    
117
    @Override
118
    public String getContentType() {
119
        return contentType;
120
    }
121

    
122
    public void setContentType(String contentType){
123
        this.contentType = contentType;
124
    }
125

    
126
    public String getFileName() {
127
        return fileName;
128
    }
129

    
130
    public void setFileName(String fileName){
131
        this.fileName = fileName;
132
    }
133

    
134
    public String getSuffix() {
135
        return suffix;
136
    }
137

    
138
    public void setSuffix(String suffix){
139
        this.suffix = suffix;
140
    }
141

    
142
    public String getEncoding() {
143
        return encoding;
144
    }
145
    public void setEncoding(String encoding) {
146
        this.encoding = encoding;
147
    }
148

    
149
    protected boolean isBinaryData(){
150
        return encoding == null;
151
    }
152
}
(3-3/10)