use of spark.routematch.RouteMatch in project spark by perwendel.
the class Routes method execute.
static void execute(RouteContext context) throws Exception {
Object content = context.body().get();
RouteMatch match = context.routeMatcher().find(context.httpMethod(), context.uri(), context.acceptType());
Object target = null;
if (match != null) {
target = match.getTarget();
} else if (context.httpMethod() == HttpMethod.head && context.body().notSet()) {
// See if get is mapped to provide default head mapping
content = context.routeMatcher().find(HttpMethod.get, context.uri(), context.acceptType()) != null ? "" : null;
}
if (target != null) {
Object result = null;
if (target instanceof RouteImpl) {
RouteImpl route = ((RouteImpl) target);
if (context.requestWrapper().getDelegate() == null) {
Request request = RequestResponseFactory.create(match, context.httpRequest());
context.requestWrapper().setDelegate(request);
} else {
context.requestWrapper().changeMatch(match);
}
context.responseWrapper().setDelegate(context.response());
Object element = route.handle(context.requestWrapper(), context.responseWrapper());
result = route.render(element);
}
if (result != null) {
content = result;
if (content instanceof String) {
String contentStr = (String) content;
if (!contentStr.equals("")) {
context.responseWrapper().body(contentStr);
}
}
}
}
context.body().set(content);
}
use of spark.routematch.RouteMatch in project spark by perwendel.
the class AfterFilters method execute.
static void execute(RouteContext context) throws Exception {
Object content = context.body().get();
List<RouteMatch> matchSet = context.routeMatcher().findMultiple(HttpMethod.after, context.uri(), context.acceptType());
for (RouteMatch filterMatch : matchSet) {
Object filterTarget = filterMatch.getTarget();
if (filterTarget instanceof FilterImpl) {
if (context.requestWrapper().getDelegate() == null) {
Request request = RequestResponseFactory.create(filterMatch, context.httpRequest());
context.requestWrapper().setDelegate(request);
} else {
context.requestWrapper().changeMatch(filterMatch);
}
context.responseWrapper().setDelegate(context.response());
FilterImpl filter = (FilterImpl) filterTarget;
filter.handle(context.requestWrapper(), context.responseWrapper());
String bodyAfterFilter = context.response().body();
if (bodyAfterFilter != null) {
content = bodyAfterFilter;
}
}
}
context.body().set(content);
}
use of spark.routematch.RouteMatch in project spark by perwendel.
the class BeforeFilters method execute.
static void execute(RouteContext context) throws Exception {
Object content = context.body().get();
List<RouteMatch> matchSet = context.routeMatcher().findMultiple(HttpMethod.before, context.uri(), context.acceptType());
for (RouteMatch filterMatch : matchSet) {
Object filterTarget = filterMatch.getTarget();
if (filterTarget instanceof FilterImpl) {
Request request = RequestResponseFactory.create(filterMatch, context.httpRequest());
FilterImpl filter = (FilterImpl) filterTarget;
context.requestWrapper().setDelegate(request);
context.responseWrapper().setDelegate(context.response());
filter.handle(context.requestWrapper(), context.responseWrapper());
String bodyAfterFilter = context.response().body();
if (bodyAfterFilter != null) {
content = bodyAfterFilter;
}
}
}
context.body().set(content);
}
use of spark.routematch.RouteMatch in project spark by perwendel.
the class Routes method find.
/**
* finds target for a requested route
*
* @param httpMethod the http method
* @param path the path
* @param acceptType the accept type
* @return the target
*/
public RouteMatch find(HttpMethod httpMethod, String path, String acceptType) {
List<RouteEntry> routeEntries = this.findTargetsForRequestedRoute(httpMethod, path);
RouteEntry entry = findTargetWithGivenAcceptType(routeEntries, acceptType);
return entry != null ? new RouteMatch(entry.target, entry.path, path, acceptType) : null;
}
use of spark.routematch.RouteMatch in project spark by perwendel.
the class Routes method findMultiple.
/**
* Finds multiple targets for a requested route.
*
* @param httpMethod the http method
* @param path the route path
* @param acceptType the accept type
* @return the targets
*/
public List<RouteMatch> findMultiple(HttpMethod httpMethod, String path, String acceptType) {
List<RouteMatch> matchSet = new ArrayList<>();
List<RouteEntry> routeEntries = findTargetsForRequestedRoute(httpMethod, path);
for (RouteEntry routeEntry : routeEntries) {
if (acceptType != null) {
String bestMatch = MimeParse.bestMatch(Arrays.asList(routeEntry.acceptedType), acceptType);
if (routeWithGivenAcceptType(bestMatch)) {
matchSet.add(new RouteMatch(routeEntry.target, routeEntry.path, path, acceptType));
}
} else {
matchSet.add(new RouteMatch(routeEntry.target, routeEntry.path, path, acceptType));
}
}
return matchSet;
}
Aggregations