James Moger
2013-06-24 5abe3350a38b3cdfc28ac2839860d28b88f2306a
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
b2fde8 44     protected final int forbiddenCode = HttpServletResponse.SC_FORBIDDEN;
JM 45     
46     protected final int notAllowedCode = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
47
48     protected final int failureCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
49     
93f0b1 50     protected final Logger logger;
JM 51
52     public JsonServlet() {
53         super();
54         logger = LoggerFactory.getLogger(getClass());
55     }
56
57     /**
58      * Processes an gson request.
59      * 
60      * @param request
61      * @param response
62      * @throws javax.servlet.ServletException
63      * @throws java.io.IOException
64      */
65     protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
66             throws ServletException, IOException;
67
68     @Override
69     protected void doPost(HttpServletRequest request, HttpServletResponse response)
70             throws ServletException, java.io.IOException {
71         processRequest(request, response);
72     }
73
74     @Override
75     protected void doGet(HttpServletRequest request, HttpServletResponse response)
76             throws ServletException, IOException {
77         processRequest(request, response);
78     }
79
80     protected <X> X deserialize(HttpServletRequest request, HttpServletResponse response,
81             Class<X> clazz) throws IOException {
31abc2 82         String json = readJson(request, response);
JM 83         if (StringUtils.isEmpty(json)) {
84             return null;
85         }
86
f146f2 87         X object = JsonUtils.fromJsonString(json.toString(), clazz);
31abc2 88         return object;
JM 89     }
90
91     protected <X> X deserialize(HttpServletRequest request, HttpServletResponse response, Type type)
92             throws IOException {
93         String json = readJson(request, response);
94         if (StringUtils.isEmpty(json)) {
95             return null;
96         }
97
f146f2 98         X object = JsonUtils.fromJsonString(json.toString(), type);
31abc2 99         return object;
JM 100     }
101
102     private String readJson(HttpServletRequest request, HttpServletResponse response)
103             throws IOException {
93f0b1 104         BufferedReader reader = request.getReader();
JM 105         StringBuilder json = new StringBuilder();
106         String line = null;
107         while ((line = reader.readLine()) != null) {
108             json.append(line);
109         }
110         reader.close();
111
112         if (json.length() == 0) {
113             logger.error(MessageFormat.format("Failed to receive json data from {0}",
114                     request.getRemoteAddr()));
115             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
116             return null;
117         }
31abc2 118         return json.toString();
93f0b1 119     }
JM 120
121     protected void serialize(HttpServletResponse response, Object o) throws IOException {
122         if (o != null) {
123             // Send JSON response
f146f2 124             String json = JsonUtils.toJsonString(o);
d7ec6e 125             response.setCharacterEncoding(Constants.ENCODING);
5e1f7d 126             response.setContentType("application/json");
93f0b1 127             response.getWriter().append(json);
JM 128         }
129     }
130 }