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.ObjectNotFoundException;
22 import gr.ebs.gss.client.exceptions.RpcException;
23 import gr.ebs.gss.server.domain.FileHeader;
24 import gr.ebs.gss.server.domain.Folder;
25 import gr.ebs.gss.server.domain.User;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.json.JSONArray;
37 import org.json.JSONException;
38 import org.json.JSONObject;
42 * A class that handles operations on the 'others' namespace.
46 public class OthersHandler extends RequestHandler {
50 private static Log logger = LogFactory.getLog(OthersHandler.class);
53 * Serve the 'others' namespace for the user.
55 * @param req The servlet request we are processing
56 * @param resp The servlet response we are processing
57 * @throws IOException if an input/output error occurs
59 void serveOthers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
60 String parentUrl = getContextPath(req, true);
61 String path = getInnerPath(req, PATH_OTHERS);
65 User user = getUser(req);
66 User owner = getOwner(req);
67 if (!owner.equals(user)) {
68 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
73 // Request to retrieve the other users who have shared resources to this user
74 JSONArray json = new JSONArray();
76 List<User> others = getService().getUsersSharingFoldersForUser(owner.getId());
77 for (User u: others) {
78 JSONObject j = new JSONObject();
79 j.put("username", u.getUsername()).
80 put("uri", parentUrl + u.getUsername());
84 sendJson(req, resp, json.toString());
85 } catch (ObjectNotFoundException e) {
86 logger.error("User not found", e);
87 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
89 } catch (RpcException e) {
91 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
93 } catch (JSONException e) {
95 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
100 // Request to retrieve the files and folders shared by the other user
101 // Chop any trailing slash
102 path = path.endsWith("/")? path.substring(0, path.length()-1): path;
103 // Chop any leading slash
104 path = path.startsWith("/")? path.substring(1): path;
106 User other = getService().findUser(path);
107 JSONObject json = new JSONObject();
109 List<JSONObject> subfolders = new ArrayList<JSONObject>();
110 List<Folder> folders = getService().getSharedRootFolders(other.getId(), owner.getId());
111 for (Folder f: folders) {
112 JSONObject j = new JSONObject();
113 j.put("name", f.getName()).
114 put("uri", getApiRoot() + f.getURI());
117 json.put("folders", subfolders);
119 List<JSONObject> files = new ArrayList<JSONObject>();
120 List<FileHeader> fileHeaders = getService().getSharedFiles(other.getId(), owner.getId());
121 for (FileHeader f: fileHeaders) {
122 JSONObject j = new JSONObject();
123 j.put("name", f.getName()).
124 put("owner", f.getOwner().getUsername()).
125 put("deleted", f.isDeleted()).
126 put("version", f.getCurrentBody().getVersion()).
127 put("size", f.getCurrentBody().getFileSize()).
128 put("content", f.getCurrentBody().getMimeType()).
129 put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
130 put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
131 put("path", f.getFolder().getPath()).
132 put("shared", f.getShared()).
133 put("versioned",f.isVersioned()).
134 put("uri", getApiRoot() + f.getURI());
137 json.put("files", files);
139 sendJson(req, resp, json.toString());
140 } catch (ObjectNotFoundException e) {
141 logger.error("User not found", e);
142 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
144 } catch (RpcException e) {
146 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
148 } catch (JSONException e) {
150 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
154 resp.setHeader("Expires", "-1");