Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / rest / TrashHandler.java @ 3b6b7f25

History | View | Annotate | Download (6.2 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
import gr.ebs.gss.server.ejb.TransactionHelper;
28

    
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;
34

    
35
import javax.servlet.ServletException;
36
import javax.servlet.http.HttpServletRequest;
37
import javax.servlet.http.HttpServletResponse;
38

    
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.json.JSONException;
42
import org.json.JSONObject;
43

    
44

    
45
/**
46
 * A class that handles operations on the 'trash' namespace.
47
 *
48
 * @author past
49
 */
50
public class TrashHandler extends RequestHandler {
51
        /**
52
         * The logger.
53
         */
54
        private static Log logger = LogFactory.getLog(TrashHandler.class);
55

    
56
        /**
57
         * Return the files and folders that are in the trash can.
58
         *
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
63
         */
64
        void serveTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
65
        String path = getInnerPath(req, PATH_TRASH);
66
                if (path.equals(""))
67
                        path = "/";
68

    
69
            if (!path.equals("/")) {
70
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
71
                        return;
72
            }
73

    
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);
80
                    return;
81
            }
82
                try {
83
                        files = getService().getDeletedFiles(user.getId());
84
                        folders = getService().getDeletedRootFolders(user.getId());
85
                } catch (ObjectNotFoundException e) {
86
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
87
                        return;
88
                } catch (RpcException e) {
89
                        logger.error("", e);
90
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
91
                        return;
92
                }
93

    
94
                if (files.isEmpty() && folders.isEmpty()) {
95
                        resp.sendError(HttpServletResponse.SC_NO_CONTENT);
96
                        return;
97
                }
98

    
99
                JSONObject json = new JSONObject();
100
            try {
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());
108
                                trashFolders.add(j);
109
                    }
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("content", f.getMimeType()).
120
                                        put("path", f.getFolder().getPath()).
121
                                        put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
122
                                    put("uri", getApiRoot() + f.getURI());
123
                                JSONObject p = new JSONObject();
124
                                p.put("uri", getApiRoot() + f.getFolder().getURI()).
125
                                                put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
126
                                j.put("folder", p);
127
                                trashFiles.add(j);
128
                    }
129
                    json.put("files", trashFiles);
130
                } catch (JSONException e) {
131
                        logger.error("", e);
132
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
133
                        return;
134
                }
135

    
136
                // Workaround for IE's broken caching behavior.
137
                   resp.setHeader("Expires", "-1");
138
            sendJson(req, resp, json.toString());
139
        }
140

    
141
        /**
142
         * Empties the trash can from any currently stored resource,
143
         * making all trashed files and folders permanently deleted.
144
         *
145
     * @param req The HTTP request we are processing
146
     * @param resp The HTTP response we are processing
147
         * @throws IOException
148
     * @throws IOException if an input/output error occurs
149
         */
150
        public void emptyTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException {
151
        String path = getInnerPath(req, PATH_TRASH);
152
                if (path.equals(""))
153
                        path = "/";
154

    
155
            if (!path.equals("/")) {
156
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
157
                        return;
158
            }
159
                try {
160
                        User user = getUser(req);
161
                        final User owner = getOwner(req);
162
                        if (!owner.equals(user))
163
                                throw new InsufficientPermissionsException("User " + user.getUsername()
164
                                                        + " does not have permission to empty the trash can owned by "
165
                                                        + owner.getUsername());
166
                        if (logger.isDebugEnabled())
167
                                logger.debug("Emptying trash for user " + owner.getUsername());
168
                        new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
169
                                @Override
170
                                public Void call() throws Exception {
171
                                        getService().emptyTrash(owner.getId());
172
                                        return null;
173
                                }
174
                        });
175
                        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
176
                } catch (RpcException e) {
177
                        logger.error("", e);
178
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
179
                } catch (ObjectNotFoundException e) {
180
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
181
                } catch (InsufficientPermissionsException e) {
182
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
183
                } catch (Exception e) {
184
                        logger.error("", e);
185
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
186
                }
187
        }
188

    
189
}