Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / rest / UserHandler.java @ 086c7250

History | View | Annotate | Download (6.6 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.server.rest;
20 14ad7326 pastith
21 50135fa5 pastith
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 af6aa461 Christos V. Stathis
import gr.ebs.gss.common.exceptions.InsufficientPermissionsException;
23 af6aa461 Christos V. Stathis
import gr.ebs.gss.common.exceptions.ObjectNotFoundException;
24 af6aa461 Christos V. Stathis
import gr.ebs.gss.common.exceptions.RpcException;
25 3ef7b691 Dimitris Routsis
import gr.ebs.gss.server.Login;
26 14ad7326 pastith
import gr.ebs.gss.server.domain.User;
27 366f3f31 Natasa Kapravelou
import gr.ebs.gss.server.domain.UserLogin;
28 af6aa461 Christos V. Stathis
import gr.ebs.gss.common.dto.StatsDTO;
29 3b6b7f25 Dimitris Routsis
import gr.ebs.gss.server.ejb.TransactionHelper;
30 14ad7326 pastith
31 14ad7326 pastith
import java.io.IOException;
32 366f3f31 Natasa Kapravelou
import java.util.Date;
33 366f3f31 Natasa Kapravelou
import java.util.List;
34 2f1a60e0 Dimitris Routsis
import java.util.concurrent.Callable;
35 14ad7326 pastith
36 3ef7b691 Dimitris Routsis
import javax.servlet.http.Cookie;
37 14ad7326 pastith
import javax.servlet.http.HttpServletRequest;
38 14ad7326 pastith
import javax.servlet.http.HttpServletResponse;
39 14ad7326 pastith
40 14ad7326 pastith
import org.apache.commons.logging.Log;
41 14ad7326 pastith
import org.apache.commons.logging.LogFactory;
42 14ad7326 pastith
import org.json.JSONException;
43 14ad7326 pastith
import org.json.JSONObject;
44 14ad7326 pastith
45 14ad7326 pastith
46 14ad7326 pastith
/**
47 14ad7326 pastith
 * A class that handles operations on the user's root namespace.
48 14ad7326 pastith
 *
49 14ad7326 pastith
 * @author past
50 14ad7326 pastith
 */
51 14ad7326 pastith
public class UserHandler extends RequestHandler {
52 3ef7b691 Dimitris Routsis
53 3ef7b691 Dimitris Routsis
        /**
54 3ef7b691 Dimitris Routsis
         * The reset WebDAV password parameter name.
55 3ef7b691 Dimitris Routsis
         */
56 3ef7b691 Dimitris Routsis
        protected static final String RESET_WEBDAV_PARAMETER = "resetWebDAV";
57 3ef7b691 Dimitris Routsis
58 14ad7326 pastith
        /**
59 14ad7326 pastith
         * The logger.
60 14ad7326 pastith
         */
61 14ad7326 pastith
        private static Log logger = LogFactory.getLog(UserHandler.class);
62 14ad7326 pastith
63 14ad7326 pastith
    /**
64 14ad7326 pastith
     * Serve the root namespace for the user.
65 14ad7326 pastith
     *
66 14ad7326 pastith
     * @param req The servlet request we are processing
67 14ad7326 pastith
     * @param resp The servlet response we are processing
68 14ad7326 pastith
     * @throws IOException if an input/output error occurs
69 14ad7326 pastith
         */
70 14ad7326 pastith
        void serveUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
71 14ad7326 pastith
            String parentUrl = getContextPath(req, false);
72 3ef7b691 Dimitris Routsis
73 14ad7326 pastith
            User user = getUser(req);
74 14ad7326 pastith
            User owner = getOwner(req);
75 14ad7326 pastith
            if (!owner.equals(user)) {
76 14ad7326 pastith
                    resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
77 14ad7326 pastith
                    return;
78 14ad7326 pastith
            }
79 3ef7b691 Dimitris Routsis
80 14ad7326 pastith
            JSONObject json = new JSONObject();
81 14ad7326 pastith
            try {
82 14ad7326 pastith
                    StatsDTO stats = getService().getUserStatistics(owner.getId());
83 14ad7326 pastith
                    JSONObject statistics = new JSONObject();
84 14ad7326 pastith
                    statistics.put("totalFiles", stats.getFileCount()).put("totalBytes", stats.getFileSize()).
85 14ad7326 pastith
                                    put("bytesRemaining", stats.getQuotaLeftSize());
86 14ad7326 pastith
                        json.put("name", owner.getName()).put("firstname", owner.getFirstname()).
87 14ad7326 pastith
                                        put("lastname", owner.getLastname()).put("username", owner.getUsername()).
88 14ad7326 pastith
                                        put("creationDate", owner.getAuditInfo().getCreationDate().getTime()).
89 14ad7326 pastith
                                        put("modificationDate", owner.getAuditInfo().getModificationDate().getTime()).
90 64b053d6 pastith
                                        put("email", owner.getEmail()).put("fileroot", parentUrl + PATH_FILES).
91 14ad7326 pastith
                                        put("groups", parentUrl + PATH_GROUPS).put("trash", parentUrl + PATH_TRASH).
92 14ad7326 pastith
                                        put("shared", parentUrl + PATH_SHARED).put("others", parentUrl + PATH_OTHERS).
93 ed1ac326 koutsoub
                                        put("quota", statistics).put("tags", parentUrl + PATH_TAGS);
94 6b5bd65e Christos V. Stathis
                        String announcement = getConfiguration().getString("announcement", "");
95 fcc0410c Christos V. Stathis
            if (announcement.length() > 0)
96 fcc0410c Christos V. Stathis
                announcement = "<p>" + announcement + "</p>";
97 6b5bd65e Christos V. Stathis
            String authgr = getConfiguration().getString("authgr", "auth.gr");
98 6b5bd65e Christos V. Stathis
            if (authgr.equals(user.getHomeOrganization()))
99 fcc0410c Christos V. Stathis
                announcement += "<p>" + getConfiguration().getString("authAnnouncement", "") + "</p>";
100 0e64bec2 Panagiotis Astithas
                        if (announcement != null && !announcement.isEmpty())
101 50135fa5 pastith
                                json.put("announcement", announcement);
102 769f9814 Natasa Kapravelou
                        List<UserLogin> userLogins = getService().getLastUserLogins(owner.getId());                        
103 366f3f31 Natasa Kapravelou
                        UserLogin currentLogin = userLogins.get(0);
104 366f3f31 Natasa Kapravelou
                        Date currentLoginDate = currentLogin.getLoginDate();
105 366f3f31 Natasa Kapravelou
                        UserLogin lastLogin = userLogins.get(1);
106 a3d193a5 Natasa Kapravelou
                        Date lastLoginDate = lastLogin.getLoginDate();                                                
107 5d0ffbbc Natasa Kapravelou
                        json.put("lastLogin", lastLoginDate.getTime())
108 5d0ffbbc Natasa Kapravelou
                                .put("currentLogin", currentLoginDate.getTime());                                
109 14ad7326 pastith
                } catch (JSONException e) {
110 14ad7326 pastith
                        logger.error("", e);
111 14ad7326 pastith
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
112 14ad7326 pastith
                        return;
113 14ad7326 pastith
                } catch (ObjectNotFoundException e) {
114 14ad7326 pastith
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
115 14ad7326 pastith
                        return;
116 14ad7326 pastith
                } catch (RpcException e) {
117 14ad7326 pastith
                        logger.error("", e);
118 14ad7326 pastith
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
119 14ad7326 pastith
                        return;
120 14ad7326 pastith
                }
121 14ad7326 pastith
122 14ad7326 pastith
            sendJson(req, resp, json.toString());
123 14ad7326 pastith
        }
124 14ad7326 pastith
125 b4348269 Dimitris Routsis
126 b4348269 Dimitris Routsis
        /**
127 b4348269 Dimitris Routsis
         * Handle POST requests in the users namespace.
128 b4348269 Dimitris Routsis
         *
129 b4348269 Dimitris Routsis
     * @param req The servlet request we are processing
130 b4348269 Dimitris Routsis
     * @param resp The servlet response we are processing
131 b4348269 Dimitris Routsis
     * @throws IOException if an input/output error occurs
132 b4348269 Dimitris Routsis
         */
133 b4348269 Dimitris Routsis
        void postUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
134 b4348269 Dimitris Routsis
                try {
135 2f1a60e0 Dimitris Routsis
                    final User user = getUser(req);
136 b4348269 Dimitris Routsis
                User owner = getOwner(req);
137 b4348269 Dimitris Routsis
                if (!owner.equals(user))
138 b4348269 Dimitris Routsis
                        throw new InsufficientPermissionsException("User " + user.getUsername()
139 b4348269 Dimitris Routsis
                                                + " does not have permission to modify "
140 b4348269 Dimitris Routsis
                                                + owner.getUsername());
141 b4348269 Dimitris Routsis
                boolean hasResetWebDAVParam = req.getParameterMap().containsKey(RESET_WEBDAV_PARAMETER);
142 b4348269 Dimitris Routsis
                if (hasResetWebDAVParam) {
143 2f1a60e0 Dimitris Routsis
                        String newPassword = new TransactionHelper<String>().tryExecute(new Callable<String>() {
144 2f1a60e0 Dimitris Routsis
                                        @Override
145 2f1a60e0 Dimitris Routsis
                                        public String call() throws Exception {
146 2f1a60e0 Dimitris Routsis
                                                return getService().resetWebDAVPassword(user.getId());
147 2f1a60e0 Dimitris Routsis
                                        }
148 2f1a60e0 Dimitris Routsis
                                });
149 2f1a60e0 Dimitris Routsis
150 b4348269 Dimitris Routsis
                            // Set the cookie again to send new value
151 b4348269 Dimitris Routsis
                            Cookie cookie = new Cookie(Login.WEBDAV_COOKIE, newPassword);
152 b4348269 Dimitris Routsis
                            cookie.setMaxAge(-1);
153 b4348269 Dimitris Routsis
                            String domain = req.getRemoteHost();
154 b4348269 Dimitris Routsis
                            String path = req.getContextPath();
155 b4348269 Dimitris Routsis
                            cookie.setDomain(domain);
156 b4348269 Dimitris Routsis
                            cookie.setPath(path);
157 b4348269 Dimitris Routsis
                        resp.addCookie(cookie);
158 b4348269 Dimitris Routsis
                }
159 b4348269 Dimitris Routsis
                    // Workaround for IE's broken caching behavior.
160 b4348269 Dimitris Routsis
                        resp.setHeader("Expires", "-1");
161 b4348269 Dimitris Routsis
                } catch (ObjectNotFoundException e) {
162 b4348269 Dimitris Routsis
                        resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
163 b4348269 Dimitris Routsis
                } catch (RpcException e) {
164 b4348269 Dimitris Routsis
                        logger.error("", e);
165 b4348269 Dimitris Routsis
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
166 b4348269 Dimitris Routsis
                } catch (InsufficientPermissionsException e) {
167 b4348269 Dimitris Routsis
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
168 2f1a60e0 Dimitris Routsis
                } catch (Exception e) {
169 2f1a60e0 Dimitris Routsis
                        logger.error("", e);
170 2f1a60e0 Dimitris Routsis
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
171 b4348269 Dimitris Routsis
                }
172 b4348269 Dimitris Routsis
        }
173 b4348269 Dimitris Routsis
174 14ad7326 pastith
}