Skip to content

Commit b9ca440

Browse files
Allow customization of response for non-existent public resources
1 parent 3db9c20 commit b9ca440

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

ng-appserver/src/main/java/ng/appserver/NGApplication.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -476,30 +476,43 @@ private void touchSessionIfPresentAndNotTerminating( final NGRequest request ) {
476476
/**
477477
* Invoked to generate a response if no requestHandler was found for the given request. Essentially a 404 response.
478478
*
479-
* CHECKME: This currently incorporates a very experimental "public resources" handler, essentially a plain web server. This is not final // Hugi 2023-07-20
479+
* Override to provide your own response if no handler was found to handle the given request.
480+
*
481+
* CHECKME: Currently incorporates an experimental "public resources" handler, essentially a plain web server. Not final // Hugi 2023-07-20
480482
*/
481-
private NGResponse noHandlerResponse( final NGRequest request ) {
483+
protected NGResponse noHandlerResponse( final NGRequest request ) {
482484

483-
// FIXME: We've not decided what to do about namespacing and public resources // Hugi 2024-06-26
485+
// FIXME: Haven't decided what to do about namespaces and public resources // Hugi 2024-06-26
484486
final String namespace = "app";
485487
final String resourcePath = request.uri();
486488

489+
final Optional<NGResource> resource;
490+
487491
if( resourcePath.isEmpty() ) {
488-
return new NGResponse( "No resource name specified", 400 );
492+
resource = Optional.empty();
493+
}
494+
else {
495+
resource = resourceManager().obtainPublicResource( namespace, resourcePath );
489496
}
490497

491-
final Optional<NGResource> resource = resourceManager().obtainPublicResource( namespace, resourcePath );
492-
493-
// FIXME: Shouldn't we allow the user to customize the response for a non-existent resource? // Hugi 2024-02-05
494498
if( resource.isEmpty() ) {
495-
final NGResponse errorResponse = new NGResponse( "public resource '" + resourcePath + "' does not exist", 404 );
496-
errorResponse.setHeader( "content-type", "text/html" );
497-
return errorResponse;
499+
return noPublicResourceResponse( request );
498500
}
499501

500502
return NGResourceRequestHandler.responseForResource( resource.get(), resourcePath );
501503
}
502504

505+
/**
506+
* The response returned if no handler was found and then, no public resource was found either.
507+
*
508+
* Override to provide your own response if no handler/public resource was found to handle the given request.
509+
*/
510+
protected NGResponse noPublicResourceResponse( final NGRequest request ) {
511+
final NGResponse response = new NGResponse( "URL '%s' not found".formatted( request.uri() ), 404 );
512+
response.setHeader( "content-type", "text/html" );
513+
return response;
514+
}
515+
503516
/**
504517
* @return A session cookie for the given session
505518
*

0 commit comments

Comments
 (0)