View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.resource;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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          // Make sure type and filename are fiiled
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          // Set mime type
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          // Set filename
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 }