use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class WebSocketUpgradeFilterTest method data.
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> data() {
final WebSocketCreator infoCreator = (req, resp) -> new InfoSocket();
List<Object[]> cases = new ArrayList<>();
// Embedded WSUF.configureContext(), directly app-ws configuration
cases.add(new Object[] { "wsuf.configureContext/Direct configure", (ServerProvider) () -> {
Server server1 = new Server();
ServerConnector connector = new ServerConnector(server1);
connector.setPort(0);
server1.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server1.setHandler(context);
WebSocketUpgradeFilter wsuf = WebSocketUpgradeFilter.configureContext(context);
wsuf.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
wsuf.addMapping("/info/*", infoCreator);
server1.start();
return server1;
} });
// Embedded WSUF.configureContext(), apply app-ws configuration via attribute
cases.add(new Object[] { "wsuf.configureContext/Attribute based configure", (ServerProvider) () -> {
Server server12 = new Server();
ServerConnector connector = new ServerConnector(server12);
connector.setPort(0);
server12.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server12.setHandler(context);
WebSocketUpgradeFilter.configureContext(context);
NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration) context.getServletContext().getAttribute(NativeWebSocketConfiguration.class.getName());
assertThat("NativeWebSocketConfiguration", configuration, notNullValue());
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
configuration.addMapping("/info/*", infoCreator);
server12.start();
return server12;
} });
// Embedded WSUF, added as filter, apply app-ws configuration via attribute
cases.add(new Object[] { "wsuf/addFilter/Attribute based configure", (ServerProvider) () -> {
Server server13 = new Server();
ServerConnector connector = new ServerConnector(server13);
connector.setPort(0);
server13.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server13.setHandler(context);
context.addFilter(WebSocketUpgradeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
NativeWebSocketConfiguration configuration = new NativeWebSocketConfiguration(context.getServletContext());
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
configuration.addMapping("/info/*", infoCreator);
context.setAttribute(NativeWebSocketConfiguration.class.getName(), configuration);
server13.start();
return server13;
} });
// Embedded WSUF, added as filter, apply app-ws configuration via wsuf constructor
cases.add(new Object[] { "wsuf/addFilter/WSUF Constructor configure", new ServerProvider() {
@Override
public Server newServer() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
NativeWebSocketConfiguration configuration = new NativeWebSocketConfiguration(context.getServletContext());
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
configuration.addMapping("/info/*", infoCreator);
context.addBean(configuration, true);
FilterHolder wsufHolder = new FilterHolder(new WebSocketUpgradeFilter(configuration));
context.addFilter(wsufHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
server.start();
return server;
}
} });
// Embedded WSUF, added as filter, apply app-ws configuration via ServletContextListener
cases.add(new Object[] { "wsuf.configureContext/ServletContextListener configure", (ServerProvider) () -> {
Server server14 = new Server();
ServerConnector connector = new ServerConnector(server14);
connector.setPort(0);
server14.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server14.setHandler(context);
context.addFilter(WebSocketUpgradeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
context.addEventListener(new InfoContextListener());
server14.start();
return server14;
} });
// WSUF from web.xml, SCI active, apply app-ws configuration via ServletContextListener
cases.add(new Object[] { "wsuf/WebAppContext/web.xml/ServletContextListener", (ServerProvider) () -> {
File testDir = getNewTestDir();
WSServer server15 = new WSServer(testDir, "/");
server15.copyWebInf("wsuf-config-via-listener.xml");
server15.copyClass(InfoSocket.class);
server15.copyClass(InfoContextAttributeListener.class);
server15.start();
WebAppContext webapp = server15.createWebAppContext();
server15.deployWebapp(webapp);
return server15.getServer();
} });
// WSUF from web.xml, SCI active, apply app-ws configuration via ServletContextListener with WEB-INF/lib/jetty-http.jar
cases.add(new Object[] { "wsuf/WebAppContext/web.xml/ServletContextListener/jetty-http.jar", new ServerProvider() {
@Override
public Server newServer() throws Exception {
File testDir = getNewTestDir();
WSServer server = new WSServer(testDir, "/");
server.copyWebInf("wsuf-config-via-listener.xml");
server.copyClass(InfoSocket.class);
server.copyClass(InfoContextAttributeListener.class);
// Add a jetty-http.jar to ensure that the classloader constraints
// and the WebAppClassloader setup is sane and correct
// The odd version string is present to capture bad regex behavior in Jetty
server.copyLib(org.eclipse.jetty.http.pathmap.PathSpec.class, "jetty-http-9.99.999.jar");
server.start();
WebAppContext webapp = server.createWebAppContext();
server.deployWebapp(webapp);
return server.getServer();
}
} });
// WSUF from web.xml, SCI active, apply app-ws configuration via Servlet.init
cases.add(new Object[] { "wsuf/WebAppContext/web.xml/Servlet.init", (ServerProvider) () -> {
File testDir = getNewTestDir();
WSServer server16 = new WSServer(testDir, "/");
server16.copyWebInf("wsuf-config-via-servlet-init.xml");
server16.copyClass(InfoSocket.class);
server16.copyClass(InfoServlet.class);
server16.start();
WebAppContext webapp = server16.createWebAppContext();
server16.deployWebapp(webapp);
return server16.getServer();
} });
// xml based, wsuf, on alternate url-pattern and config attribute location
cases.add(new Object[] { "wsuf/WebAppContext/web.xml/ServletContextListener/alt-config", (ServerProvider) () -> {
File testDir = getNewTestDir();
WSServer server17 = new WSServer(testDir, "/");
server17.copyWebInf("wsuf-alt-config-via-listener.xml");
server17.copyClass(InfoSocket.class);
server17.copyClass(InfoContextAltAttributeListener.class);
server17.start();
WebAppContext webapp = server17.createWebAppContext();
server17.deployWebapp(webapp);
return server17.getServer();
} });
return cases;
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class JstlTest method startServer.
@BeforeClass
public static void startServer() throws Exception {
// Setup Server
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
// Setup WebAppContext
File testWebAppDir = MavenTestingUtils.getProjectDir("src/test/webapp");
// Prepare WebApp libs
File libDir = new File(testWebAppDir, "WEB-INF/lib");
FS.ensureDirExists(libDir);
File testTagLibDir = MavenTestingUtils.getProjectDir("src/test/taglibjar");
JAR.create(testTagLibDir, new File(libDir, "testtaglib.jar"));
// Configure WebAppContext
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
WebAppContext context = new WebAppContext();
context.setContextPath("/");
File scratchDir = MavenTestingUtils.getTargetFile("tests/" + JstlTest.class.getSimpleName() + "-scratch");
FS.ensureEmpty(scratchDir);
JspConfig.init(context, testWebAppDir.toURI(), scratchDir);
server.setHandler(context);
// Start Server
server.start();
// Figure out Base URI
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
baseUri = new URI(String.format("http://%s:%d/", host, port));
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class ServerWithJNDI method main.
public static void main(String[] args) throws Exception {
// Create the server
Server server = new Server(8080);
// Enable parsing of jndi-related parts of web.xml and jetty-env.xml
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
// Create a WebApp
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
File warFile = new File("../../jetty-distribution/target/distribution/demo-base/webapps/test-jndi.war");
webapp.setWar(warFile.getAbsolutePath());
server.setHandler(webapp);
// Register new transaction manager in JNDI
// At runtime, the webapp accesses this as java:comp/UserTransaction
new org.eclipse.jetty.plus.jndi.Transaction(new com.acme.MockUserTransaction());
// Define an env entry with Server scope.
// At runtime, the webapp accesses this as java:comp/env/woggle
// This is equivalent to putting an env-entry in web.xml:
// <env-entry>
// <env-entry-name>woggle</env-entry-name>
// <env-entry-type>java.lang.Integer</env-entry-type>
// <env-entry-value>4000</env-entry-value>
// </env-entry>
new org.eclipse.jetty.plus.jndi.EnvEntry(server, "woggle", new Integer(4000), false);
// Define an env entry with webapp scope.
// At runtime, the webapp accesses this as java:comp/env/wiggle
// This is equivalent to putting a web.xml entry in web.xml:
// <env-entry>
// <env-entry-name>wiggle</env-entry-name>
// <env-entry-value>100</env-entry-value>
// <env-entry-type>java.lang.Double</env-entry-type>
// </env-entry>
// Note that the last arg of "true" means that this definition for
// "wiggle" would override an entry of the
// same name in web.xml
new org.eclipse.jetty.plus.jndi.EnvEntry(webapp, "wiggle", new Double(100), true);
// Register a reference to a mail service scoped to the webapp.
// This must be linked to the webapp by an entry in web.xml:
// <resource-ref>
// <res-ref-name>mail/Session</res-ref-name>
// <res-type>javax.mail.Session</res-type>
// <res-auth>Container</res-auth>
// </resource-ref>
// At runtime the webapp accesses this as java:comp/env/mail/Session
org.eclipse.jetty.jndi.factories.MailSessionReference mailref = new org.eclipse.jetty.jndi.factories.MailSessionReference();
mailref.setUser("CHANGE-ME");
mailref.setPassword("CHANGE-ME");
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "CHANGE-ME");
props.put("mail.from", "CHANGE-ME");
props.put("mail.debug", "false");
mailref.setProperties(props);
new org.eclipse.jetty.plus.jndi.Resource(webapp, "mail/Session", mailref);
// Register a mock DataSource scoped to the webapp
// This must be linked to the webapp via an entry in web.xml:
// <resource-ref>
// <res-ref-name>jdbc/mydatasource</res-ref-name>
// <res-type>javax.sql.DataSource</res-type>
// <res-auth>Container</res-auth>
// </resource-ref>
// At runtime the webapp accesses this as
// java:comp/env/jdbc/mydatasource
new org.eclipse.jetty.plus.jndi.Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource());
server.start();
server.join();
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class JspIncludeTest method startServer.
@BeforeClass
public static void startServer() throws Exception {
// Setup Server
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
// Setup WebAppContext
File testWebAppDir = MavenTestingUtils.getProjectDir("src/test/webapp");
// Prepare WebApp libs
File libDir = new File(testWebAppDir, "WEB-INF/lib");
FS.ensureDirExists(libDir);
File testTagLibDir = MavenTestingUtils.getProjectDir("src/test/taglibjar");
JAR.create(testTagLibDir, new File(libDir, "testtaglib.jar"));
// Configure WebAppContext
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
WebAppContext context = new WebAppContext();
context.setContextPath("/");
File scratchDir = MavenTestingUtils.getTargetFile("tests/" + JspIncludeTest.class.getSimpleName() + "-scratch");
FS.ensureEmpty(scratchDir);
JspConfig.init(context, testWebAppDir.toURI(), scratchDir);
server.setHandler(context);
// Start Server
server.start();
// Figure out Base URI
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
baseUri = new URI(String.format("http://%s:%d/", host, port));
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class OneWebAppWithJsp 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);
// Setup JMX
MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
// The WebAppContext is the entity that controls the environment in
// which a web application lives and
// breathes. In this example the context path is being set to "/" so it
// is suitable for serving root context
// requests and then we see it setting the location of the war. A whole
// host of other configurations are
// available, ranging from configuring to support annotation scanning in
// the webapp (through
// PlusConfiguration) to choosing where the webapp will unpack itself.
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
File warFile = new File("../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
if (!warFile.exists()) {
throw new RuntimeException("Unable to find WAR File: " + warFile.getAbsolutePath());
}
webapp.setWar(warFile.getAbsolutePath());
webapp.setExtractWAR(true);
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in order to correctly
// set up the jsp container
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these
// container-path jars for tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will
// scan for them instead.
webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");
// A WebAppContext is a ContextHandler as well so it needs to be set to
// the server so it is aware of where to
// send the appropriate requests.
server.setHandler(webapp);
// Configure a LoginService.
// Since this example is for our test webapp, we need to setup a
// LoginService so this shows how to create a very simple hashmap based
// one. The name of the LoginService needs to correspond to what is
// configured in the webapp's web.xml and since it has a lifecycle of
// its own we register it as a bean with the Jetty server object so it
// can be started and stopped according to the lifecycle of the server
// itself.
HashLoginService loginService = new HashLoginService();
loginService.setName("Test Realm");
loginService.setConfig("src/test/resources/realm.properties");
server.addBean(loginService);
// Start things up!
server.start();
server.dumpStdErr();
// The use of server.join() the will make the current thread join and
// wait until the server is done executing.
// See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join();
}
Aggregations