Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.7 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.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;
27

    
28
import java.io.IOException;
29
import java.net.URLEncoder;
30
import java.util.ArrayList;
31
import java.util.List;
32

    
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35

    
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38
import org.json.JSONException;
39
import org.json.JSONObject;
40

    
41

    
42
/**
43
 * A class that handles operations on the 'shared' namespace.
44
 *
45
 * @author past
46
 */
47
public class SharedHandler extends RequestHandler {
48
        /**
49
         * The logger.
50
         */
51
        private static Log logger = LogFactory.getLog(SharedHandler.class);
52

    
53
        /**
54
     * Serve the 'shared' 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 serveShared(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61
        String path = getInnerPath(req, PATH_SHARED);
62
                if (path.equals(""))
63
                        path = "/";
64

    
65
            if (path.equals("/"))
66
                        try {
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();
74

    
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());
83
                                    subfolders.add(j);
84
                        }
85
                            json.put("folders", subfolders);
86

    
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"));
106
                                    j.put("folder", jf);
107
                                files.add(j);
108
                        }
109
                        json.put("files", files);
110

    
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) {
118
                            logger.error("", e);
119
                            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
120
                        } catch (JSONException e) {
121
                                logger.error("", e);
122
                                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
123
                        } catch (InsufficientPermissionsException e) {
124
                                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
125
                        }
126
                else
127
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
128
        }
129

    
130
}