Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / rest / TrashHandler.java @ 1206:292dec4eae08

History | View | Annotate | Download (6.4 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 org.gss_project.gss.server.rest;
20

    
21
import org.gss_project.gss.common.exceptions.InsufficientPermissionsException;
22
import org.gss_project.gss.common.exceptions.ObjectNotFoundException;
23
import org.gss_project.gss.common.exceptions.RpcException;
24
import org.gss_project.gss.server.domain.FileHeader;
25
import org.gss_project.gss.server.domain.Folder;
26
import org.gss_project.gss.server.domain.User;
27
import org.gss_project.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<FileHeader> files = null;
75
                List<Folder> 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 (Folder 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 (FileHeader 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.getCurrentBody().getVersion()).
118
                                        put("size", f.getCurrentBody().getFileSize()).
119
                                        put("content", f.getCurrentBody().getMimeType()).
120
                                        put("shared", f.getShared()).
121
                                    put("versioned",f.isVersioned()).
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"));
129
                                j.put("folder", p);
130
                                trashFiles.add(j);
131
                    }
132
                    json.put("files", trashFiles);
133
                } catch (JSONException e) {
134
                        logger.error("", e);
135
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
136
                        return;
137
                }
138

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

    
144
        /**
145
         * Empties the trash can from any currently stored resource,
146
         * making all trashed files and folders permanently deleted.
147
         *
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
152
         */
153
        public void emptyTrash(HttpServletRequest req, HttpServletResponse resp) throws IOException {
154
        String path = getInnerPath(req, PATH_TRASH);
155
                if (path.equals(""))
156
                        path = "/";
157

    
158
            if (!path.equals("/")) {
159
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
160
                        return;
161
            }
162
                try {
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>() {
172
                                @Override
173
                                public Void call() throws Exception {
174
                                        getService().emptyTrash(owner.getId());
175
                                        return null;
176
                                }
177
                        });
178
                        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
179
                } catch (RpcException e) {
180
                        logger.error("", 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) {
187
                        logger.error("", e);
188
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
189
                }
190
        }
191

    
192
}