- java.lang.Object
-
- org.refcodes.rest.RestEndpointBuilder
-
- All Implemented Interfaces:
org.refcodes.component.Closable
,org.refcodes.component.Closable.CloseBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
,org.refcodes.component.ConnectionStatusAccessor
,org.refcodes.component.LinkComponent
,org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>
,org.refcodes.component.Openable
,org.refcodes.component.Openable.OpenBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
,org.refcodes.component.OpenedAccessor
,RestEndpoint
,RestRequestConsumer
,org.refcodes.web.HttpMethodAccessor
,org.refcodes.web.HttpMethodAccessor.HttpMethodBuilder<RestEndpointBuilder>
,org.refcodes.web.HttpMethodAccessor.HttpMethodMutator
,org.refcodes.web.HttpMethodAccessor.HttpMethodProperty
public class RestEndpointBuilder extends Object implements RestEndpoint, org.refcodes.web.HttpMethodAccessor.HttpMethodProperty, org.refcodes.web.HttpMethodAccessor.HttpMethodBuilder<RestEndpointBuilder>, org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>, org.refcodes.component.ConnectionStatusAccessor
AnRestEndpointBuilder
extends anRestEndpoint
with builder functionality and addslambda
support for handling the requests addressed to thisRestEndpoint
. Thelambda
defined asRestRequestConsumer
acts as the single listener to thisRestEndpoint
responsible for handling the requests for which thisRestEndpoint
is responsible. The locator for which anRestEndpointBuilder
is responsible for is defined by theRestEndpointBuilder
's Locator-Pattern: A single asterisk ("*") matches zero or more characters within a locator name. A double asterisk ("**") matches zero or more characters across directory levels. A question mark ("?") matches exactly one character within a locator name. The single asterisk ("*"), the double asterisk ("**") and the question mark ("?") we refer to as wildcard: You get an array with the substitutes of the wildcards using the methodRestRequestEvent.getWildcardReplacements()
. You may name a wildcard by prefixing it with "${someWildcardName}". For example a named wildcard may look as follows: "${arg1}=*" or "${arg2}=**" or "${arg3}=?". You can get the text substituting a named wildcard using the methodRestRequestEvent.getWildcardReplacement(String)
. For ease of use, a named wildcard with single asterisk ("*") such as "${arg1}=*" can be abbreviated as "${arg1}".
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.refcodes.component.Closable
org.refcodes.component.Closable.CloseAutomaton, org.refcodes.component.Closable.CloseBuilder<B extends org.refcodes.component.Closable.CloseBuilder<B>>
-
Nested classes/interfaces inherited from interface org.refcodes.component.ConnectionStatusAccessor
org.refcodes.component.ConnectionStatusAccessor.ConnectionStatusMutator, org.refcodes.component.ConnectionStatusAccessor.ConnectionStatusProperty
-
Nested classes/interfaces inherited from interface org.refcodes.web.HttpMethodAccessor
org.refcodes.web.HttpMethodAccessor.HttpMethodBuilder<B extends org.refcodes.web.HttpMethodAccessor.HttpMethodBuilder<B>>, org.refcodes.web.HttpMethodAccessor.HttpMethodMutator, org.refcodes.web.HttpMethodAccessor.HttpMethodProperty
-
Nested classes/interfaces inherited from interface org.refcodes.component.LinkComponent
org.refcodes.component.LinkComponent.LinkAutomaton, org.refcodes.component.LinkComponent.LinkComponentBuilder<B extends org.refcodes.component.LinkComponent.LinkComponentBuilder<B>>
-
-
Field Summary
Fields Modifier and Type Field Description protected org.refcodes.component.ConnectionStatus
_connectionStatus
protected org.refcodes.web.HttpMethod
_httpMethod
protected String
_locatorPathPattern
protected Pattern
_locatorRegExp
protected RestRequestConsumer
_requestObserver
-
Constructor Summary
Constructors Constructor Description RestEndpointBuilder()
Constructs a plainRestEndpointBuilder
, make sure to provide the least required attributes as demonstrated by the constructorRestEndpointBuilder(HttpMethod, String, RestRequestConsumer)
.RestEndpointBuilder(org.refcodes.web.HttpMethod aHttpMethod, String aLocatorPathPattern, RestRequestConsumer aRequestConsumer)
Constructs anRestEndpointBuilder
with the least required attributes.RestEndpointBuilder(org.refcodes.web.HttpMethod aHttpMethod, Pattern aLocatorRegExp, RestRequestConsumer aRequestConsumer)
Constructs anRestEndpointBuilder
with the least required attributes.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
org.refcodes.component.ConnectionStatus
getConnectionStatus()
org.refcodes.web.HttpMethod
getHttpMethod()
String
getLocatorPathPattern()
Retrieves the resource Locator-Pattern this endpoint has been registered to.Pattern
getLocatorRegExp()
Retrieves the resource locator (regular expression) pattern this endpoint has been registered to.RestRequestConsumer
getRequestObserver()
Retrieves theRestRequestConsumer
to which any requests are delegated upon invocation of theonRequest(RestRequestEvent, org.refcodes.web.HttpServerResponse)
method.void
onRequest(RestRequestEvent aRequest, org.refcodes.web.HttpServerResponse aResponse)
The invoker provides a request context being aRestRequestEvent
) describing the request and a response being aHttpServerResponse
to be processed upon by yourlambda
's code.void
open()
void
setHttpMethod(org.refcodes.web.HttpMethod aHttpMethod)
void
setLocatorPathPattern(String aLocatorPathPattern)
Sets the resource locator pattern.void
setLocatorRegExp(Pattern aLocatorRegExp)
Sets the resource locator (regular expression) pattern.void
setRequestObserver(RestRequestConsumer aLambda)
Sets theRestRequestConsumer
to which any requests are delegated upon invocation of theonRequest(RestRequestEvent, org.refcodes.web.HttpServerResponse)
method.RestEndpointBuilder
withClose()
RestEndpointBuilder
withCloseIn(int aCloseInMillis)
RestEndpointBuilder
withCloseQuietly()
RestEndpointBuilder
withHttpMethod(org.refcodes.web.HttpMethod aHttpMethod)
RestEndpointBuilder
withLocatorPathPattern(String aLocatorPathPattern)
Sets the resource locator pattern as of the Builder-Pattern.RestEndpointBuilder
withLocatorRegExp(Pattern aLocatorRegExp)
Sets the resource locator (regular expression) pattern as of the Builder-Pattern.RestEndpointBuilder
withOpen()
RestEndpointBuilder
withRequestObserver(RestRequestConsumer aLambda)
Builder method for setting theRestRequestConsumer
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.refcodes.component.Closable
closeIn, closeQuietly, closeUnchecked
-
-
-
-
Field Detail
-
_requestObserver
protected RestRequestConsumer _requestObserver
-
_httpMethod
protected org.refcodes.web.HttpMethod _httpMethod
-
_locatorPathPattern
protected String _locatorPathPattern
-
_locatorRegExp
protected Pattern _locatorRegExp
-
_connectionStatus
protected org.refcodes.component.ConnectionStatus _connectionStatus
-
-
Constructor Detail
-
RestEndpointBuilder
public RestEndpointBuilder()
Constructs a plainRestEndpointBuilder
, make sure to provide the least required attributes as demonstrated by the constructorRestEndpointBuilder(HttpMethod, String, RestRequestConsumer)
.
-
RestEndpointBuilder
public RestEndpointBuilder(org.refcodes.web.HttpMethod aHttpMethod, String aLocatorPathPattern, RestRequestConsumer aRequestConsumer)
Constructs anRestEndpointBuilder
with the least required attributes.- Parameters:
aHttpMethod
- The HTTP-Method to which thisRestEndpointBuilder
is bound.aLocatorPathPattern
- The local host's locator pattern to which thisRestEndpointBuilder
is bound. SeesetLocatorPathPattern(String)
on the syntax of the pattern.aRequestConsumer
- The listener processing requests targeted at thisRestEndpointBuilder
.
-
RestEndpointBuilder
public RestEndpointBuilder(org.refcodes.web.HttpMethod aHttpMethod, Pattern aLocatorRegExp, RestRequestConsumer aRequestConsumer)
Constructs anRestEndpointBuilder
with the least required attributes.- Parameters:
aHttpMethod
- The HTTP-Method to which thisRestEndpointBuilder
is bound.aLocatorRegExp
- The local host's locator (regular expression) pattern to which thisRestEndpointBuilder
is bound. SeesetLocatorPathPattern(String)
on the syntax of the pattern.aRequestConsumer
- The listener processing requests targeted at thisRestEndpointBuilder
.
-
-
Method Detail
-
withHttpMethod
public RestEndpointBuilder withHttpMethod(org.refcodes.web.HttpMethod aHttpMethod)
- Specified by:
withHttpMethod
in interfaceorg.refcodes.web.HttpMethodAccessor.HttpMethodBuilder<RestEndpointBuilder>
-
withLocatorPathPattern
public RestEndpointBuilder withLocatorPathPattern(String aLocatorPathPattern)
Sets the resource locator pattern as of the Builder-Pattern. The locator may contain wildcards as known from file-systems as follows: A single asterisk ("*") matches zero or more characters within a locator name. A double asterisk ("**") matches zero or more characters across directory levels. A question mark ("?") matches exactly one character within a locator name. You may name a wildcard by prefixing it with "${someWildcardName}". For example a named wildcard may look as follows: "${arg1}=*" or "${arg2}=**" or "${arg3}=?". You can get the text substituting a named wildcard using the methodRestRequestEvent.getWildcardReplacement(String)
. For ease of use, a named wildcard with single asterisk ("*") such as "${arg1}=*" can be abbreviated as "${arg1}".- Parameters:
aLocatorPathPattern
- the locator pattern- Returns:
- The
RestEndpoint
builder to continue configuration (as of the Builder-Pattern).
-
withLocatorRegExp
public RestEndpointBuilder withLocatorRegExp(Pattern aLocatorRegExp)
Sets the resource locator (regular expression) pattern as of the Builder-Pattern.- Parameters:
aLocatorRegExp
- the locator (regular expression) pattern- Returns:
- The
RestEndpoint
builder to continue configuration (as of the Builder-Pattern).
-
withRequestObserver
public RestEndpointBuilder withRequestObserver(RestRequestConsumer aLambda)
Builder method for setting theRestRequestConsumer
.- Parameters:
aLambda
- The (user defined)RestRequestConsumer
to handle requests, feel free to code it aslambda
expression- Returns:
- The
RestEndpointBuilder
for the sake of a fluent API.
-
withOpen
public RestEndpointBuilder withOpen() throws IOException
- Specified by:
withOpen
in interfaceorg.refcodes.component.Openable.OpenBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
- Throws:
IOException
-
withClose
public RestEndpointBuilder withClose() throws IOException
- Specified by:
withClose
in interfaceorg.refcodes.component.Closable.CloseBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
- Throws:
IOException
-
withCloseQuietly
public RestEndpointBuilder withCloseQuietly()
- Specified by:
withCloseQuietly
in interfaceorg.refcodes.component.Closable.CloseBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
-
withCloseIn
public RestEndpointBuilder withCloseIn(int aCloseInMillis)
- Specified by:
withCloseIn
in interfaceorg.refcodes.component.Closable.CloseBuilder<org.refcodes.component.LinkComponent.LinkComponentBuilder<RestEndpointBuilder>>
-
setLocatorPathPattern
public void setLocatorPathPattern(String aLocatorPathPattern)
Sets the resource locator pattern. The locator may contain wildcards as known from file-systems as follows: A single asterisk ("*") matches zero or more characters within a locator name. A double asterisk ("**") matches zero or more characters across directory levels. A question mark ("?") matches exactly one character within a locator name. You may name a wildcard by prefixing it with "${someWildcardName}". For example a named wildcard may look as follows: "${arg1}=*" or "${arg2}=**" or "${arg3}=?". You can get the text substituting a named wildcard using the methodRestRequestEvent.getWildcardReplacement(String)
. For ease of use, a named wildcard with single asterisk ("*") such as "${arg1}=*" can be abbreviated as "${arg1}".- Parameters:
aLocatorPathPattern
- the new locator pattern
-
setLocatorRegExp
public void setLocatorRegExp(Pattern aLocatorRegExp)
Sets the resource locator (regular expression) pattern.- Parameters:
aLocatorRegExp
- the new locator (regular expression) pattern
-
setHttpMethod
public void setHttpMethod(org.refcodes.web.HttpMethod aHttpMethod)
- Specified by:
setHttpMethod
in interfaceorg.refcodes.web.HttpMethodAccessor.HttpMethodMutator
-
setRequestObserver
public void setRequestObserver(RestRequestConsumer aLambda)
Sets theRestRequestConsumer
to which any requests are delegated upon invocation of theonRequest(RestRequestEvent, org.refcodes.web.HttpServerResponse)
method.- Parameters:
aLambda
- The (user defined)RestRequestConsumer
to handle requests, feel free to code it aslambda
expression!
-
getRequestObserver
public RestRequestConsumer getRequestObserver()
Retrieves theRestRequestConsumer
to which any requests are delegated upon invocation of theonRequest(RestRequestEvent, org.refcodes.web.HttpServerResponse)
method.- Returns:
- The (user defined)
RestRequestConsumer
to handle requests.
-
onRequest
public void onRequest(RestRequestEvent aRequest, org.refcodes.web.HttpServerResponse aResponse) throws org.refcodes.web.HttpStatusException
The invoker provides a request context being aRestRequestEvent
) describing the request and a response being aHttpServerResponse
to be processed upon by yourlambda
's code. The method works synchronously and waits (blocks the caller's thread) till it finishes execution.- Specified by:
onRequest
in interfaceRestRequestConsumer
- Parameters:
aRequest
- The request of typeRestRequestEvent
describing the request context. UseHttpServerRequest.getRequest(Class)
to retrieve the caller's request body orAbstractHttpRequest.getHeaderFields()
to retrieve the request's cookies and other Header-Fields.aResponse
- The response of typeHttpServerResponse
do be processed upon by the method's implementation. UseHttpServerResponse.setResponse(Object)
to provide a response for the client. UseAbstractHttpResponse.getHeaderFields()
to set cookies or modify Header-Fields. Throw one of theHttpStatusException
sub-types to signal an according erroneous HTTP state.- Throws:
org.refcodes.web.HttpStatusException
- to be thrown in case something went wrong.
-
getHttpMethod
public org.refcodes.web.HttpMethod getHttpMethod()
- Specified by:
getHttpMethod
in interfaceorg.refcodes.web.HttpMethodAccessor
-
getLocatorPathPattern
public String getLocatorPathPattern()
Retrieves the resource Locator-Pattern this endpoint has been registered to. The Locator-Pattern may contain wildcards as known from file-systems as follows: A single asterisk ("*") matches zero or more characters within a locator name. A double asterisk ("**") matches zero or more characters across directory levels. A question mark ("?") matches exactly one character within a locator name. The single asterisk ("*"), the double asterisk ("**") and the question mark ("?") we refer to as wildcard: You get an array with the substitutes of the wildcards using theRestRequestEvent
's methodRestRequestEvent.getWildcardReplacements()
inside theRestRequestConsumer.onRequest(RestRequestEvent, org.refcodes.web.HttpServerResponse)
method. You may name a wildcard by prefixing it with "{someWildcardName}". For example a named wildcard may look as follows: "{arg1}*" or "{arg2}**" or "{arg3}?". TheRestRequestEvent
lets you access the wildcard substitutes either by index or by name.- Specified by:
getLocatorPathPattern
in interfaceRestEndpoint
-
getLocatorRegExp
public Pattern getLocatorRegExp()
Retrieves the resource locator (regular expression) pattern this endpoint has been registered to. TheRestRequestEvent
lets you access the matching regular expression groups either by index or by name.- Specified by:
getLocatorRegExp
in interfaceRestEndpoint
-
open
public void open() throws IOException
- Specified by:
open
in interfaceorg.refcodes.component.Openable
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceorg.refcodes.component.Closable
- Throws:
IOException
-
getConnectionStatus
public org.refcodes.component.ConnectionStatus getConnectionStatus()
- Specified by:
getConnectionStatus
in interfaceorg.refcodes.component.ConnectionStatusAccessor
-
-