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.User;
24 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
25 import gr.ebs.gss.server.domain.dto.FolderDTO;
26 import gr.ebs.gss.server.domain.dto.UserDTO;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.json.JSONArray;
38 import org.json.JSONException;
39 import org.json.JSONObject;
43 * A class that handles operations on the 'others' namespace.
47 public class OthersHandler extends RequestHandler {
51 private static Log logger = LogFactory.getLog(OthersHandler.class);
54 * Serve the 'others' namespace for the user.
56 * @param req The servlet request we are processing
57 * @param resp The servlet response we are processing
58 * @throws IOException if an input/output error occurs
60 void serveOthers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61 String parentUrl = getContextPath(req, true);
62 String path = getInnerPath(req, PATH_OTHERS);
66 User user = getUser(req);
67 User owner = getOwner(req);
68 if (!owner.equals(user)) {
69 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
74 // Request to retrieve the other users who have shared resources to this user
75 JSONArray json = new JSONArray();
77 List<UserDTO> others = getService().getUsersSharingFoldersForUser(owner.getId());
78 for (UserDTO u: others) {
79 JSONObject j = new JSONObject();
80 j.put("username", u.getUsername()).
81 put("uri", parentUrl + u.getUsername());
85 sendJson(req, resp, json.toString());
86 } catch (ObjectNotFoundException e) {
87 logger.error("User not found", e);
88 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
90 } catch (RpcException e) {
92 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
94 } catch (JSONException e) {
96 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
101 // Request to retrieve the files and folders shared by the other user
102 // Chop any trailing slash
103 path = path.endsWith("/")? path.substring(0, path.length()-1): path;
104 // Chop any leading slash
105 path = path.startsWith("/")? path.substring(1): path;
107 User other = getService().findUser(path);
108 JSONObject json = new JSONObject();
110 List<JSONObject> subfolders = new ArrayList<JSONObject>();
111 List<FolderDTO> folders = getService().getSharedRootFolders(other.getId(), owner.getId());
112 for (FolderDTO f: folders) {
113 JSONObject j = new JSONObject();
114 j.put("name", f.getName()).
115 put("uri", getApiRoot() + f.getURI());
118 json.put("folders", subfolders);
120 List<JSONObject> files = new ArrayList<JSONObject>();
121 List<FileHeaderDTO> fileHeaders = getService().getSharedFiles(other.getId(), owner.getId());
122 for (FileHeaderDTO f: fileHeaders) {
123 JSONObject j = new JSONObject();
124 j.put("name", f.getName()).
125 put("owner", f.getOwner().getUsername()).
126 put("deleted", f.isDeleted()).
127 put("version", f.getVersion()).
128 put("size", f.getFileSize()).
129 put("content", f.getMimeType()).
130 put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
131 put("path", f.getFolder().getPath()).
132 put("uri", getApiRoot() + f.getURI());
135 json.put("files", files);
137 sendJson(req, resp, json.toString());
138 } catch (ObjectNotFoundException e) {
139 logger.error("User not found", e);
140 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
142 } catch (RpcException e) {
144 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
146 } catch (JSONException e) {
148 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
152 resp.setHeader("Expires", "-1");