Class ResponseCreationSupport
Response events.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefaultMaxAgeCalculator provides an implementation that tries to guess a good max age value by looking at the path of the requested resource.static interfaceDescribes a calculator for the max-age property.static classCombines the known information about a resource. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ResponseCreationSupport.MaxAgeCalculatorA default implementation for the max-age calculator. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionresourceInfo(URL resource) Attempts to lookup the additional resource information for the given URL.static voidsendResponse(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/plainbody with the status code and reason phrase.static voidsendResponse(HttpRequest request, IOSubchannel channel, HttpConstants.HttpStatus status) Shorthand for invokingsendResponse(HttpRequest, IOSubchannel, int, String)with a predefined HTTP status.static booleansendStaticContent(HttpRequest request, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Creates and sends a response with static content.static booleansendStaticContent(Request.In event, IOSubchannel channel, Function<String, URL> resolver, ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Shorthand for invokingsendStaticContent(HttpRequest, IOSubchannel, Function, MaxAgeCalculator)with theHttpRequestfrom the event.static longsetMaxAge(HttpResponse response, int maxAge) Sets the cache control header in the given response.static URIuriFromPath(String path) Create aURIfrom a path.static URIuriFromUrl(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/plainbody 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, ifnullthe default calculator is used.- Returns:
trueif 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 theHttpRequestfrom the event.Also sets the result of the event to
trueand invokesEvent.stop()if a response was sent.- Parameters:
event- the eventchannel- the channelresolver- the resolvermaxAgeCalculator- the max age calculator, ifnullthe default calculator is used.- Returns:
trueif a response was sent- Throws:
ParseException- the parse exception
-
resourceInfo
Attempts to lookup the additional resource information for the given URL.If a
URLreferences 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 aURLthat 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 aURIfrom a path.This is similar to calling
new URI(null, null, path, null)with theURISyntaxExceptionconverted to aIllegalArgumentException.- Parameters:
path- the path- Returns:
- the uri
- Throws:
IllegalArgumentException- if the string violates RFC 2396
-
uriFromUrl
Create aURIfrom aURL.This is similar to calling
url.toURI()with theURISyntaxExceptionconverted 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
-