java-undertow-server.handler.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
package org.openapitools.handler;
import com.networknt.server.HandlerProvider;
import io.undertow.Handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.util.Methods;
/**
* The default implementation for {@link HandlerProvider} and {@link PathHandlerInterface}.
*
* There are two flavors of {@link HttpHandler}s to choose from, depending on your needs:
*
*
* -
* Stateless: if a specific endpoint is called more than once from multiple sessions,
* its state is not retained – a different {@link HttpHandler} is instantiated for every new
* session. This is the default behavior.
*
* -
* Stateful: if a specific endpoint is called more than once from multiple sessions,
* its state is retained properly. For example, if you want to keep a class property that counts
* the number of requests or the last time a request was received.
*
*
* Note: Stateful flavor is more performant than Stateless.
*/
@SuppressWarnings("TooManyFunctions")
abstract public class PathHandlerProvider implements HandlerProvider, PathHandlerInterface {
/**
* Returns the default base path to access this server.
*/
@javax.annotation.Nonnull
public String getBasePath() {
return "{{{basePathWithoutHost}}}";
}
/**
* Returns a stateless {@link HttpHandler} that configures all endpoints in this server.
*
* Endpoints bound in this method do NOT start with "{{{basePathWithoutHost}}}", and
* it's your responsibility to configure a {@link PathHandler} with a prefix path
* by calling {@link PathHandler#addPrefixPath} like so:
*
* pathHandler.addPrefixPath("{{{basePathWithoutHost}}}", handler)
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateless and won't
* retain any state between multiple sessions.
*
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@javax.annotation.Nonnull
@Override
public HttpHandler getHandler() {
return getHandler(false);
}
/**
* Returns a stateless {@link HttpHandler} that configures all endpoints in this server.
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateless and won't
* retain any state between multiple sessions.
*
* @param withBasePath if true, all endpoints would start with "{{{basePathWithoutHost}}}"
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@javax.annotation.Nonnull
public HttpHandler getHandler(final boolean withBasePath) {
return getHandler(withBasePath ? getBasePath() : "");
}
/**
* Returns a stateless {@link HttpHandler} that configures all endpoints in this server.
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateless and won't
* retain any state between multiple sessions.
*
* @param basePath base path to set for all endpoints
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@SuppressWarnings("Convert2Lambda")
@javax.annotation.Nonnull
public HttpHandler getHandler(final String basePath) {
return Handlers.routing()
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
.add(Methods.{{{httpMethod}}}, basePath + "{{{path}}}", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
{{{operationId}}}().handleRequest(exchange);
}
})
{{/operation}}
{{/operations}}
{{/apis}}
;
{{/apiInfo}}
}
/**
* Returns a stateful {@link HttpHandler} that configures all endpoints in this server.
*
* Endpoints bound in this method do NOT start with "{{{basePathWithoutHost}}}", and
* it's your responsibility to configure a {@link PathHandler} with a prefix path
* by calling {@link PathHandler#addPrefixPath} like so:
*
* pathHandler.addPrefixPath("{{{basePathWithoutHost}}}", handler)
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateful and will
* retain any state between multiple sessions.
*
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@javax.annotation.Nonnull
public HttpHandler getStatefulHandler() {
return getStatefulHandler(false);
}
/**
* Returns a stateful {@link HttpHandler} that configures all endpoints in this server.
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateful and will
* retain any state between multiple sessions.
*
* @param withBasePath if true, all endpoints would start with "{{{basePathWithoutHost}}}"
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@javax.annotation.Nonnull
public HttpHandler getStatefulHandler(final boolean withBasePath) {
return getStatefulHandler(withBasePath ? getBasePath() : "");
}
/**
* Returns a stateful {@link HttpHandler} that configures all endpoints in this server.
*
* Note: the endpoints bound to the returned {@link HttpHandler} are stateful and will
* retain any state between multiple sessions.
*
* @param basePath base path to set for all endpoints
* @return an {@link HttpHandler} of type {@link RoutingHandler}
*/
@javax.annotation.Nonnull
public HttpHandler getStatefulHandler(final String basePath) {
return Handlers.routing()
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
.add(Methods.{{{httpMethod}}}, basePath + "{{{path}}}", {{{operationId}}}())
{{/operation}}
{{/operations}}
{{/apis}}
;
{{/apiInfo}}
}
}