2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.server.rest;
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 import gr.ebs.gss.server.ejb.TransactionHelper;
29 import java.io.IOException;
30 import java.net.URLEncoder;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.concurrent.Callable;
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.json.JSONException;
42 import org.json.JSONObject;
46 * A class that handles operations on the 'trash' namespace.
50 public class TrashHandler extends RequestHandler {
54 private static Log logger = LogFactory.getLog(TrashHandler.class);
57 * Return the files and folders that are in the trash can.
59 * @param req The servlet request we are processing
60 * @param resp The servlet response we are processing
61 * @throws IOException if the response cannot be sent
62 * @throws ServletException
64 void serveTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
65 String path = getInnerPath(req, PATH_TRASH);
69 if (!path.equals("/")) {
70 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
74 List<FileHeaderDTO> files = null;
75 List<FolderDTO> folders = null;
76 User user = getUser(req);
77 User owner = getOwner(req);
78 if (!owner.equals(user)) {
79 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
83 files = getService().getDeletedFiles(user.getId());
84 folders = getService().getDeletedRootFolders(user.getId());
85 } catch (ObjectNotFoundException e) {
86 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
88 } catch (RpcException e) {
90 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
94 if (files.isEmpty() && folders.isEmpty()) {
95 resp.sendError(HttpServletResponse.SC_NO_CONTENT);
99 JSONObject json = new JSONObject();
101 List<JSONObject> trashFolders = new ArrayList<JSONObject>();
102 for (FolderDTO f: folders) {
103 JSONObject j = new JSONObject();
104 j.put("name", f.getName()).
105 put("uri", getApiRoot() + f.getURI());
106 if (f.getParent() != null)
107 j.put("parent", getApiRoot() + f.getParent().getURI());
110 json.put("folders", trashFolders);
111 List<JSONObject> trashFiles = new ArrayList<JSONObject>();
112 for (FileHeaderDTO f: files) {
113 JSONObject j = new JSONObject();
114 j.put("name", f.getName()).
115 put("owner", f.getOwner().getUsername()).
116 put("deleted", f.isDeleted()).
117 put("version", f.getVersion()).
118 put("size", f.getFileSize()).
119 put("shared", f.getShared()).
120 put("versioned",f.isVersioned()).
121 put("content", f.getMimeType()).
122 put("path", f.getFolder().getPath()).
123 put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
124 put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
125 put("uri", getApiRoot() + f.getURI());
126 JSONObject p = new JSONObject();
127 p.put("uri", getApiRoot() + f.getFolder().getURI()).
128 put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
132 json.put("files", trashFiles);
133 } catch (JSONException e) {
135 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
139 // Workaround for IE's broken caching behavior.
140 resp.setHeader("Expires", "-1");
141 sendJson(req, resp, json.toString());
145 * Empties the trash can from any currently stored resource,
146 * making all trashed files and folders permanently deleted.
148 * @param req The HTTP request we are processing
149 * @param resp The HTTP response we are processing
150 * @throws IOException
151 * @throws IOException if an input/output error occurs
153 public void emptyTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException {
154 String path = getInnerPath(req, PATH_TRASH);
158 if (!path.equals("/")) {
159 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
163 User user = getUser(req);
164 final User owner = getOwner(req);
165 if (!owner.equals(user))
166 throw new InsufficientPermissionsException("User " + user.getUsername()
167 + " does not have permission to empty the trash can owned by "
168 + owner.getUsername());
169 if (logger.isDebugEnabled())
170 logger.debug("Emptying trash for user " + owner.getUsername());
171 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
173 public Void call() throws Exception {
174 getService().emptyTrash(owner.getId());
178 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
179 } catch (RpcException e) {
181 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
182 } catch (ObjectNotFoundException e) {
183 resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
184 } catch (InsufficientPermissionsException e) {
185 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
186 } catch (Exception e) {
188 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);