use of org.glassfish.jersey.uri.PathPattern in project jersey by jersey.
the class NaiveResourceMappingContext method buildMappings.
private void buildMappings() {
if (mappings != null) {
return;
}
mappings = new HashMap<>();
erc.getResourceModel().accept(new ResourceModelVisitor() {
Deque<PathPattern> stack = new LinkedList<>();
private void processComponents(final ResourceModelComponent component) {
final List<? extends ResourceModelComponent> components = component.getComponents();
if (components != null) {
for (final ResourceModelComponent rc : components) {
rc.accept(this);
}
}
}
@Override
public void visitInvocable(final Invocable invocable) {
processComponents(invocable);
}
@Override
public void visitRuntimeResource(final RuntimeResource runtimeResource) {
processComponents(runtimeResource);
}
@Override
public void visitResourceModel(final ResourceModel resourceModel) {
processComponents(resourceModel);
}
@Override
public void visitResourceHandlerConstructor(final HandlerConstructor handlerConstructor) {
processComponents(handlerConstructor);
}
@Override
public void visitMethodHandler(final MethodHandler methodHandler) {
processComponents(methodHandler);
}
@Override
public void visitChildResource(final Resource resource) {
visitResourceIntl(resource, false);
}
@Override
public void visitResource(final Resource resource) {
visitResourceIntl(resource, true);
}
private void visitResourceIntl(final Resource resource, final boolean isRoot) {
try {
stack.addLast(resource.getPathPattern());
processComponents(resource);
if (isRoot) {
Class likelyToBeRoot = null;
for (final Class next : resource.getHandlerClasses()) {
if (!(Inflector.class.isAssignableFrom(next))) {
likelyToBeRoot = next;
}
}
if (likelyToBeRoot != null) {
mappings.put(likelyToBeRoot, getMapping(getTemplate()));
}
}
} finally {
stack.removeLast();
}
}
@Override
public void visitResourceMethod(final ResourceMethod resourceMethod) {
if (resourceMethod.isExtended()) {
return;
}
if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) {
if (resourceMethod.getInvocable() != null) {
final Invocable i = resourceMethod.getInvocable();
final Type type = i.getResponseType();
final StringBuilder template = getTemplate();
mappings.put((Class) type, getMapping(template));
// Process sub resources ?
Resource.Builder builder = Resource.builder(i.getRawResponseType());
if (builder == null) {
// for example in the case the return type of the sub resource locator is Object
builder = Resource.builder().path(resourceMethod.getParent().getPath());
}
final Resource subResource = builder.build();
visitChildResource(subResource);
}
}
processComponents(resourceMethod);
}
private StringBuilder getTemplate() {
final StringBuilder template = new StringBuilder();
for (final PathPattern pp : stack) {
final String ppTemplate = pp.getTemplate().getTemplate();
final int tlength = template.length();
if (tlength > 0) {
if (template.charAt(tlength - 1) == '/') {
if (ppTemplate.startsWith("/")) {
template.append(ppTemplate, 1, ppTemplate.length());
} else {
template.append(ppTemplate);
}
} else {
if (ppTemplate.startsWith("/")) {
template.append(ppTemplate);
} else {
template.append("/");
template.append(ppTemplate);
}
}
} else {
template.append(ppTemplate);
}
}
return template;
}
});
}
use of org.glassfish.jersey.uri.PathPattern in project jersey by jersey.
the class ChildResourceTest method process.
private void process(String str) {
PathPattern pattern = new PathPattern(str);
System.out.println("template: " + pattern.getTemplate().toString());
System.out.println("pattern: " + pattern.toString());
}
use of org.glassfish.jersey.uri.PathPattern in project jersey by jersey.
the class PathMatchingRouter method apply.
@Override
public Router.Continuation apply(final RequestProcessingContext context) {
final RoutingContext rc = context.routingContext();
// Peek at matching information to obtain path to match
String path = rc.getFinalMatchingGroup();
final TracingLogger tracingLogger = TracingLogger.getInstance(context.request());
tracingLogger.log(ServerTraceEvent.MATCH_PATH_FIND, path);
Router.Continuation result = null;
final Iterator<Route> iterator = acceptedRoutes.iterator();
while (iterator.hasNext()) {
final Route acceptedRoute = iterator.next();
final PathPattern routePattern = acceptedRoute.routingPattern();
final MatchResult m = routePattern.match(path);
if (m != null) {
// Push match result information and rest of path to match
rc.pushMatchResult(m);
result = Router.Continuation.of(context, acceptedRoute.next());
//tracing
tracingLogger.log(ServerTraceEvent.MATCH_PATH_SELECTED, routePattern.getRegex());
break;
} else {
tracingLogger.log(ServerTraceEvent.MATCH_PATH_NOT_MATCHED, routePattern.getRegex());
}
}
if (tracingLogger.isLogEnabled(ServerTraceEvent.MATCH_PATH_SKIPPED)) {
while (iterator.hasNext()) {
tracingLogger.log(ServerTraceEvent.MATCH_PATH_SKIPPED, iterator.next().routingPattern().getRegex());
}
}
if (result == null) {
// No match
return Router.Continuation.of(context);
}
return result;
}
use of org.glassfish.jersey.uri.PathPattern in project jersey by jersey.
the class RuntimeModelBuilder method buildModel.
/**
* Build a runtime model of routers based on the {@code resourceModel}.
*
* @param resourceModel Resource model from which the runtime model should be built.
* @param subResourceMode True if the {@code resourceModel} is a sub resource model returned from sub resource locator.
* @return Root router of the router structure representing the resource model.
*/
public Router buildModel(final RuntimeResourceModel resourceModel, final boolean subResourceMode) {
final List<RuntimeResource> runtimeResources = resourceModel.getRuntimeResources();
final PushMatchedUriRouter uriPushingRouter = new PushMatchedUriRouter();
PathMatchingRouterBuilder currentRouterBuilder = null;
// route methods
for (final RuntimeResource resource : runtimeResources) {
final PushMatchedRuntimeResourceRouter resourcePushingRouter = new PushMatchedRuntimeResourceRouter(resource);
// resource methods
if (!resource.getResourceMethods().isEmpty()) {
final List<MethodRouting> methodRoutings = createResourceMethodRouters(resource, subResourceMode);
final Router methodSelectingRouter = new MethodSelectingRouter(workers, methodRoutings);
if (subResourceMode) {
currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.END_OF_PATH_PATTERN).to(resourcePushingRouter).to(methodSelectingRouter);
} else {
currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.asClosed(resource.getPathPattern())).to(uriPushingRouter).to(resourcePushingRouter).to(methodSelectingRouter);
}
}
PathMatchingRouterBuilder srRoutedBuilder = null;
if (!resource.getChildRuntimeResources().isEmpty()) {
for (final RuntimeResource childResource : resource.getChildRuntimeResources()) {
final PathPattern childOpenPattern = childResource.getPathPattern();
final PathPattern childClosedPattern = PathPattern.asClosed(childOpenPattern);
final PushMatchedRuntimeResourceRouter childResourcePushingRouter = new PushMatchedRuntimeResourceRouter(childResource);
// sub resource methods
if (!childResource.getResourceMethods().isEmpty()) {
final List<MethodRouting> childMethodRoutings = createResourceMethodRouters(childResource, subResourceMode);
srRoutedBuilder = startNextRoute(srRoutedBuilder, childClosedPattern).to(uriPushingRouter).to(childResourcePushingRouter).to(new MethodSelectingRouter(workers, childMethodRoutings));
}
// sub resource locator
if (childResource.getResourceLocator() != null) {
final PushMatchedTemplateRouter locTemplateRouter = getTemplateRouterForChildLocator(subResourceMode, childResource);
srRoutedBuilder = startNextRoute(srRoutedBuilder, childOpenPattern).to(uriPushingRouter).to(locTemplateRouter).to(childResourcePushingRouter).to(new PushMatchedMethodRouter(childResource.getResourceLocator())).to(createMethodRouter(childResource.getResourceLocator()));
}
}
}
// resource locator with empty path
if (resource.getResourceLocator() != null) {
final PushMatchedTemplateRouter resourceTemplateRouter = getTemplateRouter(subResourceMode, getLocatorResource(resource).getPathPattern().getTemplate(), PathPattern.OPEN_ROOT_PATH_PATTERN.getTemplate());
srRoutedBuilder = startNextRoute(srRoutedBuilder, PathPattern.OPEN_ROOT_PATH_PATTERN).to(uriPushingRouter).to(resourceTemplateRouter).to(new PushMatchedMethodRouter(resource.getResourceLocator())).to(createMethodRouter(resource.getResourceLocator()));
}
if (srRoutedBuilder != null) {
final Router methodRouter = srRoutedBuilder.build();
if (subResourceMode) {
currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.OPEN_ROOT_PATH_PATTERN).to(resourcePushingRouter).to(methodRouter);
} else {
currentRouterBuilder = startNextRoute(currentRouterBuilder, resource.getPathPattern()).to(uriPushingRouter).to(resourcePushingRouter).to(methodRouter);
}
}
}
return createRootRouter(currentRouterBuilder, subResourceMode);
}
Aggregations