2 * Copyright 2008, 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.ObjectNotFoundException;
22 import gr.ebs.gss.client.exceptions.RpcException;
23 import gr.ebs.gss.server.domain.User;
24 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
26 import java.io.IOException;
27 import java.net.URLDecoder;
28 import java.net.URLEncoder;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.json.JSONArray;
37 import org.json.JSONException;
38 import org.json.JSONObject;
42 * A class that handles operations on the 'search' namespace.
46 public class SearchHandler extends RequestHandler {
50 private static Log logger = LogFactory.getLog(SearchHandler.class);
53 * Serve the 'search' namespace that contains results in queries to find files.
55 * @param req The servlet request we are processing
56 * @param resp The servlet response we are processing
57 * @throws IOException if an input/output error occurs
59 void serveSearchResults(HttpServletRequest req, HttpServletResponse resp) throws IOException {
60 String path = getInnerPath(req, PATH_SEARCH);
62 if(path.indexOf("/") == 0 && path.lastIndexOf("/") == path.length()-1){
63 path = path.substring(1);
64 path = path.substring(0,path.length()-1);
66 if (!isValidResourceName(path)) {
67 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
73 if (!path.equals("/"))
75 User user = getUser(req);
76 JSONArray json = new JSONArray();
78 List<FileHeaderDTO> fileHeaders = getService().searchFiles(user.getId(), URLDecoder.decode(path,"UTF-8"));
79 for (FileHeaderDTO f: fileHeaders) {
80 JSONObject j = new JSONObject();
81 j.put("name", f.getName()).
82 put("owner", f.getOwner().getUsername()).
83 put("deleted", f.isDeleted()).
84 put("version", f.getVersion()).
85 put("size", f.getFileSize()).
86 put("path", f.getFolder().getPath()).
87 put("content", f.getMimeType()).
88 put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
89 put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
90 put("uri", getApiRoot() + f.getURI());
91 JSONObject jf = new JSONObject();
92 jf.put("uri", getApiRoot() + f.getFolder().getURI()).
93 put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
97 sendJson(req, resp, json.toString());
98 } catch (ObjectNotFoundException e) {
99 logger.error("User not found or search query not specified", e);
100 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
102 } catch (RpcException e) {
104 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
106 } catch (JSONException e) {
108 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
111 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");