Search in sources :

Example 91 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class Quickstart method main.

public static void main(String... args) throws Exception {
    if (args.length < 1)
        error("No WAR file or directory given");
    //war file or dir to start
    String war = args[0];
    //optional jetty context xml file to configure the webapp
    Resource contextXml = null;
    if (args.length > 1)
        contextXml = Resource.newResource(args[1]);
    Server server = new Server(8080);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration)

Example 92 with Resource

use of org.eclipse.jetty.util.resource.Resource in project BeyondUPnP by kevinshine.

the class VideoResourceServlet method getResource.

@Override
public Resource getResource(String pathInContext) {
    Resource resource = null;
    Log.i(VideoResourceServlet.class.getSimpleName(), "Path:" + pathInContext);
    try {
        String id = Utils.parseResourceId(pathInContext);
        Log.i(VideoResourceServlet.class.getSimpleName(), "Id:" + id);
        Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Long.parseLong(id));
        Cursor cursor = BeyondApplication.getApplication().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
        File file = new File(path);
        if (file.exists()) {
            resource = FileResource.newResource(file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resource;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) FileResource(org.eclipse.jetty.util.resource.FileResource) Cursor(android.database.Cursor) Uri(android.net.Uri) File(java.io.File)

Example 93 with Resource

use of org.eclipse.jetty.util.resource.Resource in project BeyondUPnP by kevinshine.

the class AudioResourceServlet method getResource.

@Override
public Resource getResource(String pathInContext) {
    Resource resource = null;
    Log.i(AudioResourceServlet.class.getSimpleName(), "Path:" + pathInContext);
    try {
        String id = Utils.parseResourceId(pathInContext);
        Log.i(AudioResourceServlet.class.getSimpleName(), "Id:" + id);
        Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.parseLong(id));
        Cursor cursor = BeyondApplication.getApplication().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
        File file = new File(path);
        if (file.exists()) {
            resource = FileResource.newResource(file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resource;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) FileResource(org.eclipse.jetty.util.resource.FileResource) Cursor(android.database.Cursor) Uri(android.net.Uri) File(java.io.File)

Example 94 with Resource

use of org.eclipse.jetty.util.resource.Resource in project ninja by ninjaframework.

the class NinjaJetty method buildServerOrApplyConfiguration.

private Server buildServerOrApplyConfiguration(String jettyConfiguration, Server server) throws Exception {
    // try local file first
    Resource jettyConfigurationFile = Resource.newResource(jettyConfiguration);
    if (jettyConfigurationFile == null || !jettyConfigurationFile.exists()) {
        // fallback to classpath
        jettyConfigurationFile = Resource.newClassPathResource(jettyConfiguration);
        if (jettyConfigurationFile == null || !jettyConfigurationFile.exists()) {
            throw new FileNotFoundException("Unable to find jetty configuration file either locally or on classpath '" + jettyConfiguration + "'");
        }
    }
    logger.info("Applying jetty configuration '{}'", jettyConfigurationFile);
    try (InputStream is = jettyConfigurationFile.getInputStream()) {
        XmlConfiguration configuration = new XmlConfiguration(is);
        // create or apply to existing
        if (server == null) {
            return (Server) configuration.configure();
        } else {
            return (Server) configuration.configure(server);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Resource(org.eclipse.jetty.util.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration)

Example 95 with Resource

use of org.eclipse.jetty.util.resource.Resource in project neo4j by neo4j.

the class Jetty9WebServer method loadStaticContent.

private void loadStaticContent(SessionManager sm, String mountPoint) {
    String contentLocation = staticContent.get(mountPoint);
    try {
        SessionHandler sessionHandler = new SessionHandler(sm);
        sessionHandler.setServer(getJetty());
        final WebAppContext staticContext = new WebAppContext();
        staticContext.setServer(getJetty());
        staticContext.setContextPath(mountPoint);
        staticContext.setSessionHandler(sessionHandler);
        staticContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        URL resourceLoc = getClass().getClassLoader().getResource(contentLocation);
        if (resourceLoc != null) {
            URL url = resourceLoc.toURI().toURL();
            final Resource resource = Resource.newResource(url);
            staticContext.setBaseResource(resource);
            addFiltersTo(staticContext);
            staticContext.addFilter(new FilterHolder(new NoCacheHtmlFilter()), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD));
            handlers.addHandler(staticContext);
        } else {
            log.warn("No static content available for Neo Server at %s, management console may not be available.", jettyAddress);
        }
    } catch (Exception e) {
        log.error("Unknown error loading static content", e);
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Resource(org.eclipse.jetty.util.resource.Resource) URL(java.net.URL) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) PortBindException(org.neo4j.helpers.PortBindException) BindException(java.net.BindException) IOException(java.io.IOException)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6