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.InsufficientPermissionsException;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.exceptions.RpcException;
24 import gr.ebs.gss.server.domain.User;
25 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
26 import gr.ebs.gss.server.domain.dto.FolderDTO;
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.util.ArrayList;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.json.JSONException;
39 import org.json.JSONObject;
43 * A class that handles operations on the 'shared' namespace.
47 public class SharedHandler extends RequestHandler {
51 private static Log logger = LogFactory.getLog(SharedHandler.class);
54 * Serve the 'shared' 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 serveShared(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61 String path = getInnerPath(req, PATH_SHARED);
67 User user = getUser(req);
68 User owner = getOwner(req);
69 if (!owner.equals(user))
70 throw new InsufficientPermissionsException("User " + user.getUsername()
71 + " does not have permission to view the resources shared by "
72 + owner.getUsername());
73 JSONObject json = new JSONObject();
75 List<JSONObject> subfolders = new ArrayList<JSONObject>();
76 List<FolderDTO> folders = getService().getSharedRootFolders(owner.getId());
77 for (FolderDTO f: folders) {
78 JSONObject j = new JSONObject();
79 j.put("name", f.getName()).
80 put("uri", getApiRoot() + f.getURI());
81 if (f.getParent() != null)
82 j.put("parent", getApiRoot() + f.getParent().getURI());
85 json.put("folders", subfolders);
87 List<FileHeaderDTO> fileHeaders = getService().getSharedFilesNotInSharedFolders(owner.getId());
88 List<JSONObject> files = new ArrayList<JSONObject>();
89 for (FileHeaderDTO f: fileHeaders) {
90 JSONObject j = new JSONObject();
91 j.put("name", f.getName()).
92 put("owner", f.getOwner().getUsername()).
93 put("deleted", f.isDeleted()).
94 put("version", f.getVersion()).
95 put("size", f.getFileSize()).
96 put("content", f.getMimeType()).
97 put("path", f.getFolder().getPath()).
98 put("shared", f.getShared()).
99 put("versioned",f.isVersioned()).
100 put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
101 put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
102 put("uri", getApiRoot() + f.getURI());
103 JSONObject jf = new JSONObject();
104 jf.put("uri", getApiRoot() + f.getFolder().getURI()).
105 put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
109 json.put("files", files);
111 sendJson(req, resp, json.toString());
112 // Workaround for IE's broken caching behavior.
113 resp.setHeader("Expires", "-1");
114 } catch (ObjectNotFoundException e) {
115 logger.error("User not found", e);
116 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
117 } catch (RpcException e) {
119 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
120 } catch (JSONException e) {
122 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
123 } catch (InsufficientPermissionsException e) {
124 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
127 resp.sendError(HttpServletResponse.SC_NOT_FOUND);