root / src / gr / ebs / gss / server / rest / OthersHandler.java @ 086c7250
History | View | Annotate | Download (5.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 gr.ebs.gss.server.rest; |
20 |
|
21 |
import gr.ebs.gss.common.exceptions.ObjectNotFoundException; |
22 |
import gr.ebs.gss.common.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 |
put("shared", f.getShared());
|
116 |
subfolders.add(j); |
117 |
} |
118 |
json.put("folders", subfolders);
|
119 |
|
120 |
List<JSONObject> files = new ArrayList<JSONObject>(); |
121 |
List<FileHeader> fileHeaders = getService().getSharedFiles(other.getId(), owner.getId());
|
122 |
for (FileHeader 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.getCurrentBody().getVersion()).
|
128 |
put("size", f.getCurrentBody().getFileSize()).
|
129 |
put("content", f.getCurrentBody().getMimeType()).
|
130 |
put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
|
131 |
put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
|
132 |
put("path", f.getFolder().getPath()).
|
133 |
put("shared", f.getShared()).
|
134 |
put("versioned",f.isVersioned()).
|
135 |
put("uri", getApiRoot() + f.getURI());
|
136 |
files.add(j); |
137 |
} |
138 |
json.put("files", files);
|
139 |
|
140 |
sendJson(req, resp, json.toString()); |
141 |
} catch (ObjectNotFoundException e) {
|
142 |
logger.error("User not found", e);
|
143 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
144 |
return;
|
145 |
} catch (RpcException e) {
|
146 |
logger.error("", e);
|
147 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
148 |
return;
|
149 |
} catch (JSONException e) {
|
150 |
logger.error("", e);
|
151 |
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
152 |
return;
|
153 |
} |
154 |
|
155 |
resp.setHeader("Expires", "-1"); |
156 |
} |
157 |
|
158 |
} |