Add system-generated password for WebDAV. (Bug #705)
[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.Login;
25 import gr.ebs.gss.server.domain.User;
26 import gr.ebs.gss.server.domain.dto.StatsDTO;
27
28 import java.io.IOException;
29
30 import javax.servlet.http.Cookie;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38
39
40 /**
41  * A class that handles operations on the user's root namespace.
42  *
43  * @author past
44  */
45 public class UserHandler extends RequestHandler {
46
47         /**
48          * The reset WebDAV password parameter name.
49          */
50         protected static final String RESET_WEBDAV_PARAMETER = "resetWebDAV";
51
52         /**
53          * The logger.
54          */
55         private static Log logger = LogFactory.getLog(UserHandler.class);
56
57     /**
58      * Serve the root namespace for the user.
59      *
60      * @param req The servlet request we are processing
61      * @param resp The servlet response we are processing
62      * @throws IOException if an input/output error occurs
63          */
64         void serveUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
65         String parentUrl = getContextPath(req, false);
66
67         User user = getUser(req);
68         User owner = getOwner(req);
69         if (!owner.equals(user)) {
70                 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
71                 return;
72         }
73
74         JSONObject json = new JSONObject();
75         try {
76                 boolean hasResetWebDAVParam = req.getParameterMap().containsKey(RESET_WEBDAV_PARAMETER);
77                 if (hasResetWebDAVParam) {
78                         String newPassword = getService().resetWebDAVPassword(user.getId());
79                         // Set the cookie again to send new value
80                         Cookie cookie = new Cookie(Login.WEBDAV_COOKIE, newPassword);
81                         cookie.setMaxAge(-1);
82                         String domain = req.getRemoteHost();
83                         String path = req.getContextPath();
84                         cookie.setDomain(domain);
85                         cookie.setPath(path);
86                     resp.addCookie(cookie);
87                     // Workaround for IE's broken caching behavior.
88                         resp.setHeader("Expires", "-1");
89                 }
90
91                 StatsDTO stats = getService().getUserStatistics(owner.getId());
92                 JSONObject statistics = new JSONObject();
93                 statistics.put("totalFiles", stats.getFileCount()).put("totalBytes", stats.getFileSize()).
94                                 put("bytesRemaining", stats.getQuotaLeftSize());
95                         json.put("name", owner.getName()).put("firstname", owner.getFirstname()).
96                                         put("lastname", owner.getLastname()).put("username", owner.getUsername()).
97                                         put("creationDate", owner.getAuditInfo().getCreationDate().getTime()).
98                                         put("modificationDate", owner.getAuditInfo().getModificationDate().getTime()).
99                                         put("email", owner.getEmail()).put("fileroot", parentUrl + PATH_FILES).
100                                         put("groups", parentUrl + PATH_GROUPS).put("trash", parentUrl + PATH_TRASH).
101                                         put("shared", parentUrl + PATH_SHARED).put("others", parentUrl + PATH_OTHERS).
102                                         put("quota", statistics).put("tags", parentUrl + PATH_TAGS);
103                         String announcement = getConfiguration().getString("announcement");
104                         if(announcement != null && !announcement.isEmpty())
105                                 json.put("announcement", announcement);
106                 } catch (JSONException e) {
107                         logger.error("", e);
108                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
109                         return;
110                 } catch (ObjectNotFoundException e) {
111                         resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
112                         return;
113                 } catch (RpcException e) {
114                         logger.error("", e);
115                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
116                         return;
117                 }
118
119         sendJson(req, resp, json.toString());
120         }
121
122 }