BrightSide Workbench Full Report + Source Code
DownloadServlet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.download;
19 
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.net.URLDecoder;
23 import java.nio.charset.StandardCharsets;
24 import javax.servlet.ServletException;
25 import javax.servlet.annotation.WebServlet;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.apache.hc.core5.http.HttpStatus;
30 import org.turro.collections.KeyValueMap;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantApplication;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.file.Document;
35 import org.turro.file.Folder;
36 import org.turro.http.Respond;
37 import org.turro.log.SystemLogger;
38 import org.turro.log.WebLoggers;
39 
44 @WebServlet(name = "Download", urlPatterns = {"/down_/*"})
45 public class DownloadServlet extends HttpServlet {
46 
47  private void processDownload(Application application, String path, KeyValueMap map) throws IOException {
48  HttpServletResponse response = application.getHttpServletResponse();
49  try {
50  processResource(application, path, response);
51  } catch (URISyntaxException ex) {
52  WebLoggers.severe(this).exception(ex).log();
53  }
54  }
55 
56  private void processResource(Application application, String path, HttpServletResponse response) throws IOException, URISyntaxException {
57  if(path.contains("/WEB-INF") && !application.isInRole("download:app")) {
58  response.setStatus(HttpStatus.SC_FORBIDDEN);
59  return;
60  } else {
61  Document doc = Document.from(ElephantContext.getRealPath(path));
62  if(doc.exists()) {
63  Respond.to(response).flush(doc.document());
64  SystemLogger.info().entityPath("/file" + path).entityName(doc.name()).comment("downloaded").log();
65  } else {
66  Folder folder = Folder.from(ElephantContext.getRealPath(path));
67  if(folder.exists()) {
68  Respond.to(response).flush(ElephantContext.toUri(ElephantContext.getRelativePath(folder.path().toString())).toURL());
69  }
70  }
71  }
72  }
73 
74  protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
75  KeyValueMap map = new KeyValueMap();
76  for(String key : request.getParameterMap().keySet()) {
77  map.put(key, URLDecoder.decode(request.getParameter(key), StandardCharsets.UTF_8));
78  }
79  processDownload(new ElephantApplication(request, response), request.getPathInfo(), map);
80  }
81 
82  @Override
83  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
84  processRequest(request, response);
85  }
86 
87  @Override
88  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
89  processRequest(request, response);
90  }
91 
92  @Override
93  public String getServletInfo() {
94  return "Download servlet";
95  }
96 
97 }
98 
abstract HttpServletResponse getHttpServletResponse()
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
static ILogWrapper info()
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
ILogWrapper comment(String comment)
ILogWrapper entityPath(String path)
ILogWrapper entityName(String name)