Search in sources :

Example 1 with MatchInfo

use of org.webpieces.router.impl.routers.MatchInfo in project webpieces by deanhiller.

the class RouteBuilderImpl method createStaticRouter.

private void createStaticRouter(UrlPath urlPath, Port exposedPort, HttpMethod httpMethod, Charset urlEncoding, VirtualFile file, boolean isOnClassPath, boolean isFile) {
    String urlSubPath = urlPath.getSubPath();
    List<String> pathParamNames = new ArrayList<>();
    Pattern patternToMatch;
    if (file.isDirectory() && isFile) {
        throw new IllegalArgumentException("directory=" + file.getCanonicalPath() + " is not a file and must be for adding a static file route");
    } else if (!file.isDirectory() && !isFile) {
        throw new IllegalArgumentException("file=" + file.getCanonicalPath() + " is not a directory and must be for adding a static directory route");
    }
    if (file.isDirectory()) {
        // Customer can supply the resource capture group OR we provide it if it is a simple directory match
        if (!urlSubPath.contains("<resource>")) {
            patternToMatch = Pattern.compile("^" + urlSubPath + "(?<resource>.*)$");
        } else {
            patternToMatch = Pattern.compile("^" + urlSubPath + "$");
        }
        pathParamNames.add("resource");
    } else {
        // it is a file
        patternToMatch = Pattern.compile("^" + urlSubPath + "$");
    }
    MatchInfo matchInfo = new MatchInfo(urlPath, exposedPort, httpMethod, urlEncoding, patternToMatch, pathParamNames);
    String relativePath = urlSubPath.substring(1);
    File targetCacheLocation = FileFactory.newFile(holder.getCachedCompressedDirectory(), relativePath);
    FStaticRouter router = new FStaticRouter(holder.getRouteInvoker2(), matchInfo, file, isOnClassPath, targetCacheLocation, isFile);
    staticRouters.add(router);
    log.info("scope:'" + routerInfo + "' added route=" + matchInfo + " fileSystemPath=" + file);
}
Also used : Pattern(java.util.regex.Pattern) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) FStaticRouter(org.webpieces.router.impl.routers.FStaticRouter) ArrayList(java.util.ArrayList) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile)

Example 2 with MatchInfo

use of org.webpieces.router.impl.routers.MatchInfo in project webpieces by deanhiller.

the class SharedMatchUtil method buildRoutes.

protected List<AbstractRouter> buildRoutes(List<FilterInfo<?>> routeFilters) {
    List<AbstractRouter> routers = new ArrayList<>();
    for (RouterAndInfo routerAndInfo : newDynamicRoutes) {
        AbstractDynamicRouter router = routerAndInfo.getRouter();
        MatchInfo matchInfo = router.getMatchInfo();
        String path = matchInfo.getFullPath();
        Port port = matchInfo.getExposedPorts();
        ResolvedMethod methodMeta = routerAndInfo.getMetaAndController().getMethodMeta();
        StreamService streamSvc;
        if (routerAndInfo.getRouteType() == RouteType.STREAMING) {
            streamSvc = new StreamProxy(holder.getFutureUtil(), holder.getServiceInvoker());
        } else {
            // Wire in request/response filters at this point
            List<FilterInfo<?>> filters = findMatchingFilters(methodMeta, path, port, routeFilters);
            FilterCreationMeta chainInfo = new FilterCreationMeta(resettingLogic.getInjector(), filters, routerAndInfo.getSvcProxy());
            Service<MethodMeta, Action> service = holder.getFinder().loadFilters(chainInfo);
            Processor processor;
            if (routerAndInfo.getRouteType() == RouteType.CONTENT) {
                processor = holder.getResponseProcessorContent();
            } else if (routerAndInfo.getRouteType() == RouteType.HTML) {
                processor = holder.getResponseProcessorHtml();
            } else {
                throw new IllegalStateException("RouteType not supported here=" + routerAndInfo.getRouteType());
            }
            String i18nBundleName = routerAndInfo.getRouteInfo().getRouteModuleInfo().getI18nBundleName();
            Endpoint svc = new Endpoint(service);
            streamSvc = new RequestResponseStream(svc, i18nBundleName, processor, holder.getBodyParsers(), holder.getServiceInvoker());
        }
        // Add streaming filters at this point here...
        router.setDynamicInfo(streamSvc);
        routers.add(router);
    }
    return routers;
}
Also used : StreamService(org.webpieces.router.api.streams.StreamService) Action(org.webpieces.router.api.controller.actions.Action) Processor(org.webpieces.router.impl.routeinvoker.Processor) Port(org.webpieces.router.api.routes.Port) ArrayList(java.util.ArrayList) MethodMeta(org.webpieces.router.api.routes.MethodMeta) AbstractDynamicRouter(org.webpieces.router.impl.routers.AbstractDynamicRouter) ResolvedMethod(org.webpieces.router.impl.loader.ResolvedMethod) Endpoint(org.webpieces.router.impl.routers.Endpoint) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) AbstractRouter(org.webpieces.router.impl.routers.AbstractRouter)

