Implement the server-side part of user search.
authorpastith <devnull@localhost>
Mon, 30 Mar 2009 11:09:26 +0000 (11:09 +0000)
committerpastith <devnull@localhost>
Mon, 30 Mar 2009 11:09:26 +0000 (11:09 +0000)
gss/src/gr/ebs/gss/server/rest/RequestHandler.java
gss/src/gr/ebs/gss/server/rest/UserSearchHandler.java [new file with mode: 0644]

index 74324ad..f256179 100644 (file)
@@ -65,6 +65,11 @@ public class RequestHandler extends Webdav {
        protected static final String PATH_SEARCH = "/search";
 
        /**
+        * The path for the user search subsystem.
+        */
+       protected static final String PATH_USERS = "/users";
+
+       /**
         * The path for the resource manipulation subsystem.
         */
        protected static final String PATH_FILES = FileHeaderDTO.PATH_FILES;
@@ -184,6 +189,7 @@ public class RequestHandler extends Webdav {
                                        ", " + METHOD_DELETE);
                methodsAllowed.put(PATH_OTHERS, METHOD_GET);
                methodsAllowed.put(PATH_SEARCH, METHOD_GET);
+               methodsAllowed.put(PATH_USERS, METHOD_GET);
                methodsAllowed.put(PATH_SHARED, METHOD_GET);
                methodsAllowed.put(PATH_TAGS, METHOD_GET);
                methodsAllowed.put(PATH_TRASH, METHOD_GET + ", " + METHOD_DELETE);
@@ -256,6 +262,9 @@ public class RequestHandler extends Webdav {
                } else if (path.startsWith(PATH_SEARCH)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SEARCH));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+               } else if (path.startsWith(PATH_USERS)) {
+            resp.addHeader("Allow", methodsAllowed.get(PATH_USERS));
+                       resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                } else if (path.startsWith(PATH_SHARED)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SHARED));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@@ -300,6 +309,9 @@ public class RequestHandler extends Webdav {
                } else if (path.startsWith(PATH_SEARCH)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SEARCH));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+               } else if (path.startsWith(PATH_USERS)) {
+            resp.addHeader("Allow", methodsAllowed.get(PATH_USERS));
+                       resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                } else if (path.startsWith(PATH_SHARED)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SHARED));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@@ -348,6 +360,8 @@ public class RequestHandler extends Webdav {
                        new TrashHandler().serveTrash(req, resp);
                else if (path.startsWith(PATH_SEARCH))
                        new SearchHandler().serveSearchResults(req, resp);
+               else if (path.startsWith(PATH_USERS))
+                       new UserSearchHandler().serveResults(req, resp);
                else if (path.startsWith(PATH_GROUPS))
                        new GroupsHandler().serveGroups(req, resp);
                else if (path.startsWith(PATH_SHARED))
@@ -385,6 +399,9 @@ public class RequestHandler extends Webdav {
                } else if (path.startsWith(PATH_SEARCH)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SEARCH));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+               } else if (path.startsWith(PATH_USERS)) {
+            resp.addHeader("Allow", methodsAllowed.get(PATH_USERS));
+                       resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                } else if (path.startsWith(PATH_SHARED)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SHARED));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@@ -430,6 +447,9 @@ public class RequestHandler extends Webdav {
                } else if (path.startsWith(PATH_SEARCH)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SEARCH));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+               } else if (path.startsWith(PATH_USERS)) {
+            resp.addHeader("Allow", methodsAllowed.get(PATH_USERS));
+                       resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                } else if (path.startsWith(PATH_SHARED)) {
             resp.addHeader("Allow", methodsAllowed.get(PATH_SHARED));
                        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@@ -473,7 +493,7 @@ public class RequestHandler extends Webdav {
                        req.setAttribute(OWNER_ATTRIBUTE, o);
                        return path.substring(slash + 1);
                }
-               if (!path.startsWith(PATH_SEARCH))
+               if (!path.startsWith(PATH_SEARCH) && !path.startsWith(PATH_USERS))
                        throw new ObjectNotFoundException("User " + owner + " not found");
                return path;
        }
diff --git a/gss/src/gr/ebs/gss/server/rest/UserSearchHandler.java b/gss/src/gr/ebs/gss/server/rest/UserSearchHandler.java
new file mode 100644 (file)
index 0000000..ebc8bed
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2009 Electronic Business Systems Ltd.
+ *
+ * This file is part of GSS.
+ *
+ * GSS is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GSS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package gr.ebs.gss.server.rest;
+
+import gr.ebs.gss.client.exceptions.RpcException;
+import gr.ebs.gss.server.domain.dto.UserDTO;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+
+/**
+ * A class that handles operations on the 'user search' namespace.
+ *
+ * @author past
+ */
+public class UserSearchHandler extends RequestHandler {
+       /**
+        * The logger.
+        */
+       private static Log logger = LogFactory.getLog(UserSearchHandler.class);
+
+       /**
+     * Serve the 'user search' namespace that contains results in queries to find users.
+     *
+     * @param req The servlet request we are processing
+     * @param resp The servlet response we are processing
+     * @throws IOException if an input/output error occurs
+        */
+       void serveResults(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+       String contextPath = getContextPath(req, true);
+        String path = getInnerPath(req, PATH_USERS);
+               if (path.equals(""))
+                       path = "/";
+
+       if (!path.equals("/"))
+                       try {
+                       JSONArray json = new JSONArray();
+
+                               List<UserDTO> users = getService().getUsersByUserNameLike(path.substring(1));
+                       for (UserDTO u: users) {
+                                       // Build the proper parent URL
+                                       String pathInfo = req.getPathInfo();
+                                       String parentUrl = contextPath.replaceFirst(pathInfo, "");
+                               JSONObject j = new JSONObject();
+                               j.put("username", u.getUsername()).put("name", u.getName()).
+                                       put("uri", parentUrl + u.getUsername());
+                               json.put(j);
+                       }
+               sendJson(req, resp, json.toString());
+               } catch (RpcException e) {
+                       logger.error("", e);
+                       resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                       return;
+                       } catch (JSONException e) {
+                               logger.error("", e);
+                               resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                       }
+               else {
+                       resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");
+                       return;
+       }
+       }
+
+}