Throw any exceptions thrown unwrapped. This way, the caller knows what it's dealing...
[pithos] / src / gr / ebs / gss / server / rest / UserSearchHandler.java
1 /*
2  * Copyright 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 gr.ebs.gss.client.exceptions.RpcException;
22 import gr.ebs.gss.server.domain.dto.UserDTO;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.json.JSONArray;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35
36
37 /**
38  * A class that handles operations on the 'user search' namespace.
39  *
40  * @author past
41  */
42 public class UserSearchHandler extends RequestHandler {
43         /**
44          * The logger.
45          */
46         private static Log logger = LogFactory.getLog(UserSearchHandler.class);
47
48         /**
49          * A flag that will force all queries to end in a @ character, mitigating privacy
50          * concerns about user accounts.
51          */
52         private static final boolean mustEndWithAt = true;
53
54         /**
55      * Serve the 'user search' namespace that contains results in queries to find users.
56      *
57      * @param req The servlet request we are processing
58      * @param resp The servlet response we are processing
59      * @throws IOException if an input/output error occurs
60          */
61         void serveResults(HttpServletRequest req, HttpServletResponse resp) throws IOException {
62         String contextPath = getContextPath(req, true);
63         String path = getInnerPath(req, PATH_USERS);
64         // Chop any trailing slash.
65         if (path.endsWith("/"))
66                 path = path.substring(0, path.length()-1);
67                 if (path.equals(""))
68                         path = "/";
69
70         if (!path.equals("/"))
71                         try {
72                         JSONArray json = new JSONArray();
73
74                         if (mustEndWithAt && !path.endsWith("@"))
75                                 path += '@';
76                                 List<UserDTO> users = getService().getUsersByUserNameLike(path.substring(1));
77                         for (UserDTO u: users) {
78                                         // Build the proper parent URL
79                                         String pathInfo = req.getPathInfo();
80                                         String parentUrl = contextPath.replaceFirst(pathInfo, "");
81                                 JSONObject j = new JSONObject();
82                                 j.put("username", u.getUsername()).put("name", u.getName()).
83                                         put("uri", parentUrl + u.getUsername());
84                                 json.put(j);
85                         }
86                 sendJson(req, resp, json.toString());
87                 // Workaround for IE's broken caching behavior.
88                         resp.setHeader("Expires", "-1");
89                 } catch (RpcException e) {
90                         logger.error("", e);
91                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
92                         return;
93                         } catch (JSONException e) {
94                                 logger.error("", e);
95                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
96                         }
97                 else {
98                         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");
99                         return;
100         }
101         }
102
103 }