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");
}
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);
}
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());
}
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) {
}
}
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;
}
Aggregations