2 * Copyright 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 gr.ebs.gss.client.exceptions.RpcException;
22 import gr.ebs.gss.server.domain.dto.UserDTO;
24 import java.io.IOException;
25 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
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;
38 * A class that handles operations on the 'user search' namespace.
42 public class UserSearchHandler extends RequestHandler {
46 private static Log logger = LogFactory.getLog(UserSearchHandler.class);
49 * A flag that will force all queries to end in a @ character, mitigating privacy
50 * concerns about user accounts.
52 private static final boolean mustEndWithAt = true;
55 * Serve the 'user search' namespace that contains results in queries to find users.
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
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);
70 if (!path.equals("/"))
72 JSONArray json = new JSONArray();
74 if (mustEndWithAt && !path.endsWith("@"))
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());
86 sendJson(req, resp, json.toString());
87 } catch (RpcException e) {
89 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
91 } catch (JSONException e) {
93 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
96 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");