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