org.webbitserver.rest.UriTemplateHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webbit-rest Show documentation
Show all versions of webbit-rest Show documentation
An uri-template based REST API for Webbit
The newest version!
package org.webbitserver.rest;
import org.webbitserver.HttpControl;
import org.webbitserver.HttpHandler;
import org.webbitserver.HttpRequest;
import org.webbitserver.HttpResponse;
import org.webbitserver.rest.furi.FuriProcessor;
import java.net.URI;
import java.util.Map;
public class UriTemplateHandler implements HttpHandler {
private final HttpHandler httpHandler;
private final String uriTemplate;
private final UriTemplateProcessor uriTemplateProcessor;
public UriTemplateHandler(String uriTemplate, HttpHandler httpHandler) {
this(uriTemplate, httpHandler, new FuriProcessor());
}
public UriTemplateHandler(String uriTemplate, HttpHandler httpHandler, UriTemplateProcessor uriTemplateProcessor) {
this.uriTemplate = uriTemplate;
this.httpHandler = httpHandler;
this.uriTemplateProcessor = uriTemplateProcessor;
}
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
String path = URI.create(request.uri()).getPath();
Map variables = uriTemplateProcessor.extract(uriTemplate, path);
if (variables != null) {
request.data(Rest.URI_TEMPLATE_VARIABLES, variables);
httpHandler.handleHttpRequest(request, response, control);
} else {
control.nextHandler();
}
}
}