root / src / gr / ebs / gss / server / rest / TrashHandler.java @ 2f1a60e0
History | View | Annotate | Download (6.1 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.server.rest; |
20 |
|
21 |
import gr.ebs.gss.client.exceptions.InsufficientPermissionsException; |
22 |
import gr.ebs.gss.client.exceptions.ObjectNotFoundException; |
23 |
import gr.ebs.gss.client.exceptions.RpcException; |
24 |
import gr.ebs.gss.server.domain.User; |
25 |
import gr.ebs.gss.server.domain.dto.FileHeaderDTO; |
26 |
import gr.ebs.gss.server.domain.dto.FolderDTO; |
27 |
|
28 |
import java.io.IOException; |
29 |
import java.net.URLEncoder; |
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
import java.util.concurrent.Callable; |
33 |
|
34 |
import javax.servlet.ServletException; |
35 |
import javax.servlet.http.HttpServletRequest; |
36 |
import javax.servlet.http.HttpServletResponse; |
37 |
|
38 |
import org.apache.commons.logging.Log; |
39 |
import org.apache.commons.logging.LogFactory; |
40 |
import org.json.JSONException; |
41 |
import org.json.JSONObject; |
42 |
|
43 |
|
44 |
/**
|
45 |
* A class that handles operations on the 'trash' namespace.
|
46 |
*
|
47 |
* @author past
|
48 |
*/
|
49 |
public class TrashHandler extends RequestHandler { |
50 |
/**
|
51 |
* The logger.
|
52 |
*/
|
53 |
private static Log logger = LogFactory.getLog(TrashHandler.class); |
54 |
|
55 |
/**
|
56 |
* Return the files and folders that are in the trash can.
|
57 |
*
|
58 |
* @param req The servlet request we are processing
|
59 |
* @param resp The servlet response we are processing
|
60 |
* @throws IOException if the response cannot be sent
|
61 |
* @throws ServletException
|
62 |
*/
|
63 |
void serveTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { |
64 |
String path = getInnerPath(req, PATH_TRASH);
|
65 |
if (path.equals("")) |
66 |
path = "/";
|
67 |
|
68 |
if (!path.equals("/")) { |
69 |
resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
70 |
return;
|
71 |
} |
72 |
|
73 |
List<FileHeaderDTO> files = null; |
74 |
List<FolderDTO> folders = null; |
75 |
User user = getUser(req); |
76 |
User owner = getOwner(req); |
77 |
if (!owner.equals(user)) {
|
78 |
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); |
79 |
return;
|
80 |
} |
81 |
try {
|
82 |
files = getService().getDeletedFiles(user.getId()); |
83 |
folders = getService().getDeletedRootFolders(user.getId()); |
84 |
} catch (ObjectNotFoundException e) {
|
85 |
resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
86 |
return;
|
87 |
} catch (RpcException e) {
|
88 |
logger.error("", e);
|
89 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
90 |
return;
|
91 |
} |
92 |
|
93 |
if (files.isEmpty() && folders.isEmpty()) {
|
94 |
resp.sendError(HttpServletResponse.SC_NO_CONTENT); |
95 |
return;
|
96 |
} |
97 |
|
98 |
JSONObject json = new JSONObject();
|
99 |
try {
|
100 |
List<JSONObject> trashFolders = new ArrayList<JSONObject>(); |
101 |
for (FolderDTO f: folders) {
|
102 |
JSONObject j = new JSONObject();
|
103 |
j.put("name", f.getName()).
|
104 |
put("uri", getApiRoot() + f.getURI());
|
105 |
if (f.getParent() != null) |
106 |
j.put("parent", getApiRoot() + f.getParent().getURI());
|
107 |
trashFolders.add(j); |
108 |
} |
109 |
json.put("folders", trashFolders);
|
110 |
List<JSONObject> trashFiles = new ArrayList<JSONObject>(); |
111 |
for (FileHeaderDTO f: files) {
|
112 |
JSONObject j = new JSONObject();
|
113 |
j.put("name", f.getName()).
|
114 |
put("owner", f.getOwner().getUsername()).
|
115 |
put("deleted", f.isDeleted()).
|
116 |
put("version", f.getVersion()).
|
117 |
put("size", f.getFileSize()).
|
118 |
put("content", f.getMimeType()).
|
119 |
put("path", f.getFolder().getPath()).
|
120 |
put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
|
121 |
put("uri", getApiRoot() + f.getURI());
|
122 |
JSONObject p = new JSONObject();
|
123 |
p.put("uri", getApiRoot() + f.getFolder().getURI()).
|
124 |
put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8")); |
125 |
j.put("folder", p);
|
126 |
trashFiles.add(j); |
127 |
} |
128 |
json.put("files", trashFiles);
|
129 |
} catch (JSONException e) {
|
130 |
logger.error("", e);
|
131 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
132 |
return;
|
133 |
} |
134 |
|
135 |
// Workaround for IE's broken caching behavior.
|
136 |
resp.setHeader("Expires", "-1"); |
137 |
sendJson(req, resp, json.toString()); |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Empties the trash can from any currently stored resource,
|
142 |
* making all trashed files and folders permanently deleted.
|
143 |
*
|
144 |
* @param req The HTTP request we are processing
|
145 |
* @param resp The HTTP response we are processing
|
146 |
* @throws IOException
|
147 |
* @throws IOException if an input/output error occurs
|
148 |
*/
|
149 |
public void emptyTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
150 |
String path = getInnerPath(req, PATH_TRASH);
|
151 |
if (path.equals("")) |
152 |
path = "/";
|
153 |
|
154 |
if (!path.equals("/")) { |
155 |
resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
156 |
return;
|
157 |
} |
158 |
try {
|
159 |
User user = getUser(req); |
160 |
final User owner = getOwner(req);
|
161 |
if (!owner.equals(user))
|
162 |
throw new InsufficientPermissionsException("User " + user.getUsername() |
163 |
+ " does not have permission to empty the trash can owned by "
|
164 |
+ owner.getUsername()); |
165 |
if (logger.isDebugEnabled())
|
166 |
logger.debug("Emptying trash for user " + owner.getUsername());
|
167 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
168 |
@Override
|
169 |
public Void call() throws Exception { |
170 |
getService().emptyTrash(owner.getId()); |
171 |
return null; |
172 |
} |
173 |
}); |
174 |
resp.setStatus(HttpServletResponse.SC_NO_CONTENT); |
175 |
} catch (RpcException e) {
|
176 |
logger.error("", e);
|
177 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
178 |
} catch (ObjectNotFoundException e) {
|
179 |
resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage()); |
180 |
} catch (InsufficientPermissionsException e) {
|
181 |
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage()); |
182 |
} catch (Exception e) { |
183 |
logger.error("", e);
|
184 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
185 |
} |
186 |
} |
187 |
|
188 |
} |