101101 */
102102public final class JettyHttpContainer extends AbstractHandler implements Container {
103103
104- private static final ExtendedLogger logger =
104+ private static final ExtendedLogger LOGGER =
105105 new ExtendedLogger (Logger .getLogger (JettyHttpContainer .class .getName ()), Level .FINEST );
106106
107- private static final Type RequestTYPE = (new TypeLiteral <Ref <Request >>() {}).getType ();
108- private static final Type ResponseTYPE = (new TypeLiteral <Ref <Response >>() {}).getType ();
107+ private static final Type REQUEST_TYPE = (new TypeLiteral <Ref <Request >>() {}).getType ();
108+ private static final Type RESPONSE_TYPE = (new TypeLiteral <Ref <Response >>() {}).getType ();
109+
110+ private static final int INTERNAL_SERVER_ERROR = javax .ws .rs .core .Response .Status .INTERNAL_SERVER_ERROR .getStatusCode ();
109111
110112 /**
111113 * Cached value of configuration property
@@ -161,15 +163,17 @@ protected void configure() {
161163 private volatile ContainerLifecycleListener containerListener ;
162164
163165 @ Override
164- public void handle (final String target , final Request request , final HttpServletRequest httpServletRequest , final HttpServletResponse httpServletResponse ) throws IOException , ServletException {
166+ public void handle (final String target , final Request request , final HttpServletRequest httpServletRequest ,
167+ final HttpServletResponse httpServletResponse ) throws IOException , ServletException {
168+
165169 final Response response = Response .getResponse (httpServletResponse );
166170 final ResponseWriter responseWriter = new ResponseWriter (request , response , configSetStatusOverSendError );
167171 final URI baseUri = getBaseUri (request );
168172
169173 final String originalQuery = request .getUri ().getQuery ();
170174 final String encodedQuery = ContainerUtils .encodeUnsafeCharacters (originalQuery );
171- final String uriString = (originalQuery == null || originalQuery .isEmpty () || originalQuery .equals (encodedQuery )) ?
172- request .getUri ().toString () : request .getUri ().toString ().replace (originalQuery , encodedQuery );
175+ final String uriString = (originalQuery == null || originalQuery .isEmpty () || originalQuery .equals (encodedQuery ))
176+ ? request .getUri ().toString () : request .getUri ().toString ().replace (originalQuery , encodedQuery );
173177 final URI requestUri = baseUri .resolve (uriString );
174178 try {
175179 final ContainerRequest requestContext = new ContainerRequest (
@@ -188,8 +192,8 @@ public void handle(final String target, final Request request, final HttpServlet
188192 requestContext .setRequestScopedInitializer (new RequestScopedInitializer () {
189193 @ Override
190194 public void initialize (final ServiceLocator locator ) {
191- locator .<Ref <Request >>getService (RequestTYPE ).set (request );
192- locator .<Ref <Response >>getService (ResponseTYPE ).set (response );
195+ locator .<Ref <Request >>getService (REQUEST_TYPE ).set (request );
196+ locator .<Ref <Response >>getService (RESPONSE_TYPE ).set (response );
193197 }
194198 });
195199
@@ -250,6 +254,7 @@ private String getBasePath(final Request request) {
250254 }
251255
252256 private static final class ResponseWriter implements ContainerResponseWriter {
257+
253258 private final Response response ;
254259 private final Continuation continuation ;
255260 private final boolean configSetStatusOverSendError ;
@@ -261,11 +266,14 @@ private static final class ResponseWriter implements ContainerResponseWriter {
261266 }
262267
263268 @ Override
264- public OutputStream writeResponseStatusAndHeaders (final long contentLength , final ContainerResponse context ) throws ContainerException {
269+ public OutputStream writeResponseStatusAndHeaders (final long contentLength , final ContainerResponse context )
270+ throws ContainerException {
271+
265272 final javax .ws .rs .core .Response .StatusType statusInfo = context .getStatusInfo ();
266273
267274 final int code = statusInfo .getStatusCode ();
268- final String reason = statusInfo .getReasonPhrase () == null ? HttpStatus .getMessage (code ) : statusInfo .getReasonPhrase ();
275+ final String reason = statusInfo .getReasonPhrase () == null
276+ ? HttpStatus .getMessage (code ) : statusInfo .getReasonPhrase ();
269277
270278 response .setStatusWithReason (code , reason );
271279
@@ -304,7 +312,7 @@ public void onTimeout(final Continuation continuation) {
304312 }
305313 }
306314 });
307- continuation .suspend ();
315+ continuation .suspend (response );
308316 return true ;
309317 } catch (final Exception ex ) {
310318 return false ;
@@ -322,14 +330,14 @@ public void setSuspendTimeout(final long timeOut, final TimeUnit timeUnit) throw
322330 @ Override
323331 public void commit () {
324332 try {
325- if (continuation .isSuspended ()) {
326- continuation .resume ();
327- }
328333 response .closeOutput ();
329334 } catch (final IOException e ) {
330- logger .log (Level .WARNING , LocalizationMessages .UNABLE_TO_CLOSE_RESPONSE (), e );
335+ LOGGER .log (Level .WARNING , LocalizationMessages .UNABLE_TO_CLOSE_RESPONSE (), e );
331336 } finally {
332- logger .log (Level .FINEST , "commit() called" );
337+ if (continuation .isSuspended ()) {
338+ continuation .complete ();
339+ }
340+ LOGGER .log (Level .FINEST , "commit() called" );
333341 }
334342 }
335343
@@ -340,21 +348,21 @@ public void failure(final Throwable error) {
340348 try {
341349 if (configSetStatusOverSendError ) {
342350 response .reset ();
343- response .setStatus (500 , "Request failed." );
351+ //noinspection deprecation
352+ response .setStatus (INTERNAL_SERVER_ERROR , "Request failed." );
344353 } else {
345- response .sendError (500 , "Request failed." );
354+ response .sendError (INTERNAL_SERVER_ERROR , "Request failed." );
346355 }
347356 } catch (final IllegalStateException ex ) {
348357 // a race condition externally committing the response can still occur...
349- logger .log (Level .FINER , "Unable to reset failed response." , ex );
358+ LOGGER .log (Level .FINER , "Unable to reset failed response." , ex );
350359 } catch (final IOException ex ) {
351- throw new ContainerException (
352- LocalizationMessages .EXCEPTION_SENDING_ERROR_RESPONSE (500 , "Request failed." ),
353- ex );
360+ throw new ContainerException (LocalizationMessages .EXCEPTION_SENDING_ERROR_RESPONSE (INTERNAL_SERVER_ERROR ,
361+ "Request failed." ), ex );
354362 }
355363 }
356364 } finally {
357- logger .log (Level .FINEST , "failure(...) called" );
365+ LOGGER .log (Level .FINEST , "failure(...) called" );
358366 commit ();
359367 rethrow (error );
360368 }
@@ -365,9 +373,8 @@ public boolean enableResponseBuffering() {
365373 return false ;
366374 }
367375
368-
369376 /**
370- * Rethrow the original exception as required by JAX-RS, 3.3.4
377+ * Rethrow the original exception as required by JAX-RS, 3.3.4.
371378 *
372379 * @param error throwable to be re-thrown
373380 */
@@ -381,7 +388,6 @@ private void rethrow(final Throwable error) {
381388
382389 }
383390
384-
385391 @ Override
386392 public ResourceConfig getConfiguration () {
387393 return appHandler .getConfiguration ();
@@ -409,8 +415,9 @@ public ApplicationHandler getApplicationHandler() {
409415
410416 /**
411417 * Inform this container that the server has been started.
412- *
413418 * This method must be implicitly called after the server containing this container is started.
419+ *
420+ * @throws java.lang.Exception if a problem occurred during server startup.
414421 */
415422 @ Override
416423 protected void doStart () throws Exception {
@@ -420,8 +427,9 @@ protected void doStart() throws Exception {
420427
421428 /**
422429 * Inform this container that the server is being stopped.
423- *
424430 * This method must be implicitly called before the server containing this container is stopped.
431+ *
432+ * @throws java.lang.Exception if a problem occurred during server shutdown.
425433 */
426434 @ Override
427435 public void doStop () throws Exception {
0 commit comments