Search in sources :

Example 1 with CachingResourceResolver

use of com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver in project Portofino by ManyDesigns.

the class TestJacksonResourceResolver method person.

@Test
public void person() throws Exception {
    FileObject root = getRoot();
    ResourceResolver resourceResolver = new CachingResourceResolver(new JacksonResourceResolver());
    Map personMap = resourceResolver.resolve(root.resolveFile("test/person.json"), Map.class);
    assertEquals("person", personMap.get("title"));
    Entity personEntity = resourceResolver.resolve(root.resolveFile("test/person.json"), Entity.class);
    assertEquals("person", personEntity.title);
    personMap = resourceResolver.resolve(root.resolveFile("test"), "person", Map.class);
    assertEquals("person", personMap.get("title"));
    personEntity = resourceResolver.resolve(root.resolveFile("test"), "person", Entity.class);
    assertEquals("person", personEntity.title);
}
Also used : CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) JacksonResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.JacksonResourceResolver) CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) JacksonResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.JacksonResourceResolver) FileObject(org.apache.commons.vfs2.FileObject) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 2 with CachingResourceResolver

use of com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver in project Portofino by ManyDesigns.

the class DispatcherInitializer method addResourceResolver.

protected void addResourceResolver(ResourceResolvers resourceResolver, String className, CodeBase codeBase, boolean caching) {
    try {
        Class<?> resClass = Class.forName(className);
        ResourceResolver resolver;
        try {
            Constructor<?> resClassConstructor = resClass.getConstructor(CodeBase.class);
            resolver = (ResourceResolver) resClassConstructor.newInstance(codeBase);
        } catch (Exception e) {
            logger.debug("Constructor from CodeBase not available", e);
            resolver = (ResourceResolver) resClass.getConstructor().newInstance();
        }
        if (caching) {
            resolver = new CachingResourceResolver(resolver);
        }
        resourceResolver.resourceResolvers.add(resolver);
    } catch (Exception e) {
        logger.debug(className + " not available", e);
    }
}
Also used : CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) JavaResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.JavaResourceResolver) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException)

Example 3 with CachingResourceResolver

use of com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver in project Portofino by ManyDesigns.

the class GrizzlyMain method main.

public static void main(String[] args) throws Exception {
    File currentDirectory = new File("");
    FileSystemManager manager = VFS.getManager();
    if (manager instanceof DefaultFileSystemManager) {
        ((DefaultFileSystemManager) manager).setBaseFile(currentDirectory);
    }
    String rootPath = currentDirectory.getAbsolutePath();
    if (args.length > 0) {
        rootPath = args[0];
    }
    FileObject codeBaseRoot;
    try {
        codeBaseRoot = VFS.getManager().resolveFile("res:");
    } catch (Exception e) {
        codeBaseRoot = VFS.getManager().resolveFile("res:com/manydesigns/portofino/dispatcher/main/GrizzlyMain.class").getParent().getParent().getParent().getParent().getParent().getParent();
    }
    CodeBase codeBase = new JavaCodeBase(codeBaseRoot);
    codeBase = new GroovyCodeBase(codeBaseRoot, codeBase);
    ResourceResolvers resourceResolver = new ResourceResolvers();
    resourceResolver.resourceResolvers.add(new JavaResourceResolver(codeBase));
    resourceResolver.resourceResolvers.add(new GroovyResourceResolver(codeBase));
    FileObject root = manager.resolveFile(rootPath);
    logger.info("Codebase rootFactory: " + codeBaseRoot.getURL());
    logger.info("Root path: " + root.getURL());
    DocumentedApiRoot.setRootFactory(() -> Root.get(root, resourceResolver));
    String host = System.getProperty("portofino.web.host", "0.0.0.0");
    String port = System.getProperty("portofino.web.port", "8090");
    ResourceConfig config = new ResourceConfig(ApplicationRoot.class, OpenApiResource.class);
    config.property(ServletProperties.FILTER_FORWARD_ON_404, true);
    try {
        config.register(Class.forName("org.glassfish.jersey.jackson.JacksonFeature"));
        ResourceResolver jacksonRR = (ResourceResolver) Class.forName("com.manydesigns.portofino.dispatcher.resolvers.JacksonResourceResolver").newInstance();
        resourceResolver.resourceResolvers.add(new CachingResourceResolver(jacksonRR));
    } catch (ClassNotFoundException e) {
    // Jackson not available
    }
    try {
        config.register(Class.forName("com.manydesigns.portofino.dispatcher.security.ShiroResourceFilter"));
    } catch (ClassNotFoundException e) {
    // Shiro not available
    }
    /* TODO
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost(host + ":" + port);
        beanConfig.setBasePath("/");
        beanConfig.setResourcePackage(ApplicationRoot.class.getPackage().getName());
        beanConfig.setScan(true);*/
    URI uri = URI.create("http://" + host + ":" + port + "/");
    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(uri, config);
    // https://scalajamsession.wordpress.com/2015/06/27/how-to-serve-static-files-with-jerseygrizzly-setup-and-control-access-to-static-content/
    ClassLoader loader = GrizzlyMain.class.getClassLoader();
    CLStaticHttpHandler webjarsHandler = new CLStaticHttpHandler(loader, "META-INF/resources/webjars/");
    httpServer.getServerConfiguration().addHttpHandler(webjarsHandler, "/webjars/");
    httpServer.start();
}
Also used : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) ResourceResolvers(com.manydesigns.portofino.dispatcher.resolvers.ResourceResolvers) JavaResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.JavaResourceResolver) CodeBase(com.manydesigns.portofino.code.CodeBase) JavaCodeBase(com.manydesigns.portofino.code.JavaCodeBase) GroovyCodeBase(com.manydesigns.portofino.code.GroovyCodeBase) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) URI(java.net.URI) CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) GroovyResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.GroovyResourceResolver) GroovyCodeBase(com.manydesigns.portofino.code.GroovyCodeBase) ResourceResolver(com.manydesigns.portofino.dispatcher.ResourceResolver) GroovyResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.GroovyResourceResolver) CachingResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver) JavaResourceResolver(com.manydesigns.portofino.dispatcher.resolvers.JavaResourceResolver) HttpServer(org.glassfish.grizzly.http.server.HttpServer) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileObject(org.apache.commons.vfs2.FileObject) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) File(java.io.File) JavaCodeBase(com.manydesigns.portofino.code.JavaCodeBase)

Aggregations

CachingResourceResolver (com.manydesigns.portofino.dispatcher.resolvers.CachingResourceResolver)3 JavaResourceResolver (com.manydesigns.portofino.dispatcher.resolvers.JavaResourceResolver)2 FileObject (org.apache.commons.vfs2.FileObject)2 CodeBase (com.manydesigns.portofino.code.CodeBase)1 GroovyCodeBase (com.manydesigns.portofino.code.GroovyCodeBase)1 JavaCodeBase (com.manydesigns.portofino.code.JavaCodeBase)1 ResourceResolver (com.manydesigns.portofino.dispatcher.ResourceResolver)1 GroovyResourceResolver (com.manydesigns.portofino.dispatcher.resolvers.GroovyResourceResolver)1 JacksonResourceResolver (com.manydesigns.portofino.dispatcher.resolvers.JacksonResourceResolver)1 ResourceResolvers (com.manydesigns.portofino.dispatcher.resolvers.ResourceResolvers)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Map (java.util.Map)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)1 CLStaticHttpHandler (org.glassfish.grizzly.http.server.CLStaticHttpHandler)1 HttpServer (org.glassfish.grizzly.http.server.HttpServer)1