Example 3 with MatchInfo

use of org.webpieces.router.impl.routers.MatchInfo in project webpieces by deanhiller.

the class ScopedRouteBuilderImpl method createMatchInfo.

private MatchInfo createMatchInfo(UrlPath urlPath, Port exposedPort, HttpMethod httpMethod, Charset urlEncoding) {
    RegExResult result = RegExUtil.parsePath(urlPath.getSubPath());
    Pattern patternToMatch = Pattern.compile(result.regExToMatch);
    List<String> pathParamNames = result.argNames;
    return new MatchInfo(urlPath, exposedPort, httpMethod, urlEncoding, patternToMatch, pathParamNames);
}
Also used : Pattern(java.util.regex.Pattern) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) RegExResult(org.webpieces.util.urlparse.RegExResult)

Example 4 with MatchInfo

use of org.webpieces.router.impl.routers.MatchInfo in project webpieces by deanhiller.

the class ScopedRouteBuilderImpl method addRoute.

@Override
public void addRoute(Port port, HttpMethod method, String path, String controllerMethod, RouteId routeId, boolean checkToken) {
    if (!controllerMethod.contains("."))
        throw new IllegalArgumentException("controllerMethod='" + controllerMethod + " does not contain a '.' which is required separating class name and method");
    UrlPath p = new UrlPath(routerInfo, path);
    boolean isPostOnly = method == HttpMethod.POST;
    RouteModuleInfo moduleInfo = CurrentRoutes.get();
    RouteInfo routeInfo = new RouteInfo(moduleInfo, controllerMethod);
    // MUST DO loadControllerIntoMetat HERE so stack trace has customer's line in it so he knows EXACTLY what
    // he did wrong when reading the exception!!
    MethodMetaAndController metaAndController = holder.getFinder().loadHtmlController(resettingLogic.getInjector(), routeInfo, isPostOnly);
    MatchInfo matchInfo = createMatchInfo(p, port, method, holder.getUrlEncoding());
    LoadedController loadedController = metaAndController.getLoadedController();
    FHtmlRouter router = new FHtmlRouter(holder.getRouteInvoker2(), loadedController, moduleInfo.getI18nBundleName(), matchInfo, checkToken);
    SvcProxyForHtml svc = new SvcProxyForHtml(holder.getSvcProxyLogic(), futureUtil);
    RouterAndInfo routerAndInfo = new RouterAndInfo(router, routeInfo, metaAndController, svc);
    newDynamicRoutes.add(routerAndInfo);
    resettingLogic.getReverseRoutes().addRoute(routeId, router);
    log.info("scope:'" + routerInfo + "' added route=" + matchInfo + " method=" + routeInfo.getControllerMethodString());
}
Also used : SvcProxyForHtml(org.webpieces.router.impl.services.SvcProxyForHtml) LoadedController(org.webpieces.router.impl.loader.LoadedController) UrlPath(org.webpieces.router.impl.UrlPath) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) RouteModuleInfo(org.webpieces.router.impl.model.RouteModuleInfo) FHtmlRouter(org.webpieces.router.impl.routers.FHtmlRouter) MethodMetaAndController(org.webpieces.router.impl.loader.MethodMetaAndController)

Example 5 with MatchInfo

use of org.webpieces.router.impl.routers.MatchInfo in project webpieces by deanhiller.

the class ScopedRouteBuilderImpl method addContentRoute.

@Override
public void addContentRoute(Port port, HttpMethod method, String path, String controllerMethod, RouteId routeId) {
    UrlPath p = new UrlPath(routerInfo, path);
    RouteModuleInfo moduleInfo = CurrentRoutes.get();
    RouteInfo routeInfo = new RouteInfo(moduleInfo, controllerMethod);
    // MUST DO loadControllerIntoMeta HERE so stack trace has customer's line in it so he knows EXACTLY what
    // he did wrong when reading the exception!!
    BinderAndLoader container = holder.getFinder().loadContentController(resettingLogic.getInjector(), routeInfo);
    MatchInfo matchInfo = createMatchInfo(p, port, method, holder.getUrlEncoding());
    LoadedController loadedController = container.getMetaAndController().getLoadedController();
    FContentRouter router = new FContentRouter(holder.getRouteInvoker2(), loadedController, moduleInfo, matchInfo, container.getBinder());
    SvcProxyForContent svc = new SvcProxyForContent(holder.getSvcProxyLogic(), futureUtil);
    RouterAndInfo routerAndInfo = new RouterAndInfo(router, routeInfo, container.getMetaAndController(), svc);
    newDynamicRoutes.add(routerAndInfo);
    if (// if there is a routeId, then add the reverse mapping
    routeId != null)
        resettingLogic.getReverseRoutes().addRoute(routeId, router);
    log.info("scope:'" + routerInfo + "' added content route=" + matchInfo + " method=" + routeInfo.getControllerMethodString());
}
Also used : BinderAndLoader(org.webpieces.router.impl.loader.BinderAndLoader) LoadedController(org.webpieces.router.impl.loader.LoadedController) UrlPath(org.webpieces.router.impl.UrlPath) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) SvcProxyForContent(org.webpieces.router.impl.services.SvcProxyForContent) FContentRouter(org.webpieces.router.impl.routers.FContentRouter) RouteModuleInfo(org.webpieces.router.impl.model.RouteModuleInfo)

Aggregations

MatchInfo (org.webpieces.router.impl.routers.MatchInfo)9 UrlPath (org.webpieces.router.impl.UrlPath)4 RouteModuleInfo (org.webpieces.router.impl.model.RouteModuleInfo)4 Pattern (java.util.regex.Pattern)3 LoadedController (org.webpieces.router.impl.loader.LoadedController)3 SvcProxyForContent (org.webpieces.router.impl.services.SvcProxyForContent)3 ArrayList (java.util.ArrayList)2 BinderAndLoader (org.webpieces.router.impl.loader.BinderAndLoader)2 MethodMetaAndController (org.webpieces.router.impl.loader.MethodMetaAndController)2 FContentRouter (org.webpieces.router.impl.routers.FContentRouter)2 RegExResult (org.webpieces.util.urlparse.RegExResult)2 File (java.io.File)1 Action (org.webpieces.router.api.controller.actions.Action)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1 MethodMeta (org.webpieces.router.api.routes.MethodMeta)1 Port (org.webpieces.router.api.routes.Port)1 StreamService (org.webpieces.router.api.streams.StreamService)1 ResolvedMethod (org.webpieces.router.impl.loader.ResolvedMethod)1 PortAndIsSecure (org.webpieces.router.impl.routeinvoker.PortAndIsSecure)1 Processor (org.webpieces.router.impl.routeinvoker.Processor)1