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