Commit after code review concerning handling of lastLogin in the admin interface...
[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.FileHeader;
24 import gr.ebs.gss.server.domain.FileBody;
25 import gr.ebs.gss.server.domain.User;
26
27 import java.io.IOException;
28 import java.net.URLDecoder;
29 import java.net.URLEncoder;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.json.JSONArray;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40
41 /**
42  * A class that handles operations on the 'search' namespace.
43  *
44  * @author past
45  */
46 public class SearchHandler extends RequestHandler {
47         /**
48          * The logger.
49          */
50         private static Log logger = LogFactory.getLog(SearchHandler.class);
51
52         /**
53      * Serve the 'search' namespace that contains results in queries to find files.
54      *
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
58          */
59         void serveSearchResults(HttpServletRequest req, HttpServletResponse resp) throws IOException {
60         String path = getInnerPath(req, PATH_SEARCH);
61
62                 if(path.indexOf("/") == 0)
63                         path = path.substring(1);
64                 if(path.lastIndexOf("/") == path.length()-1)
65                         path = path.substring(0,path.length()-1);
66                 if (!isValidResourceName(path)) {
67                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
68                 return;
69
70                 }
71                 if (path.equals(""))
72                         path = "/";
73         if (!path.equals("/"))
74                         try {
75                         User user = getUser(req);
76                         JSONArray json = new JSONArray();
77
78                                 List<FileHeader> fileHeaders = getService().searchFiles(user.getId(), URLDecoder.decode(path,"UTF-8"));
79                 for (FileHeader f: fileHeaders) {
80                         FileBody currentBody = f.getCurrentBody();
81                         JSONObject j = new JSONObject();
82                                 j.put("name", f.getName()).
83                                         put("owner", f.getOwner().getUsername()).
84                                         put("deleted", f.isDeleted()).
85                                         put("version", currentBody.getVersion()).
86                                         put("size", currentBody.getFileSize()).
87                                         put("path", f.getFolder().getPath()).
88                                                 put("content", currentBody.getMimeType()).
89                                                 put("shared", f.getShared()).
90                                         put("versioned",f.isVersioned()).
91                                         put("creationDate", f.getAuditInfo().getCreationDate().getTime()).
92                                         put("modificationDate", f.getAuditInfo().getModificationDate().getTime()).
93                                         put("uri", getApiRoot() + f.getURI());
94                                 JSONObject jf = new JSONObject();
95                                 jf.put("uri", getApiRoot() + f.getFolder().getURI()).
96                                                 put("name", URLEncoder.encode(f.getFolder().getName(),"UTF-8"));
97                                 j.put("folder", jf);
98                                 json.put(j);
99                 }
100                 sendJson(req, resp, json.toString());
101                 } catch (ObjectNotFoundException e) {
102                         logger.error("User not found or search query not specified", e);
103                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
104                         return;
105                 } catch (RpcException e) {
106                         logger.error("", e);
107                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
108                         return;
109                         } catch (JSONException e) {
110                                 logger.error("", e);
111                                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
112                         }
113                 else {
114                         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No search query found");
115                         return;
116         }
117         }
118
119 }