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 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;
27 import java.io.IOException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.json.JSONException;
35 import org.json.JSONObject;
39 * A class that handles operations on the user's root namespace.
43 public class UserHandler extends RequestHandler {
47 private static Log logger = LogFactory.getLog(UserHandler.class);
50 * Serve the root namespace for the user.
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
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);
64 JSONObject json = new JSONObject();
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) {
83 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
85 } catch (ObjectNotFoundException e) {
86 resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
88 } catch (RpcException e) {
90 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
94 sendJson(req, resp, json.toString());