1616package org .jitsi .jigasi .rest ;
1717
1818import net .java .sip .communicator .util .osgi .*;
19- import org .eclipse .jetty .server .*;
20- import org .eclipse .jetty .server .handler .*;
2119import org .jitsi .utils .version .*;
2220import org .jivesoftware .smack .packet .*;
2321import org .json .simple .*;
2826import java .io .*;
2927
3028/**
31- * Implements an abstract Jetty {@code Handler} which provides content in JSON
32- * format.
29+ * Implements an abstract Jetty servlet which provides content in JSON format.
3330 *
3431 * @author Lyubomir Marinov
3532 * @author Pawel Domas
3633 */
3734public abstract class AbstractJSONHandler
38- extends AbstractHandler
35+ extends HttpServlet
3936{
40- /**
41- * The default suffix/extension of the HTTP resources which provide access
42- * to JSON representations.
43- */
44- private static final String DEFAULT_JSON_TARGET = null ;
45-
4637 /**
4738 * The HTTP GET method.
4839 */
@@ -105,12 +96,6 @@ else if (StanzaError.Condition.service_unavailable.equals(
10596 */
10697 protected final BundleContext bundleContext ;
10798
108- /**
109- * The suffix/extension of the HTTP resources which provide access to JSON
110- * representations.
111- */
112- private final String jsonTarget ;
113-
11499 /**
115100 * Initializes a new {@code AbstractJSONHandler} instance within a specific
116101 * {@code BundleContext}.
@@ -121,35 +106,19 @@ else if (StanzaError.Condition.service_unavailable.equals(
121106 protected AbstractJSONHandler (BundleContext bundleContext )
122107 {
123108 this .bundleContext = bundleContext ;
124-
125- // jsonTarget
126- String jsonTarget = DEFAULT_JSON_TARGET ;
127-
128- if (jsonTarget != null && !jsonTarget .startsWith ("." ))
129- jsonTarget = "." + jsonTarget ;
130- this .jsonTarget = jsonTarget ;
131109 }
132110
133111 /**
134112 * Begins an {@link HttpServletResponse} the handling of which appears to
135113 * have chances of success.
136- *
137- * @param target the target of the request
138- * @param baseRequest the original unwrapped {@link Request} object
139- * @param request the request either as the {@code Request} object or a
140- * wrapper of that request
141- * @param response the response either as the {@code Response} object or a
142- * wrapper of that response
143114 */
144115 protected void beginResponse (
145116 String target ,
146- Request baseRequest ,
147117 HttpServletRequest request ,
148118 HttpServletResponse response )
149119 {
150120 beginResponse (
151121 target ,
152- baseRequest ,
153122 request ,
154123 response ,
155124 RESTUtil .JSON_CONTENT_TYPE_WITH_CHARSET );
@@ -158,19 +127,9 @@ protected void beginResponse(
158127 /**
159128 * Begins an {@link HttpServletResponse} the handling of which appears to
160129 * have chances of success.
161- *
162- * @param target the target of the request
163- * @param baseRequest the original unwrapped {@link Request} object
164- * @param request the request either as the {@code Request} object or a
165- * wrapper of that request
166- * @param response the response either as the {@code Response} object or a
167- * wrapper of that response
168- * @param contentType the MIME type of the content to be set on
169- * {@code response}
170130 */
171131 protected void beginResponse (
172132 String target ,
173- Request baseRequest ,
174133 HttpServletRequest request ,
175134 HttpServletResponse response ,
176135 String contentType )
@@ -184,17 +143,8 @@ protected void beginResponse(
184143 * Gets a JSON representation of the health (status) of the associated
185144 * server/service. The default implementation does nothing because it serves
186145 * as a placeholder for extenders.
187- *
188- * @param baseRequest the original unwrapped {@link Request} object
189- * @param request the request either as the {@code Request} object or a
190- * wrapper of that request
191- * @param response the response either as the {@code Response} object or a
192- * wrapper of that response
193- * @throws IOException
194- * @throws ServletException
195146 */
196147 protected void doGetHealthJSON (
197- Request baseRequest ,
198148 HttpServletRequest request ,
199149 HttpServletResponse response )
200150 throws IOException ,
@@ -205,23 +155,14 @@ protected void doGetHealthJSON(
205155 /**
206156 * Gets a JSON representation of the {@code Version} of the associated
207157 * server/service.
208- *
209- * @param baseRequest the original unwrapped {@link Request} object
210- * @param request the request either as the {@code Request} object or a
211- * wrapper of that request
212- * @param response the response either as the {@code Response} object or a
213- * wrapper of that response
214- * @throws IOException
215- * @throws ServletException
216158 */
217159 protected void doGetVersionJSON (
218- Request baseRequest ,
219160 HttpServletRequest request ,
220161 HttpServletResponse response )
221162 throws IOException ,
222163 ServletException
223164 {
224- beginResponse (/*target*/ null , baseRequest , request , response );
165+ beginResponse (/*target*/ null , request , response );
225166
226167 BundleContext bundleContext = getBundleContext ();
227168 int status = HttpServletResponse .SC_SERVICE_UNAVAILABLE ;
@@ -252,32 +193,6 @@ protected void doGetVersionJSON(
252193
253194 if (response .getStatus () != status )
254195 response .setStatus (status );
255-
256- endResponse (/*target*/ null , baseRequest , request , response );
257- }
258-
259- /**
260- * Ends an {@link HttpServletResponse}.
261- *
262- * @param target the target of the request
263- * @param baseRequest the original unwrapped {@link Request} object
264- * @param request the request either as the {@code Request} object or a
265- * wrapper of that request
266- * @param response the response either as the {@code Response} object or a
267- * wrapper of that response
268- */
269- protected void endResponse (
270- String target ,
271- Request baseRequest ,
272- HttpServletRequest request ,
273- HttpServletResponse response )
274- {
275- if (!baseRequest .isHandled ())
276- {
277- if (response .getStatus () == 0 )
278- response .setStatus (HttpServletResponse .SC_NOT_FOUND );
279- baseRequest .setHandled (true );
280- }
281196 }
282197
283198 /**
@@ -315,43 +230,28 @@ public <T> T getService(Class<T> serviceClass)
315230 * {@inheritDoc}
316231 */
317232 @ Override
318- public void handle (
319- String target ,
320- Request baseRequest ,
233+ protected void service (
321234 HttpServletRequest request ,
322235 HttpServletResponse response )
323- throws IOException ,
324- ServletException
236+ throws IOException , ServletException
325237 {
326- if (target != null )
327- {
328- // The target ends with ".json".
329- int jsonTargetLength
330- = (jsonTarget == null ) ? 0 : jsonTarget .length ();
238+ String target = request .getRequestURI ();
331239
332- if (jsonTargetLength == 0 || target .endsWith (jsonTarget ))
240+ if (!handleJSON (target , request , response ))
241+ {
242+ if (response .getStatus () == 0
243+ || response .getStatus () == HttpServletResponse .SC_OK )
333244 {
334- target
335- = target .substring (0 , target .length () - jsonTargetLength );
336-
337- handleJSON (target , baseRequest , request , response );
245+ response .setStatus (HttpServletResponse .SC_NOT_FOUND );
338246 }
339247 }
340248 }
341249
342250 /**
343251 * Handles an HTTP request for a {@link #HEALTH_TARGET}-related resource.
344- *
345- * @param target the target of the request
346- * @param baseRequest the original unwrapped {@link Request} object
347- * @param request the request either as the {@code Request} object or a
348- * wrapper of that request
349- * @param response the response either as the {@code Response} object or a
350- * wrapper of that response
351252 */
352253 protected void handleHealthJSON (
353254 String target ,
354- Request baseRequest ,
355255 HttpServletRequest request ,
356256 HttpServletResponse response )
357257 throws IOException ,
@@ -360,7 +260,7 @@ protected void handleHealthJSON(
360260 if (GET_HTTP_METHOD .equals (request .getMethod ()))
361261 {
362262 // Check/get the health (status) of the associated server/service.
363- doGetHealthJSON (baseRequest , request , response );
263+ doGetHealthJSON (request , response );
364264 }
365265 else
366266 {
@@ -369,45 +269,34 @@ protected void handleHealthJSON(
369269 }
370270
371271 /**
372- * Handles a specific HTTP request for JSON content.
272+ * Handles a specific HTTP request for JSON content. Returns {@code true}
273+ * if the request was handled.
373274 */
374- protected void handleJSON (
275+ protected boolean handleJSON (
375276 String target ,
376- Request baseRequest ,
377277 HttpServletRequest request ,
378278 HttpServletResponse response )
379279 throws IOException ,
380280 ServletException
381281 {
382282 if (HEALTH_TARGET .equals (target ))
383283 {
384- target = target .substring (HEALTH_TARGET .length ());
385-
386- handleHealthJSON (target , baseRequest , request , response );
284+ handleHealthJSON (target , request , response );
285+ return true ;
387286 }
388287 else if (VERSION_TARGET .equals (target ))
389288 {
390- target = target .substring (VERSION_TARGET .length ());
391-
392- handleVersionJSON (target , baseRequest , request , response );
289+ handleVersionJSON (target , request , response );
290+ return true ;
393291 }
292+ return false ;
394293 }
395294
396295 /**
397296 * Handles an HTTP request for a {@link #VERSION_TARGET}-related resource.
398- *
399- * @param target the target of the request
400- * @param baseRequest the original unwrapped {@link Request} object
401- * @param request the request either as the {@code Request} object or a
402- * wrapper of that request
403- * @param response the response either as the {@code Response} object or a
404- * wrapper of that response
405- * @throws IOException
406- * @throws ServletException
407297 */
408298 protected void handleVersionJSON (
409299 String target ,
410- Request baseRequest ,
411300 HttpServletRequest request ,
412301 HttpServletResponse response )
413302 throws IOException ,
@@ -416,7 +305,7 @@ protected void handleVersionJSON(
416305 if (GET_HTTP_METHOD .equals (request .getMethod ()))
417306 {
418307 // Get the Version of the associated server/service.
419- doGetVersionJSON (baseRequest , request , response );
308+ doGetVersionJSON (request , response );
420309 }
421310 else
422311 {
0 commit comments