James Moger
2011-10-01 93f0b1a11d5a7f7c44cfcb8ff5300bb68b8b8188
commit | author | age
93f0b1 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.text.MessageFormat;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
33 /**
34  * Servlet class for interpreting json requests.
35  * 
36  * @author James Moger
37  * 
38  */
39 public abstract class JsonServlet extends HttpServlet {
40
41     private static final long serialVersionUID = 1L;
42
43     protected final Logger logger;
44
45     public JsonServlet() {
46         super();
47         logger = LoggerFactory.getLogger(getClass());
48     }
49
50     /**
51      * Processes an gson request.
52      * 
53      * @param request
54      * @param response
55      * @throws javax.servlet.ServletException
56      * @throws java.io.IOException
57      */
58     protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
59             throws ServletException, IOException;
60
61     @Override
62     protected void doPost(HttpServletRequest request, HttpServletResponse response)
63             throws ServletException, java.io.IOException {
64         processRequest(request, response);
65     }
66
67     @Override
68     protected void doGet(HttpServletRequest request, HttpServletResponse response)
69             throws ServletException, IOException {
70         processRequest(request, response);
71     }
72
73     protected <X> X deserialize(HttpServletRequest request, HttpServletResponse response,
74             Class<X> clazz) throws IOException {
75         BufferedReader reader = request.getReader();
76         StringBuilder json = new StringBuilder();
77         String line = null;
78         while ((line = reader.readLine()) != null) {
79             json.append(line);
80         }
81         reader.close();
82
83         if (json.length() == 0) {
84             logger.error(MessageFormat.format("Failed to receive json data from {0}",
85                     request.getRemoteAddr()));
86             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
87             return null;
88         }
89
90         Gson gson = new Gson();
91         X object = gson.fromJson(json.toString(), clazz);
92         return object;
93     }
94
95     protected void serialize(HttpServletResponse response, Object o) throws IOException {
96         if (o != null) {
97             // Send JSON response
98             Gson gson = new GsonBuilder().setPrettyPrinting().create();
99             String json = gson.toJson(o);
100             response.getWriter().append(json);
101         }
102     }
103 }