1 package fr.ifremer.quadrige2.synchro.server.resource;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.quadrige2.synchro.server.application.WebSession;
28 import fr.ifremer.quadrige2.synchro.server.config.SynchroServerConfiguration;
29 import fr.ifremer.quadrige2.synchro.server.security.SecurityContextHelper;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.FilenameUtils;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.wicket.WicketRuntimeException;
35 import org.apache.wicket.request.resource.AbstractResource;
36 import org.springframework.util.MimeTypeUtils;
37
38 import java.io.*;
39
40 public class DownloadFileRessource extends AbstractResource {
41
42 private static final long serialVersionUID = 1L;
43
44 public static final String PARAM_TYPE = "type";
45 public static final String PARAM_FILE = "file";
46
47 @Override
48 protected ResourceResponse newResourceResponse(Attributes attributes) {
49
50 String type = attributes.getParameters().get(PARAM_TYPE).toString();
51 String fileName = attributes.getParameters().get(PARAM_FILE).toString();
52 ResourceResponse resourceResponse = new ResourceResponse();
53
54
55 if (StringUtils.isBlank(type) || StringUtils.isBlank(fileName)) {
56 resourceResponse.setError(404);
57 return resourceResponse;
58 }
59
60 final File file = getFile(type, fileName);
61 if (file == null || !file.exists()) {
62 resourceResponse.setError(404);
63 return resourceResponse;
64 }
65
66
67 String contentType = "application/download";
68 if (FilenameUtils.isExtension(file.getName(), "properties")) {
69 contentType = MimeTypeUtils.TEXT_PLAIN.getType();
70 }
71 resourceResponse.setContentType(contentType);
72
73
74 resourceResponse.setFileName(fileName);
75
76 long fileLength = FileUtils.sizeOf(file);
77 resourceResponse.setContentLength(fileLength);
78
79 resourceResponse.setWriteCallback(new WriteCallback() {
80 @Override
81 public void writeData(Attributes attributes) throws IOException {
82 OutputStream os = attributes.getResponse().getOutputStream();
83 InputStream is = new BufferedInputStream(new FileInputStream(file));
84
85 try {
86 IOUtils.copyLarge(is, os);
87
88 } catch (IOException e) {
89 throw new WicketRuntimeException("Error while writing file content to response", e);
90 }
91 finally {
92 IOUtils.closeQuietly(os);
93 IOUtils.closeQuietly(is);
94 }
95 }
96 });
97
98 return resourceResponse;
99 }
100
101 public File getFile(String type, String filename) {
102 SynchroServerConfiguration config = SynchroServerConfiguration.getInstance();
103
104 int userId = SecurityContextHelper.getPrincipalUserId();
105 if ("import".equalsIgnoreCase(type)) {
106 return new File(config.getSynchroImportDirectoryByUser(userId), filename);
107 }
108 if ("export".equalsIgnoreCase(type)) {
109 return new File(config.getSynchroExportDirectoryByUser(userId), filename);
110 }
111 if ("install".equalsIgnoreCase(type)) {
112 return new File(config.getSynchroDirectory(), filename);
113 }
114
115 return null;
116 }
117 }