Search in sources :

Example 11 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project nifi by apache.

the class JettyServer method createDocsWebApp.

private ContextHandler createDocsWebApp(final String contextPath) {
    try {
        final ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        final File docsDir = getDocsDir("docs");
        final Resource docsResource = Resource.newResource(docsDir);
        // load the component documentation working directory
        final File componentDocsDirPath = props.getComponentDocumentationWorkingDirectory();
        final File workingDocsDirectory = getWorkingDocsDirectory(componentDocsDirPath);
        final Resource workingDocsResource = Resource.newResource(workingDocsDirectory);
        final File webApiDocsDir = getWebApiDocsDir();
        final Resource webApiDocsResource = Resource.newResource(webApiDocsDir);
        // create resources for both docs locations
        final ResourceCollection resources = new ResourceCollection(docsResource, workingDocsResource, webApiDocsResource);
        resourceHandler.setBaseResource(resources);
        // create the context handler
        final ContextHandler handler = new ContextHandler(contextPath);
        handler.setHandler(resourceHandler);
        logger.info("Loading documents web app with context path set to " + contextPath);
        return handler;
    } catch (Exception ex) {
        logger.error("Unhandled Exception in createDocsWebApp: " + ex.getMessage());
        startUpFailure(ex);
        // required by compiler, though never be executed.
        return null;
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Resource(org.eclipse.jetty.util.resource.Resource) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) JarFile(java.util.jar.JarFile) File(java.io.File) ServletException(javax.servlet.ServletException) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) SocketException(java.net.SocketException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 12 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jangaroo-tools by CoreMedia.

the class JooTestMojoBase method jettyRunTest.

protected Server jettyRunTest(boolean tryPortRange) throws MojoExecutionException {
    JettyWebAppContext handler;
    try {
        handler = new JettyWebAppContext();
        handler.setWebInfLib(findJars());
        handler.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
        List<Resource> baseResources = new ArrayList<Resource>();
        baseResources.add(toResource(new File(outputDirectory, "META-INF/resources")));
        baseResources.add(toResource(testOutputDirectory));
        for (org.apache.maven.model.Resource r : testResources) {
            File testResourceDirectory = new File(r.getDirectory());
            if (testResourceDirectory.exists()) {
                baseResources.add(toResource(testResourceDirectory));
            }
        }
        handler.setBaseResource(new ResourceCollection(baseResources.toArray(new Resource[baseResources.size()])));
        getLog().info("Using base resources " + baseResources);
        ServletHolder servletHolder = new ServletHolder("default", DefaultServlet.class);
        servletHolder.setInitParameter("cacheControl", "no-store, no-cache, must-revalidate, max-age=0");
        handler.addServlet(servletHolder, "/");
        getLog().info("Set servlet cache control to 'do not cache'.");
    } catch (Exception e) {
        throw wrap(e);
    }
    return startJetty(handler, tryPortRange);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JettyWebAppContext(org.mortbay.jetty.plugin.JettyWebAppContext) Resource(org.eclipse.jetty.util.resource.Resource) ArrayList(java.util.ArrayList) File(java.io.File) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 13 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project wcomponents by BorderTech.

the class TestServlet method createWebApp.

/**
 * Creates the Web app context to use in the LDE. The context will be registered with the given server.
 *
 * @param srv the Jetty server.
 * @return the newly created Web app context.
 * @throws Exception an exception
 */
protected WebAppContext createWebApp(final Server srv) throws Exception {
    String[] webdocs = getWebdocsDir();
    String[] themeWebdocs = getThemeWebdocs();
    String[] resourceDirs = getResourceDir();
    if (webdocs != null) {
        for (int i = 0; i < webdocs.length; i++) {
            union.add(Resource.newResource(webdocs[i]));
        }
    }
    if (themeWebdocs != null) {
        for (int i = 0; i < themeWebdocs.length; i++) {
            union.add(Resource.newResource(themeWebdocs[i]));
        }
    }
    if (resourceDirs != null) {
        for (int i = 0; i < resourceDirs.length; i++) {
            union.add(Resource.newResource(resourceDirs[i]));
        }
    }
    HandlerCollection handlers = new HandlerCollection();
    WebAppContext webapp = null;
    // If there is no external web.xml override, register the default servlets
    if (webdocs == null) {
        webapp = new WebAppContext();
        webapp.setContextPath("/");
        registerServlets(webapp);
    } else {
        webapp = new WebAppContext(webdocs[0], "/");
    }
    // Must have at least one resource
    if (union.isEmpty()) {
        webapp.setResourceBase(".");
    } else {
        webapp.setBaseResource(new ResourceCollection(union.toArray(new Resource[union.size()])));
    }
    webapp.addServlet(new ServletHolder(this), "/app/*");
    // file using the theme servlet.
    if (themeWebdocs == null) {
        WebAppContext themeWebapp = new WebAppContext();
        themeWebapp.setContextPath("/theme");
        themeWebapp.addServlet("com.github.bordertech.wcomponents.servlet.ThemeServlet", "/*");
        themeWebapp.setResourceBase(".");
        handlers.addHandler(themeWebapp);
    } else {
        WebAppContext themeWebapp = new WebAppContext();
        themeWebapp.setContextPath("/theme");
        themeWebapp.setResourceBase(themeWebdocs[0]);
        handlers.addHandler(themeWebapp);
    }
    // Initialise security
    String realmFile = ConfigurationProperties.getLdeServerJettyRealmFile();
    if (realmFile != null) {
        HashLoginService loginService = new HashLoginService("LdeRunner", realmFile);
        webapp.getSecurityHandler().setLoginService(loginService);
    }
    handlers.addHandler(webapp);
    srv.setHandler(handlers);
    return webapp;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 14 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project nifi-registry by apache.

the class JettyServer method createDocsWebApp.

private ContextHandler createDocsWebApp(final String contextPath) throws IOException {
    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(false);
    // load the docs directory
    final File docsDir = Paths.get("docs").toRealPath().toFile();
    final Resource docsResource = Resource.newResource(docsDir);
    // load the rest documentation
    final File webApiDocsDir = new File(webApiContext.getTempDirectory(), "webapp/docs");
    if (!webApiDocsDir.exists()) {
        final boolean made = webApiDocsDir.mkdirs();
        if (!made) {
            throw new RuntimeException(webApiDocsDir.getAbsolutePath() + " could not be created");
        }
    }
    final Resource webApiDocsResource = Resource.newResource(webApiDocsDir);
    // create resources for both docs locations
    final ResourceCollection resources = new ResourceCollection(docsResource, webApiDocsResource);
    resourceHandler.setBaseResource(resources);
    // create the context handler
    final ContextHandler handler = new ContextHandler(contextPath);
    handler.setHandler(resourceHandler);
    logger.info("Loading documents web app with context path set to " + contextPath);
    return handler;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Resource(org.eclipse.jetty.util.resource.Resource) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) File(java.io.File) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 15 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project gtfs-realtime-validator by CUTR-at-USF.

the class Main method main.

public static void main(String[] args) throws InterruptedException, ParseException {
    // Parse command line parameters
    Options options = setupCommandLineOptions();
    // Start validator in normal server mode
    int port = getPortFromArgs(options, args);
    HibernateUtil.configureSessionFactory();
    GTFSDB.initializeDB();
    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    /*
         * Create '/classes/webroot' directory if not exists in the same directory where jar is located.
         * '/jar-location-directory/classes/webroot' is where we store static GTFS feed validation json output.
         * 'classes/webroot' is created so that it will be in sync with or without build directories.
         */
    File jsonDirectory = new File(jsonFilePath);
    jsonDirectory.mkdirs();
    /*
         * As we cannot directly add static GTFS feed json output file to jar, we add an other web resource directory 'jsonFilePath'
         *  such that json output file can also be accessed from server.
         * Now there are two paths for web resources; 'BASE_RESOURCE' and 'jsonFilePath'.
         * 'jsonFilePath' as web resource directory is needed when we don't have any build folders. For example, see issue #181
         *  where we only have Travis generated jar file without any build directories.
         */
    ResourceCollection resources = new ResourceCollection(new String[] { BASE_RESOURCE, jsonFilePath });
    context.setBaseResource(resources);
    server.setHandler(context);
    context.addServlet(GetFeedJSON.class, "/getFeed");
    context.addServlet(DefaultServlet.class, "/");
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/api/*");
    jerseyServlet.setInitOrder(1);
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "org.glassfish.jersey.moxy.json.MoxyJsonFeature");
    jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "edu.usf.cutr.gtfsrtvalidator.api.resource");
    try {
        server.start();
        _log.info("Go to http://localhost:" + port + " in your browser");
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GetFile(edu.usf.cutr.gtfsrtvalidator.helper.GetFile) File(java.io.File) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Aggregations

ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)30 Resource (org.eclipse.jetty.util.resource.Resource)22 File (java.io.File)13 ArrayList (java.util.ArrayList)8 URL (java.net.URL)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 JarResource (org.eclipse.jetty.util.resource.JarResource)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 MimeTypes (org.eclipse.jetty.http.MimeTypes)3 Server (org.eclipse.jetty.server.Server)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2