1255 |
22 Oct 10 |
nicklas |
1 |
/** |
1255 |
22 Oct 10 |
nicklas |
$Id $ |
1255 |
22 Oct 10 |
nicklas |
3 |
|
1255 |
22 Oct 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
1255 |
22 Oct 10 |
nicklas |
5 |
|
1255 |
22 Oct 10 |
nicklas |
This file is part of Bittorent download service for BASE. |
1255 |
22 Oct 10 |
nicklas |
Available at http://baseplugins.thep.lu.se/ |
1255 |
22 Oct 10 |
nicklas |
8 |
|
1255 |
22 Oct 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
1255 |
22 Oct 10 |
nicklas |
modify it under the terms of the GNU General Public License |
1255 |
22 Oct 10 |
nicklas |
as published by the Free Software Foundation; either version 2 |
1255 |
22 Oct 10 |
nicklas |
of the License, or (at your option) any later version. |
1255 |
22 Oct 10 |
nicklas |
13 |
|
1255 |
22 Oct 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1255 |
22 Oct 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1255 |
22 Oct 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1255 |
22 Oct 10 |
nicklas |
GNU General Public License for more details. |
1255 |
22 Oct 10 |
nicklas |
18 |
|
1255 |
22 Oct 10 |
nicklas |
You should have received a copy of the GNU General Public License |
1255 |
22 Oct 10 |
nicklas |
along with this program; if not, write to the Free Software |
1255 |
22 Oct 10 |
nicklas |
Foundation, Inc., 59 Temple Place - Suite 330, |
1255 |
22 Oct 10 |
nicklas |
Boston, MA 02111-1307, USA. |
1255 |
22 Oct 10 |
nicklas |
23 |
*/ |
1255 |
22 Oct 10 |
nicklas |
24 |
package net.sf.basedb.clients.torrent.servlet; |
1255 |
22 Oct 10 |
nicklas |
25 |
|
1255 |
22 Oct 10 |
nicklas |
26 |
import java.io.IOException; |
1255 |
22 Oct 10 |
nicklas |
27 |
import java.io.InputStream; |
1255 |
22 Oct 10 |
nicklas |
28 |
|
1255 |
22 Oct 10 |
nicklas |
29 |
import javax.servlet.ServletException; |
1255 |
22 Oct 10 |
nicklas |
30 |
import javax.servlet.http.HttpServlet; |
1255 |
22 Oct 10 |
nicklas |
31 |
import javax.servlet.http.HttpServletRequest; |
1255 |
22 Oct 10 |
nicklas |
32 |
import javax.servlet.http.HttpServletResponse; |
1255 |
22 Oct 10 |
nicklas |
33 |
|
1255 |
22 Oct 10 |
nicklas |
34 |
import net.sf.basedb.clients.torrent.service.TorrentManager; |
1255 |
22 Oct 10 |
nicklas |
35 |
import net.sf.basedb.clients.torrent.service.TorrentService; |
1255 |
22 Oct 10 |
nicklas |
36 |
import net.sf.basedb.clients.torrent.service.TorrentState; |
1255 |
22 Oct 10 |
nicklas |
37 |
import net.sf.basedb.clients.web.fileupload.FileUpload; |
1255 |
22 Oct 10 |
nicklas |
38 |
import net.sf.basedb.clients.web.fileupload.UploadedFile; |
1255 |
22 Oct 10 |
nicklas |
39 |
import net.sf.basedb.core.Application; |
1255 |
22 Oct 10 |
nicklas |
40 |
import net.sf.basedb.core.DbControl; |
1255 |
22 Oct 10 |
nicklas |
41 |
import net.sf.basedb.core.Directory; |
1255 |
22 Oct 10 |
nicklas |
42 |
import net.sf.basedb.core.ItemParameterType; |
1255 |
22 Oct 10 |
nicklas |
43 |
import net.sf.basedb.core.Job; |
1255 |
22 Oct 10 |
nicklas |
44 |
import net.sf.basedb.core.NotLoggedInException; |
1255 |
22 Oct 10 |
nicklas |
45 |
import net.sf.basedb.core.Permission; |
1255 |
22 Oct 10 |
nicklas |
46 |
import net.sf.basedb.core.SessionControl; |
1255 |
22 Oct 10 |
nicklas |
47 |
import net.sf.basedb.util.FileUtil; |
1255 |
22 Oct 10 |
nicklas |
48 |
import net.sf.basedb.util.Values; |
1255 |
22 Oct 10 |
nicklas |
49 |
|
1255 |
22 Oct 10 |
nicklas |
50 |
|
1255 |
22 Oct 10 |
nicklas |
51 |
/** |
1255 |
22 Oct 10 |
nicklas |
Servlet for handling upload of torrent files to BASE. The servlet creates |
1255 |
22 Oct 10 |
nicklas |
a new job and {@link TorrentManager}. When everything is setup the download |
1255 |
22 Oct 10 |
nicklas |
is started and the user is redirected to the "View job" screen. |
1255 |
22 Oct 10 |
nicklas |
The download progress will be reported to the user through the job |
1255 |
22 Oct 10 |
nicklas |
and so will any errors. |
1255 |
22 Oct 10 |
nicklas |
57 |
|
1255 |
22 Oct 10 |
nicklas |
@author nicklas |
1255 |
22 Oct 10 |
nicklas |
@since 1.0 |
1255 |
22 Oct 10 |
nicklas |
60 |
*/ |
1255 |
22 Oct 10 |
nicklas |
61 |
public class UploadTorrentServlet |
1255 |
22 Oct 10 |
nicklas |
62 |
extends HttpServlet |
1255 |
22 Oct 10 |
nicklas |
63 |
{ |
1255 |
22 Oct 10 |
nicklas |
64 |
|
1255 |
22 Oct 10 |
nicklas |
65 |
|
1255 |
22 Oct 10 |
nicklas |
66 |
private static final long serialVersionUID = 6335465421132496613L; |
1255 |
22 Oct 10 |
nicklas |
67 |
|
1255 |
22 Oct 10 |
nicklas |
68 |
public UploadTorrentServlet() |
1255 |
22 Oct 10 |
nicklas |
69 |
{} |
1255 |
22 Oct 10 |
nicklas |
70 |
|
1255 |
22 Oct 10 |
nicklas |
71 |
@Override |
1255 |
22 Oct 10 |
nicklas |
72 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
1255 |
22 Oct 10 |
nicklas |
73 |
throws ServletException, IOException |
1255 |
22 Oct 10 |
nicklas |
74 |
{ |
1255 |
22 Oct 10 |
nicklas |
// Get the current session & check that a user is logged in |
1255 |
22 Oct 10 |
nicklas |
76 |
String ID = req.getParameter("ID"); |
1255 |
22 Oct 10 |
nicklas |
77 |
String remoteId = req.getRemoteAddr(); |
1255 |
22 Oct 10 |
nicklas |
78 |
SessionControl sc = Application.getSessionControl(ID, remoteId); |
1255 |
22 Oct 10 |
nicklas |
79 |
if (!sc.isLoggedIn()) throw new NotLoggedInException(); |
1255 |
22 Oct 10 |
nicklas |
80 |
|
1255 |
22 Oct 10 |
nicklas |
// Get the torrent service |
1255 |
22 Oct 10 |
nicklas |
82 |
TorrentService service = TorrentService.getInstance(); |
1255 |
22 Oct 10 |
nicklas |
83 |
|
1255 |
22 Oct 10 |
nicklas |
// Prepare the file upload & get form parameters |
1255 |
22 Oct 10 |
nicklas |
85 |
FileUpload upload = new FileUpload(req); |
1255 |
22 Oct 10 |
nicklas |
86 |
UploadedFile torrentUpload = upload.next(); |
1255 |
22 Oct 10 |
nicklas |
87 |
String torrentName = torrentUpload.getFilename(); |
1255 |
22 Oct 10 |
nicklas |
88 |
|
1255 |
22 Oct 10 |
nicklas |
89 |
Job job = null; |
1255 |
22 Oct 10 |
nicklas |
90 |
TorrentManager tm = null; |
1255 |
22 Oct 10 |
nicklas |
91 |
DbControl dc = null; |
1255 |
22 Oct 10 |
nicklas |
92 |
InputStream in = null; |
1255 |
22 Oct 10 |
nicklas |
93 |
try |
1255 |
22 Oct 10 |
nicklas |
94 |
{ |
1255 |
22 Oct 10 |
nicklas |
95 |
dc = sc.newDbControl(); |
1255 |
22 Oct 10 |
nicklas |
// Check that the user may use the selected upload directory |
1255 |
22 Oct 10 |
nicklas |
97 |
int directoryId = Values.getInt(req.getParameter("directory_id")); |
1255 |
22 Oct 10 |
nicklas |
98 |
Directory uploadDir = Directory.getById(dc, directoryId); |
1255 |
22 Oct 10 |
nicklas |
99 |
uploadDir.checkPermission(Permission.USE); |
1255 |
22 Oct 10 |
nicklas |
100 |
|
1255 |
22 Oct 10 |
nicklas |
// Store the torrent file |
1255 |
22 Oct 10 |
nicklas |
102 |
in = torrentUpload.getInputStream(); |
1255 |
22 Oct 10 |
nicklas |
103 |
tm = service.newTorrentManager(torrentName); |
1255 |
22 Oct 10 |
nicklas |
104 |
tm.setTorrent(in); |
1255 |
22 Oct 10 |
nicklas |
105 |
|
1255 |
22 Oct 10 |
nicklas |
// Create a job that is used to handle the torrent upload |
1255 |
22 Oct 10 |
nicklas |
107 |
job = Job.getNew(dc, null, null, null); |
1255 |
22 Oct 10 |
nicklas |
108 |
job.setName("Torrent upload: " + torrentName); |
1255 |
22 Oct 10 |
nicklas |
109 |
job.setParameterValue("directory", "Directory", |
1255 |
22 Oct 10 |
nicklas |
110 |
"The directory in the BASE file system were the uploaded files should be stored", |
1255 |
22 Oct 10 |
nicklas |
111 |
new ItemParameterType<Directory>(Directory.class, null), |
1255 |
22 Oct 10 |
nicklas |
112 |
uploadDir); |
1255 |
22 Oct 10 |
nicklas |
113 |
dc.saveItem(job); |
1255 |
22 Oct 10 |
nicklas |
114 |
dc.commit(); |
1255 |
22 Oct 10 |
nicklas |
115 |
|
1255 |
22 Oct 10 |
nicklas |
116 |
tm.setJob(job); |
1255 |
22 Oct 10 |
nicklas |
117 |
tm.queueDownload(); |
1255 |
22 Oct 10 |
nicklas |
118 |
} |
1255 |
22 Oct 10 |
nicklas |
119 |
finally |
1255 |
22 Oct 10 |
nicklas |
120 |
{ |
1255 |
22 Oct 10 |
nicklas |
121 |
if (dc != null) dc.close(); |
1255 |
22 Oct 10 |
nicklas |
122 |
FileUtil.close(in); |
1255 |
22 Oct 10 |
nicklas |
123 |
if (tm != null && tm.getState() == TorrentState.INITIAL) tm.close(); |
1255 |
22 Oct 10 |
nicklas |
124 |
} |
1255 |
22 Oct 10 |
nicklas |
125 |
|
1255 |
22 Oct 10 |
nicklas |
126 |
int jobId = tm.getJobId(); |
1255 |
22 Oct 10 |
nicklas |
127 |
resp.sendRedirect(req.getContextPath() + "/views/jobs/index.jsp?ID=" + ID + "&cmd=ViewItem&item_id=" + jobId); |
1255 |
22 Oct 10 |
nicklas |
128 |
} |
1255 |
22 Oct 10 |
nicklas |
129 |
|
1255 |
22 Oct 10 |
nicklas |
130 |
} |