Class ResponseCreationSupport
Response
events.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
DefaultMaxAgeCalculator provides an implementation that tries to guess a good max age value by looking at the path of the requested resource.static interface
Describes a calculator for the max-age property.static class
Combines the known information about a resource. -
Field Summary
Modifier and TypeFieldDescriptionstatic final ResponseCreationSupport.MaxAgeCalculator
A default implementation for the max-age calculator. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionresourceInfo
(URL resource) Attempts to lookup the additional resource information for the given URL.static void
sendResponse
(HttpRequest request, IOSubchannel channel, int statusCode, String reasonPhrase) Send a response to the given request with the given status code and reason phrase, including atext/plain
body with the status code and reason phrase.static void
sendResponse
(HttpRequest request, IOSubchannel channel, HttpConstants.HttpStatus status) Shorthand for invokingsendResponse(HttpRequest, IOSubchannel, int, String)
with a predefined HTTP status.static boolean
sendStaticContent
(HttpRequest request, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Creates and sends a response with static content.static boolean
sendStaticContent
(Request.In event, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Shorthand for invokingsendStaticContent(HttpRequest, IOSubchannel, Function, MaxAgeCalculator)
with theHttpRequest
from the event.static long
setMaxAge
(HttpResponse response, int maxAge) Sets the cache control header in the given response.static URI
uriFromPath
(String path) Create aURI
from a path.static URI
uriFromUrl
(URL url)
-
Field Details
-
DEFAULT_MAX_AGE_CALCULATOR
A default implementation for the max-age calculator.
-
-
Constructor Details
-
ResponseCreationSupport
public ResponseCreationSupport()
-
-
Method Details
-
sendResponse
public static void sendResponse(HttpRequest request, IOSubchannel channel, int statusCode, String reasonPhrase) Send a response to the given request with the given status code and reason phrase, including atext/plain
body with the status code and reason phrase.- Parameters:
request
- the requestchannel
- for responding; events will be sent usingIOSubchannel.respond(org.jgrapes.core.Event)
statusCode
- the status code to sendreasonPhrase
- the reason phrase to send
-
sendResponse
public static void sendResponse(HttpRequest request, IOSubchannel channel, HttpConstants.HttpStatus status) Shorthand for invokingsendResponse(HttpRequest, IOSubchannel, int, String)
with a predefined HTTP status.- Parameters:
request
- the requestchannel
- the channelstatus
- the status
-
sendStaticContent
public static boolean sendStaticContent(HttpRequest request, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Creates and sends a response with static content.The content is looked up by invoking the resolver with the path from the request.
The response includes a max-age header with a default value of 600. The value may be modified by specifying validity infos.
- Parameters:
request
- the requestchannel
- the channelresolver
- the resolvermaxAgeCalculator
- the max age calculator, ifnull
the default calculator is used.- Returns:
true
if a response was sent
-
sendStaticContent
public static boolean sendStaticContent(Request.In event, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Shorthand for invokingsendStaticContent(HttpRequest, IOSubchannel, Function, MaxAgeCalculator)
with theHttpRequest
from the event.Also sets the result of the event to
true
and invokesEvent.stop()
if a response was sent.- Parameters:
event
- the eventchannel
- the channelresolver
- the resolvermaxAgeCalculator
- the max age calculator, ifnull
the default calculator is used.- Returns:
true
if a response was sent- Throws:
ParseException
- the parse exception
-
resourceInfo
Attempts to lookup the additional resource information for the given URL.If a
URL
references a file, it is easy to find out if the resource referenced is a directory and to get its last modification time. Getting the same information for aURL
that references resources in a jar is a bit more difficult. This method handles both cases.- Parameters:
resource
- the resource URL- Returns:
- the resource info
-
uriFromPath
Create aURI
from a path.This is similar to calling
new URI(null, null, path, null)
with theURISyntaxException
converted to aIllegalArgumentException
.- Parameters:
path
- the path- Returns:
- the uri
- Throws:
IllegalArgumentException
- if the string violates RFC 2396
-
uriFromUrl
Create aURI
from aURL
.This is similar to calling
url.toURI()
with theURISyntaxException
converted to aIllegalArgumentException
.- Parameters:
url
- the url- Returns:
- the uri
- Throws:
IllegalArgumentException
- if the url violates RFC 2396
-
setMaxAge
Sets the cache control header in the given response.- Parameters:
response
- the responsemaxAge
- the max age- Returns:
- the value set
-