Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / server / rest / UserHandler.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 static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
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.StatsDTO;
26
27 import java.io.IOException;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36
37
38 /**
39  * A class that handles operations on the user's root namespace.
40  *
41  * @author past
42  */
43 public class UserHandler extends RequestHandler {
44         /**
45          * The logger.
46          */
47         private static Log logger = LogFactory.getLog(UserHandler.class);
48
49     /**
50      * Serve the root namespace for the user.
51      *
52      * @param req The servlet request we are processing
53      * @param resp The servlet response we are processing
54      * @throws IOException if an input/output error occurs
55          */
56         void serveUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
57         String parentUrl = getContextPath(req, false);
58         User user = getUser(req);
59         User owner = getOwner(req);
60         if (!owner.equals(user)) {
61                 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
62                 return;
63         }
64         JSONObject json = new JSONObject();
65         try {
66                 StatsDTO stats = getService().getUserStatistics(owner.getId());
67                 JSONObject statistics = new JSONObject();
68                 statistics.put("totalFiles", stats.getFileCount()).put("totalBytes", stats.getFileSize()).
69                                 put("bytesRemaining", stats.getQuotaLeftSize());
70                         json.put("name", owner.getName()).put("firstname", owner.getFirstname()).
71                                         put("lastname", owner.getLastname()).put("username", owner.getUsername()).
72                                         put("creationDate", owner.getAuditInfo().getCreationDate().getTime()).
73                                         put("modificationDate", owner.getAuditInfo().getModificationDate().getTime()).
74                                         put("email", owner.getEmail()).put("fileroot", parentUrl + PATH_FILES).
75                                         put("groups", parentUrl + PATH_GROUPS).put("trash", parentUrl + PATH_TRASH).
76                                         put("shared", parentUrl + PATH_SHARED).put("others", parentUrl + PATH_OTHERS).
77                                         put("quota", statistics).put("tags", parentUrl + PATH_TAGS);
78                         String announcement = getConfiguration().getString("announcement");
79                         if(announcement != null && !announcement.isEmpty())
80                                 json.put("announcement", announcement);
81                 } catch (JSONException e) {
82                         logger.error("", e);
83                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
84                         return;
85                 } catch (ObjectNotFoundException e) {
86                         resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
87                         return;
88                 } catch (RpcException e) {
89                         logger.error("", e);
90                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
91                         return;
92                 }
93
94         sendJson(req, resp, json.toString());
95         }
96
97 }