- Check when renaming file that name doesn't already exist.
[pithos] / src / gr / ebs / gss / server / rest / OthersHandler.java
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.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;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
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;
40
41
42 /**
43  * A class that handles operations on the 'others' namespace.
44  *
45  * @author past
46  */
47 public class OthersHandler extends RequestHandler {
48         /**
49          * The logger.
50          */
51         private static Log logger = LogFactory.getLog(OthersHandler.class);
52
53         /**
54      * Serve the 'others' namespace for the user.
55      *
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
59          */
60         void serveOthers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61         String parentUrl = getContextPath(req, true);
62         String path = getInnerPath(req, PATH_OTHERS);
63                 if (path.equals(""))
64                         path = "/";
65
66         User user = getUser(req);
67         User owner = getOwner(req);
68         if (!owner.equals(user)) {
69                 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
70                 return;
71         }
72         if (path.equals("/"))
73                         try {
74                         // Request to retrieve the other users who have shared resources to this user
75                         JSONArray json = new JSONArray();
76
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());
82                                 json.put(j);
83                         }
84
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);
89                         return;
90                 } catch (RpcException e) {
91                         logger.error("", e);
92                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
93                         return;
94                         } catch (JSONException e) {
95                                 logger.error("", e);
96                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
97                                 return;
98                         }
99                 else
100                         try {
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;
106
107                         User other = getService().findUser(path);
108                         JSONObject json = new JSONObject();
109
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());
116                                 subfolders.add(j);
117                         }
118                         json.put("folders", subfolders);
119
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("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
132                                         put("path", f.getFolder().getPath()).
133                                         put("uri", getApiRoot() + f.getURI());
134                                 files.add(j);
135                 }
136                 json.put("files", files);
137
138                 sendJson(req, resp, json.toString());
139                 } catch (ObjectNotFoundException e) {
140                         logger.error("User not found", e);
141                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
142                         return;
143                 } catch (RpcException e) {
144                         logger.error("", e);
145                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
146                         return;
147                         } catch (JSONException e) {
148                                 logger.error("", e);
149                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
150                                 return;
151                         }
152
153                 resp.setHeader("Expires", "-1");
154         }
155
156 }