Throw any exceptions thrown unwrapped. This way, the caller knows what it's dealing...
[pithos] / src / gr / ebs / gss / server / rest / TagsHandler.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
25 import java.io.IOException;
26 import java.util.Set;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.json.JSONArray;
34
35
36 /**
37  * A class that handles operations on the 'tags' namespace.
38  *
39  * @author past
40  */
41 public class TagsHandler extends RequestHandler {
42         /**
43          * The logger.
44          */
45         private static Log logger = LogFactory.getLog(TagsHandler.class);
46
47         /**
48      * Serve the tags defined by the user.
49      *
50      * @param req The servlet request we are processing
51      * @param resp The servlet response we are processing
52      * @throws IOException if an input/output error occurs
53          */
54         void serveTags(HttpServletRequest req, HttpServletResponse resp) throws IOException {
55         String path = getInnerPath(req, PATH_TAGS);
56                 if (path.equals(""))
57                         path = "/";
58
59         if (path.equals("/")) {
60                 Set<String> tags;
61                 try {
62                 User user = getUser(req);
63                 User owner = getOwner(req);
64                 if (!owner.equals(user)) {
65                         resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
66                         return;
67                 }
68                         tags = getService().getUserTags(user.getId());
69                 } catch (ObjectNotFoundException e) {
70                         logger.error("User not found", e);
71                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
72                         return;
73                 } catch (RpcException e) {
74                         logger.error("", e);
75                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
76                         return;
77                 }
78                 JSONArray json = new JSONArray();
79                 for (String tag: tags)
80                         json.put(tag);
81
82                 sendJson(req, resp, json.toString());
83         } else {
84                         resp.sendError(HttpServletResponse.SC_NOT_FOUND);
85                         return;
86         }
87         }
88
89
90 }