BrightSide Workbench Full Report + Source Code
org.turro.elephant.download.DownloadServlet Class Reference
Inheritance diagram for org.turro.elephant.download.DownloadServlet:
Collaboration diagram for org.turro.elephant.download.DownloadServlet:

Public Member Functions

 DownloadServlet ()
 
void init (ServletConfig config) throws ServletException
 
void destroy ()
 
String getServletInfo ()
 

Protected Member Functions

void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 
void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 
void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 

Detailed Description

Author
Lluis TurrĂ³ Cutiller lluis.nosp@m.@tur.nosp@m.ro.or.nosp@m.g

Definition at line 42 of file DownloadServlet.java.

Constructor & Destructor Documentation

◆ DownloadServlet()

org.turro.elephant.download.DownloadServlet.DownloadServlet ( )

Creates a new instance of DownloadServlet

Definition at line 46 of file DownloadServlet.java.

46  {
47  }

Member Function Documentation

◆ destroy()

void org.turro.elephant.download.DownloadServlet.destroy ( )

Destroys the servlet.

Definition at line 60 of file DownloadServlet.java.

60  {
61 
62  }

◆ doGet()

void org.turro.elephant.download.DownloadServlet.doGet ( HttpServletRequest  request,
HttpServletResponse  response 
) throws ServletException, IOException
protected

Handles the HTTP GET method.

Parameters
requestservlet request
responseservlet response

Definition at line 162 of file DownloadServlet.java.

163  {
164  processRequest(request, response);
165  }
void processRequest(HttpServletRequest request, HttpServletResponse response)

◆ doPost()

void org.turro.elephant.download.DownloadServlet.doPost ( HttpServletRequest  request,
HttpServletResponse  response 
) throws ServletException, IOException
protected

Handles the HTTP POST method.

Parameters
requestservlet request
responseservlet response

Definition at line 172 of file DownloadServlet.java.

173  {
174  processRequest(request, response);
175  }

◆ getServletInfo()

String org.turro.elephant.download.DownloadServlet.getServletInfo ( )

Returns a short description of the servlet.

Definition at line 180 of file DownloadServlet.java.

180  {
181  return "Download servlet";
182  }

◆ init()

void org.turro.elephant.download.DownloadServlet.init ( ServletConfig  config) throws ServletException

Initializes the servlet.

Definition at line 52 of file DownloadServlet.java.

52  {
53  super.init(config);
54 
55  }

◆ processRequest()

void org.turro.elephant.download.DownloadServlet.processRequest ( HttpServletRequest  request,
HttpServletResponse  response 
) throws ServletException, IOException
protected

Processes requests for both HTTP GET and POST methods.

Parameters
requestservlet request
responseservlet response

Definition at line 68 of file DownloadServlet.java.

69  {
70  constructor = ElephantContext.getConstructor(request, response);
71  if(!request.getParameter("down_path").matches("\\@\\-?[0-9\\-]+") &&
72  (constructor.getUser() == null || !constructor.getUser().isInRole("download:file"))) {
73  return;
74  }
75 
76  response.setHeader("pragma", "no-cache");
77  response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
78  response.setHeader("Expires", "01 Apr 1995 01:10:10 GMT");
79 
80  // set download attributes
81  String file = request.getParameter("down_file"),
82  path = request.getParameter("down_path");
83  if(file == null) file = (String) constructor.findAttribute("xp_down_file");
84  if(path != null && path.startsWith("@")) {
85  path = (String) constructor.findAttribute(path);
86  }
87 
88  // append path to a defined root
89  String root = (String) constructor.findAttribute("xp_down_path");
90  path = (root == null ? "" : root) + (path == null ? "" : path);
91 
92  // check whether there are null values
93  if(file == null) {
94  int p = path.lastIndexOf('/');
95  if(p > -1) {
96  file = path.substring(p + 1);
97  path = path.substring(0, p);
98  } else {
99  file = path;
100  path = "";
101  }
102  }
103 
104  // already tried everything!
105  if(file == null) {
106  return;
107  }
108 
109  // translate #root and #user macros
110  if(path != null) {
111  path = path.replaceAll("\\@root", ElephantContext.getRealPath("/"));
112  if(constructor.getUser() != null) {
113  path = path.replaceAll("\\@user", constructor.getUser().getId());
114  }
115  }
116 
117  if(file != null) {
118  if(file.startsWith("/") || file.startsWith("\\")) {
119  file = file.substring(1);
120  }
121  }
122 
123  new File(ElephantContext.getRealPath("/WEB-INF/logs")).mkdirs();
124  File record = new File(ElephantContext.getRealPath("/WEB-INF/logs/downloads.txt"));
125  BufferedWriter bw = new BufferedWriter(new FileWriter(record, record.exists()));
126  bw.write(
127  (constructor.getUser() != null ? constructor.getUser().getId() : "**") + " : " +
128  ObjectString.formatNativeObject(new java.util.Date(), true) +
129  " - " + path + "/" + file +
130  "\n"
131  );
132  bw.close();
133  response.setContentType(new MimetypesFileTypeMap().getContentType(path + "/" + file));
134  response.setContentLength((int) new File(path + "/" + file).length());
135  String onlyFile = file;
136  int p = file.lastIndexOf("/");
137  if(p > -1) {
138  onlyFile = onlyFile.substring(p + 1);
139  }
140  response.setHeader("Content-Disposition", "attachment;filename=\"" + onlyFile.replaceAll(",", "_") + "\"");
141  OutputStream out = response.getOutputStream();
142  FileInputStream fis = new FileInputStream(path + "/" + file);
143 
144  byte buffer[] = new byte[10240];
145  int count;
146  while((count = fis.read(buffer)) > -1) {
147  out.write(buffer, 0, count);
148  }
149  out.flush();
150  out.close();
151  fis.close();
152  bw = new BufferedWriter(new FileWriter(record, record.exists()));
153  bw.write(" >>>>>>>>>>>>>> Complete\n");
154  bw.close();
155  }
boolean isInRole(String role)
Here is the call graph for this function:

The documentation for this class was generated from the following file: