Search in sources :

Example 1 with FStaticRouter

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

the class ProdCompressionCacheSetup method setupCache.

public void setupCache(List<FStaticRouter> staticRoutes) {
    if (config.getCachedCompressedDirectory() == null) {
        log.info("NOT setting up compressed cached directory so performance will not be as good");
        return;
    }
    log.info("setting up compressed cache directories");
    for (FStaticRouter route : staticRoutes) {
        if (!route.isOnClassPath())
            createCache(route);
    }
    log.info("all cached directories setup");
}
Also used : FStaticRouter(org.webpieces.router.impl.routers.FStaticRouter)

Example 2 with FStaticRouter

use of org.webpieces.router.impl.routers.FStaticRouter 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 3 with FStaticRouter

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

the class TestCompressionCache method testStartServerTwiceNoChanges.

@Test
public void testStartServerTwiceNoChanges() throws IOException {
    File f = new File("src/test/resources/cacheTest1");
    File stagingDir = FileFactory.newBaseFile("output/staging");
    FileUtils.copyDirectory(f, stagingDir);
    List<FStaticRouter> routes = runBasicServerOnce(stagingDir);
    // if server is just restarted(no file changes), we should skip reading files...
    cache.setupCache(routes);
    Assert.assertEquals(0, proxy.getReadFiles().size());
    Assert.assertEquals(0, proxy.getCompressedFiles().size());
}
Also used : FStaticRouter(org.webpieces.router.impl.routers.FStaticRouter) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) Test(org.junit.Test)

Example 4 with FStaticRouter

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

the class TestCompressionCache method testModifyFileContentsButNotUrlPathFailure.

@Test
public void testModifyFileContentsButNotUrlPathFailure() throws IOException {
    File f = new File("src/test/resources/cacheTest1");
    File stagingDir = FileFactory.newBaseFile("output/staging");
    FileUtils.copyDirectory(f, stagingDir);
    List<FStaticRouter> routes = runBasicServerOnce(stagingDir);
    File f2 = new File("src/test/resources/cacheTest2");
    FileUtils.deleteDirectory(stagingDir);
    // do not preserve dates here...
    FileUtils.copyDirectory(f2, stagingDir, false);
    try {
        cache.setupCache(routes);
        Assert.fail("should have failed since file contents changed and url path didn't which would have broken web app customers");
    } catch (IllegalStateException e) {
    }
}
Also used : FStaticRouter(org.webpieces.router.impl.routers.FStaticRouter) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) Test(org.junit.Test)

Example 5 with FStaticRouter

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

the class TestCompressionCache method runBasicServerOnce.

private List<FStaticRouter> runBasicServerOnce(File stagingDir) {
    List<FStaticRouter> routes = new ArrayList<>();
    VirtualFile dir = VirtualFileFactory.newFile(stagingDir);
    routes.add(create(Port.BOTH, new UrlPath("", "/public/"), dir, false, cacheDir));
    cache.setupCache(routes);
    Assert.assertEquals(2, proxy.getReadFiles().size());
    Assert.assertEquals(2, proxy.getCompressedFiles().size());
    proxy.clear();
    return routes;
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) FStaticRouter(org.webpieces.router.impl.routers.FStaticRouter) UrlPath(org.webpieces.router.impl.UrlPath) ArrayList(java.util.ArrayList)

Aggregations

FStaticRouter (org.webpieces.router.impl.routers.FStaticRouter)7 VirtualFile (org.webpieces.util.file.VirtualFile)6 File (java.io.File)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 UrlPath (org.webpieces.router.impl.UrlPath)2 Pattern (java.util.regex.Pattern)1 MatchInfo (org.webpieces.router.impl.routers.MatchInfo)1