use of org.eclipse.jetty.server.handler.ResourceHandler in project elastic-job by dangdangdotcom.
the class RestfulServer method buildResourceHandler.
private ResourceHandler buildResourceHandler(final String resourcePath) throws Exception {
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setBaseResource(Resource.newClassPathResource(resourcePath));
return resourceHandler;
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project jetty.project by eclipse.
the class FileServer method main.
public static void main(String[] args) throws Exception {
// Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
// then a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
// a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
ResourceHandler resource_handler = new ResourceHandler();
// Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
// In this example it is the current directory but it can be configured to anything that the jvm has access to.
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[] { "index.html" });
resource_handler.setResourceBase(".");
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
// Start things up! By using the server.join() the server thread will join with the current thread.
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();
server.join();
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project jetty.project by eclipse.
the class DispatcherTest method init.
@Before
public void init() throws Exception {
_server = new Server();
_connector = new LocalConnector(_server);
_connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
_connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
_contextCollection = new ContextHandlerCollection();
_contextHandler = new ServletContextHandler();
_contextHandler.setContextPath("/context");
_contextCollection.addHandler(_contextHandler);
_resourceHandler = new ResourceHandler();
_resourceHandler.setResourceBase(MavenTestingUtils.getTestResourceDir("dispatchResourceTest").getAbsolutePath());
_resourceHandler.setPathInfoOnly(true);
ContextHandler resourceContextHandler = new ContextHandler("/resource");
resourceContextHandler.setHandler(_resourceHandler);
_contextCollection.addHandler(resourceContextHandler);
_server.setHandler(_contextCollection);
_server.addConnector(_connector);
_server.start();
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project buck by facebook.
the class WebServerTest method testCreateHandlersCoversExpectedContextPaths.
@Test
public void testCreateHandlersCoversExpectedContextPaths() {
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
WebServer webServer = new WebServer(/* port */
9999, projectFilesystem, "/static/", ObjectMappers.newDefaultInstance());
ImmutableList<ContextHandler> handlers = webServer.createHandlers();
final Map<String, ContextHandler> contextPathToHandler = Maps.newHashMap();
for (ContextHandler handler : handlers) {
contextPathToHandler.put(handler.getContextPath(), handler);
}
Function<String, TemplateHandlerDelegate> getDelegate = contextPath -> ((TemplateHandler) contextPathToHandler.get(contextPath).getHandler()).getDelegate();
assertTrue(getDelegate.apply("/") instanceof IndexHandlerDelegate);
assertTrue(contextPathToHandler.get("/static").getHandler() instanceof ResourceHandler);
assertTrue(getDelegate.apply("/trace") instanceof TraceHandlerDelegate);
assertTrue(getDelegate.apply("/traces") instanceof TracesHandlerDelegate);
assertTrue(contextPathToHandler.get("/tracedata").getHandler() instanceof TraceDataHandler);
}
use of org.eclipse.jetty.server.handler.ResourceHandler 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;
}
}
Aggregations