James Moger
2011-10-02 f762b160efd5cafd919a6fd7f9587f578eceb454
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;
31abc2 20 import java.lang.reflect.Type;
93f0b1 21 import java.text.MessageFormat;
JM 22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
f146f2 31 import com.gitblit.utils.JsonUtils;
31abc2 32 import com.gitblit.utils.StringUtils;
93f0b1 33
JM 34 /**
35  * Servlet class for interpreting json requests.
36  * 
37  * @author James Moger
38  * 
39  */
40 public abstract class JsonServlet extends HttpServlet {
41
42     private static final long serialVersionUID = 1L;
43
44     protected final Logger logger;
45
46     public JsonServlet() {
47         super();
48         logger = LoggerFactory.getLogger(getClass());
49     }
50
51     /**
52      * Processes an gson request.
53      * 
54      * @param request
55      * @param response
56      * @throws javax.servlet.ServletException
57      * @throws java.io.IOException
58      */
59     protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
60             throws ServletException, IOException;
61
62     @Override
63     protected void doPost(HttpServletRequest request, HttpServletResponse response)
64             throws ServletException, java.io.IOException {
65         processRequest(request, response);
66     }
67
68     @Override
69     protected void doGet(HttpServletRequest request, HttpServletResponse response)
70             throws ServletException, IOException {
71         processRequest(request, response);
72     }
73
74     protected <X> X deserialize(HttpServletRequest request, HttpServletResponse response,
75             Class<X> clazz) throws IOException {
31abc2 76         String json = readJson(request, response);
JM 77         if (StringUtils.isEmpty(json)) {
78             return null;
79         }
80
f146f2 81         X object = JsonUtils.fromJsonString(json.toString(), clazz);
31abc2 82         return object;
JM 83     }
84
85     protected <X> X deserialize(HttpServletRequest request, HttpServletResponse response, Type type)
86             throws IOException {
87         String json = readJson(request, response);
88         if (StringUtils.isEmpty(json)) {
89             return null;
90         }
91
f146f2 92         X object = JsonUtils.fromJsonString(json.toString(), type);
31abc2 93         return object;
JM 94     }
95
96     private String readJson(HttpServletRequest request, HttpServletResponse response)
97             throws IOException {
93f0b1 98         BufferedReader reader = request.getReader();
JM 99         StringBuilder json = new StringBuilder();
100         String line = null;
101         while ((line = reader.readLine()) != null) {
102             json.append(line);
103         }
104         reader.close();
105
106         if (json.length() == 0) {
107             logger.error(MessageFormat.format("Failed to receive json data from {0}",
108                     request.getRemoteAddr()));
109             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
110             return null;
111         }
31abc2 112         return json.toString();
93f0b1 113     }
JM 114
115     protected void serialize(HttpServletResponse response, Object o) throws IOException {
116         if (o != null) {
117             // Send JSON response
f146f2 118             String json = JsonUtils.toJsonString(o);
93f0b1 119             response.getWriter().append(json);
JM 120         }
121     }
122 }