Merge with f948e955504b0f4f31c1fe813f97f0297cd64cdb
[pithos] / src / gr / ebs / gss / server / rest / SearchHandler.java
1 /*
2  * Copyright 2008, 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.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;
25
26 import java.io.IOException;
27 import java.net.URLEncoder;
28 import java.util.List;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.json.JSONArray;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38
39
40 /**
41  * A class that handles operations on the 'search' namespace.
42  *
43  * @author past
44  */
45 public class SearchHandler extends RequestHandler {
46         /**
47          * The logger.
48          */
49         private static Log logger = LogFactory.getLog(SearchHandler.class);
50
51         /**
52      * Serve the 'search' namespace that contains results in queries to find files.
53      *
54      * @param req The servlet request we are processing
55      * @param resp The servlet response we are processing
56      * @throws IOException if an input/output error occurs
57          */
58         void serveSearchResults(HttpServletRequest req, HttpServletResponse resp) throws IOException {
59         String path = getInnerPath(req, PATH_SEARCH);
60                 if (path.equals(""))
61                         path = "/";
62
63         if (!path.equals("/"))
64                         try {
65                         User user = getUser(req);
66                         JSONArray json = new JSONArray();
67
68                                 List<FileHeaderDTO> fileHeaders = getService().searchFiles(user.getId(), path.substring(1));
69                 for (FileHeaderDTO f: fileHeaders) {
70                         JSONObject j = new JSONObject();
71                                 j.put("name", f.getName()).
72                                         put("owner", f.getOwner().getUsername()).
73                                         put("deleted", f.isDeleted()).
74                                         put("version", f.getVersion()).
75                                         put("size", f.getFileSize()).
76                                         put("path", f.getFolder().getPath()).
77                                         put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
78                                         put("uri", getApiRoot() + f.getURI());
79                                 JSONObject jf = new JSONObject();
80                                 jf.put("uri", getApiRoot() + f.getFolder().getURI()).
81                                                 put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
82                                 j.put("folder", jf);
83                                 json.put(j);
84                 }
85                 sendJson(req, resp, json.toString());
86                 } catch (ObjectNotFoundException e) {
87                         logger.error("User not found or search query not specified", e);
88                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
89                         return;
90                 } catch (RpcException e) {
91                         logger.error("", e);
92                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
93                         return;
94                         } catch (JSONException e) {
95                                 logger.error("", e);
96                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
97                         }
98                 else {
99                         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");
100                         return;
101         }
102         }
103
104 }