Removed all DTO where possible
[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.FileHeader;
24 import gr.ebs.gss.server.domain.Folder;
25 import gr.ebs.gss.server.domain.User;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
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;
39
40
41 /**
42  * A class that handles operations on the 'others' namespace.
43  *
44  * @author past
45  */
46 public class OthersHandler extends RequestHandler {
47         /**
48          * The logger.
49          */
50         private static Log logger = LogFactory.getLog(OthersHandler.class);
51
52         /**
53      * Serve the 'others' namespace for the user.
54      *
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
58          */
59         void serveOthers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
60         String parentUrl = getContextPath(req, true);
61         String path = getInnerPath(req, PATH_OTHERS);
62                 if (path.equals(""))
63                         path = "/";
64
65         User user = getUser(req);
66         User owner = getOwner(req);
67         if (!owner.equals(user)) {
68                 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
69                 return;
70         }
71         if (path.equals("/"))
72                         try {
73                         // Request to retrieve the other users who have shared resources to this user
74                         JSONArray json = new JSONArray();
75
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());
81                                 json.put(j);
82                         }
83
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);
88                         return;
89                 } catch (RpcException e) {
90                         logger.error("", e);
91                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
92                         return;
93                         } catch (JSONException e) {
94                                 logger.error("", e);
95                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
96                                 return;
97                         }
98                 else
99                         try {
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;
105
106                         User other = getService().findUser(path);
107                         JSONObject json = new JSONObject();
108
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());
115                                 subfolders.add(j);
116                         }
117                         json.put("folders", subfolders);
118
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());
135                                 files.add(j);
136                 }
137                 json.put("files", files);
138
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);
143                         return;
144                 } catch (RpcException e) {
145                         logger.error("", e);
146                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
147                         return;
148                         } catch (JSONException e) {
149                                 logger.error("", e);
150                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
151                                 return;
152                         }
153
154                 resp.setHeader("Expires", "-1");
155         }
156
157 }