Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / rest / SharedHandler.java @ 41ccd791

History | View | Annotate | Download (4.7 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.server.rest;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
22 14ad7326 pastith
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 14ad7326 pastith
import gr.ebs.gss.client.exceptions.RpcException;
24 14ad7326 pastith
import gr.ebs.gss.server.domain.User;
25 2f2da9c7 pastith
import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
26 2f2da9c7 pastith
import gr.ebs.gss.server.domain.dto.FolderDTO;
27 14ad7326 pastith
28 14ad7326 pastith
import java.io.IOException;
29 13571ef8 pastith
import java.net.URLEncoder;
30 14ad7326 pastith
import java.util.ArrayList;
31 14ad7326 pastith
import java.util.List;
32 14ad7326 pastith
33 14ad7326 pastith
import javax.servlet.http.HttpServletRequest;
34 14ad7326 pastith
import javax.servlet.http.HttpServletResponse;
35 14ad7326 pastith
36 14ad7326 pastith
import org.apache.commons.logging.Log;
37 14ad7326 pastith
import org.apache.commons.logging.LogFactory;
38 14ad7326 pastith
import org.json.JSONException;
39 14ad7326 pastith
import org.json.JSONObject;
40 14ad7326 pastith
41 14ad7326 pastith
42 14ad7326 pastith
/**
43 14ad7326 pastith
 * A class that handles operations on the 'shared' namespace.
44 14ad7326 pastith
 *
45 14ad7326 pastith
 * @author past
46 14ad7326 pastith
 */
47 14ad7326 pastith
public class SharedHandler extends RequestHandler {
48 14ad7326 pastith
        /**
49 14ad7326 pastith
         * The logger.
50 14ad7326 pastith
         */
51 14ad7326 pastith
        private static Log logger = LogFactory.getLog(SharedHandler.class);
52 14ad7326 pastith
53 14ad7326 pastith
        /**
54 14ad7326 pastith
     * Serve the 'shared' namespace for the user.
55 14ad7326 pastith
     *
56 14ad7326 pastith
     * @param req The servlet request we are processing
57 14ad7326 pastith
     * @param resp The servlet response we are processing
58 14ad7326 pastith
     * @throws IOException if an input/output error occurs
59 14ad7326 pastith
         */
60 14ad7326 pastith
        void serveShared(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61 14ad7326 pastith
        String path = getInnerPath(req, PATH_SHARED);
62 14ad7326 pastith
                if (path.equals(""))
63 14ad7326 pastith
                        path = "/";
64 14ad7326 pastith
65 14ad7326 pastith
            if (path.equals("/"))
66 14ad7326 pastith
                        try {
67 14ad7326 pastith
                            User user = getUser(req);
68 14ad7326 pastith
                        User owner = getOwner(req);
69 14ad7326 pastith
                        if (!owner.equals(user))
70 14ad7326 pastith
                                throw new InsufficientPermissionsException("User " + user.getUsername()
71 14ad7326 pastith
                                                        + " does not have permission to view the resources shared by "
72 14ad7326 pastith
                                                        + owner.getUsername());
73 14ad7326 pastith
                        JSONObject json = new JSONObject();
74 14ad7326 pastith
75 f7abcd5a pastith
                                List<JSONObject> subfolders = new ArrayList<JSONObject>();
76 8c4469ce koutsoub
                        List<FolderDTO> folders = getService().getSharedRootFolders(owner.getId());
77 f7abcd5a pastith
                        for (FolderDTO f: folders) {
78 f7abcd5a pastith
                                JSONObject j = new JSONObject();
79 f7abcd5a pastith
                                j.put("name", f.getName()).
80 2b90ab8a pastith
                                        put("uri", getApiRoot() + f.getURI());
81 175ec038 pastith
                                if (f.getParent() != null)
82 865f1d02 pastith
                                        j.put("parent", getApiRoot() + f.getParent().getURI());
83 f7abcd5a pastith
                                    subfolders.add(j);
84 f7abcd5a pastith
                        }
85 14ad7326 pastith
                            json.put("folders", subfolders);
86 14ad7326 pastith
87 ac4d1481 pastith
                        List<FileHeaderDTO> fileHeaders = getService().getSharedFilesNotInSharedFolders(owner.getId());
88 f7abcd5a pastith
                        List<JSONObject> files = new ArrayList<JSONObject>();
89 f7abcd5a pastith
                        for (FileHeaderDTO f: fileHeaders) {
90 f7abcd5a pastith
                                JSONObject j = new JSONObject();
91 f7abcd5a pastith
                                    j.put("name", f.getName()).
92 3f2e14a0 pastith
                                            put("owner", f.getOwner().getUsername()).
93 f7abcd5a pastith
                                            put("deleted", f.isDeleted()).
94 f7abcd5a pastith
                                            put("version", f.getVersion()).
95 f7abcd5a pastith
                                            put("size", f.getFileSize()).
96 0e4865ee pastith
                                            put("content", f.getMimeType()).
97 d6ba3052 koutsoub
                                            put("path", f.getFolder().getPath()).
98 16b54aa8 koutsoub
                                            put("shared", f.getShared()).
99 16b54aa8 koutsoub
                                            put("versioned",f.isVersioned()).
100 f7abcd5a pastith
                                            put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
101 d0d4d0f5 Panagiotis Astithas
                                            put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
102 2b90ab8a pastith
                                        put("uri", getApiRoot() + f.getURI());
103 13571ef8 pastith
                                    JSONObject jf = new JSONObject();
104 865f1d02 pastith
                                    jf.put("uri", getApiRoot() + f.getFolder().getURI()).
105 13571ef8 pastith
                                                    put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
106 13571ef8 pastith
                                    j.put("folder", jf);
107 f7abcd5a pastith
                                files.add(j);
108 f7abcd5a pastith
                        }
109 14ad7326 pastith
                        json.put("files", files);
110 14ad7326 pastith
111 14ad7326 pastith
                    sendJson(req, resp, json.toString());
112 b722599f droutsis
                    // Workaround for IE's broken caching behavior.
113 b722599f droutsis
                            resp.setHeader("Expires", "-1");
114 14ad7326 pastith
                    } catch (ObjectNotFoundException e) {
115 14ad7326 pastith
                            logger.error("User not found", e);
116 14ad7326 pastith
                            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
117 14ad7326 pastith
                    } catch (RpcException e) {
118 14ad7326 pastith
                            logger.error("", e);
119 14ad7326 pastith
                            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
120 14ad7326 pastith
                        } catch (JSONException e) {
121 14ad7326 pastith
                                logger.error("", e);
122 14ad7326 pastith
                                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
123 14ad7326 pastith
                        } catch (InsufficientPermissionsException e) {
124 14ad7326 pastith
                                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
125 14ad7326 pastith
                        }
126 14ad7326 pastith
                else
127 14ad7326 pastith
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
128 14ad7326 pastith
        }
129 14ad7326 pastith
130 14ad7326 pastith
}