Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / rest / UserHandler.java @ 1206:292dec4eae08

History | View | Annotate | Download (6.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 org.gss_project.gss.server.rest;
20

    
21
import static org.gss_project.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22
import org.gss_project.gss.common.exceptions.InsufficientPermissionsException;
23
import org.gss_project.gss.common.exceptions.ObjectNotFoundException;
24
import org.gss_project.gss.common.exceptions.RpcException;
25
import org.gss_project.gss.server.Login;
26
import org.gss_project.gss.server.domain.User;
27
import org.gss_project.gss.server.domain.UserLogin;
28
import org.gss_project.gss.common.dto.StatsDTO;
29
import org.gss_project.gss.server.ejb.TransactionHelper;
30

    
31
import java.io.IOException;
32
import java.util.Date;
33
import java.util.List;
34
import java.util.concurrent.Callable;
35

    
36
import javax.servlet.http.Cookie;
37
import javax.servlet.http.HttpServletRequest;
38
import javax.servlet.http.HttpServletResponse;
39

    
40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
42
import org.json.JSONException;
43
import org.json.JSONObject;
44

    
45

    
46
/**
47
 * A class that handles operations on the user's root namespace.
48
 *
49
 * @author past
50
 */
51
public class UserHandler extends RequestHandler {
52

    
53
        /**
54
         * The reset WebDAV password parameter name.
55
         */
56
        protected static final String RESET_WEBDAV_PARAMETER = "resetWebDAV";
57

    
58
        /**
59
         * The logger.
60
         */
61
        private static Log logger = LogFactory.getLog(UserHandler.class);
62

    
63
    /**
64
     * Serve the root namespace for the user.
65
     *
66
     * @param req The servlet request we are processing
67
     * @param resp The servlet response we are processing
68
     * @throws IOException if an input/output error occurs
69
         */
70
        void serveUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
71
            String parentUrl = getContextPath(req, false);
72

    
73
            User user = getUser(req);
74
            User owner = getOwner(req);
75
            if (!owner.equals(user)) {
76
                    resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
77
                    return;
78
            }
79

    
80
            JSONObject json = new JSONObject();
81
            try {
82
                    StatsDTO stats = getService().getUserStatistics(owner.getId());
83
                    JSONObject statistics = new JSONObject();
84
                    statistics.put("totalFiles", stats.getFileCount()).put("totalBytes", stats.getFileSize()).
85
                                    put("bytesRemaining", stats.getQuotaLeftSize());
86
                        json.put("name", owner.getName()).put("firstname", owner.getFirstname()).
87
                                        put("lastname", owner.getLastname()).put("username", owner.getUsername()).
88
                                        put("creationDate", owner.getAuditInfo().getCreationDate().getTime()).
89
                                        put("modificationDate", owner.getAuditInfo().getModificationDate().getTime()).
90
                                        put("email", owner.getEmail()).put("fileroot", parentUrl + PATH_FILES).
91
                                        put("groups", parentUrl + PATH_GROUPS).put("trash", parentUrl + PATH_TRASH).
92
                                        put("shared", parentUrl + PATH_SHARED).put("others", parentUrl + PATH_OTHERS).
93
                                        put("quota", statistics).put("tags", parentUrl + PATH_TAGS);
94
                        String announcement = getConfiguration().getString("announcement", "");
95
            if (announcement.length() > 0)
96
                announcement = "<p>" + announcement + "</p>";
97
            String authgr = getConfiguration().getString("authgr", "auth.gr");
98
            if (authgr.equals(user.getHomeOrganization()))
99
                announcement += "<p>" + getConfiguration().getString("authAnnouncement", "") + "</p>";
100
                        if (announcement != null && !announcement.isEmpty())
101
                                json.put("announcement", announcement);
102
                        List<UserLogin> userLogins = getService().getLastUserLogins(owner.getId());                        
103
                        UserLogin currentLogin = userLogins.get(0);
104
                        Date currentLoginDate = currentLogin.getLoginDate();
105
                        UserLogin lastLogin = userLogins.get(1);
106
                        Date lastLoginDate = lastLogin.getLoginDate();                                                
107
                        json.put("lastLogin", lastLoginDate.getTime())
108
                                .put("currentLogin", currentLoginDate.getTime());                                
109
                } catch (JSONException e) {
110
                        logger.error("", e);
111
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
112
                        return;
113
                } catch (ObjectNotFoundException e) {
114
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
115
                        return;
116
                } catch (RpcException e) {
117
                        logger.error("", e);
118
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
119
                        return;
120
                }
121

    
122
            sendJson(req, resp, json.toString());
123
        }
124

    
125

    
126
        /**
127
         * Handle POST requests in the users namespace.
128
         *
129
     * @param req The servlet request we are processing
130
     * @param resp The servlet response we are processing
131
     * @throws IOException if an input/output error occurs
132
         */
133
        void postUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
134
                try {
135
                    final User user = getUser(req);
136
                User owner = getOwner(req);
137
                if (!owner.equals(user))
138
                        throw new InsufficientPermissionsException("User " + user.getUsername()
139
                                                + " does not have permission to modify "
140
                                                + owner.getUsername());
141
                boolean hasResetWebDAVParam = req.getParameterMap().containsKey(RESET_WEBDAV_PARAMETER);
142
                if (hasResetWebDAVParam) {
143
                        String newPassword = new TransactionHelper<String>().tryExecute(new Callable<String>() {
144
                                        @Override
145
                                        public String call() throws Exception {
146
                                                return getService().resetWebDAVPassword(user.getId());
147
                                        }
148
                                });
149

    
150
                            // Set the cookie again to send new value
151
                            Cookie cookie = new Cookie(Login.WEBDAV_COOKIE, newPassword);
152
                            cookie.setMaxAge(-1);
153
                            String domain = req.getRemoteHost();
154
                            String path = req.getContextPath();
155
                            cookie.setDomain(domain);
156
                            cookie.setPath(path);
157
                        resp.addCookie(cookie);
158
                }
159
                    // Workaround for IE's broken caching behavior.
160
                        resp.setHeader("Expires", "-1");
161
                } catch (ObjectNotFoundException e) {
162
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
163
                } catch (RpcException e) {
164
                        logger.error("", e);
165
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
166
                } catch (InsufficientPermissionsException e) {
167
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
168
                } catch (Exception e) {
169
                        logger.error("", e);
170
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
171
                }
172
        }
173

    
174
}