Search in sources :

Example 1 with HttpListener

use of org.glassfish.embeddable.web.HttpListener in project Payara by payara.

the class BasicCDITest method test.

@Test
public void test() throws Exception {
    GlassFishProperties props = new GlassFishProperties();
    props.setPort("http-listener", 8080);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
    glassfish.start();
    // Test Scattered Web Archive
    ScatteredArchive sa = new ScatteredArchive("cdi_basic", ScatteredArchive.Type.WAR, new File("src/main/webapp"));
    sa.addClassPath(new File("target/classes"));
    sa.addClassPath(new File("src/main/resources"));
    URI warURI = sa.toURI();
    printContents(warURI);
    // Deploy archive
    Deployer deployer = glassfish.getDeployer();
    String appname = deployer.deploy(warURI);
    System.out.println("Deployed [" + appname + "]");
    Assert.assertEquals(appname, "cdi_basic");
    // Now create a http listener and access the app.
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    HttpListener listener = new HttpListener();
    listener.setId("my-listener");
    listener.setPort(9090);
    webcontainer.addWebListener(listener);
    get("http://localhost:8080/cdi_basic/BasicCDITestServlet", "All CDI beans have been injected.");
    deployer.undeploy(appname);
    glassfish.dispose();
}
Also used : ScatteredArchive(org.glassfish.embeddable.archive.ScatteredArchive) WebContainer(org.glassfish.embeddable.web.WebContainer) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) URI(java.net.URI) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) Test(org.junit.Test)

Example 2 with HttpListener

use of org.glassfish.embeddable.web.HttpListener in project Payara by payara.

the class BasicCDITest method test.

@Test
public void test() throws Exception {
    GlassFishProperties props = new GlassFishProperties();
    BootstrapProperties bootstrapProperties = new BootstrapProperties();
    props.setPort("http-listener", 8080);
    GlassFish glassfish = GlassFishRuntime.bootstrap(bootstrapProperties).newGlassFish(props);
    glassfish.start();
    // Test Scattered Web Archive
    ScatteredArchive sa = new ScatteredArchive("cdi_ejb_jpa", ScatteredArchive.Type.WAR, new File("src/main/webapp"));
    sa.addClassPath(new File("target/classes"));
    sa.addClassPath(new File("src/main/resources"));
    URI warURI = sa.toURI();
    printContents(warURI);
    // Deploy archive
    Deployer deployer = glassfish.getDeployer();
    String appname = deployer.deploy(warURI);
    System.out.println("Deployed [" + appname + "]");
    Assert.assertEquals(appname, "cdi_ejb_jpa");
    // Now create a http listener and access the app.
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    HttpListener listener = new HttpListener();
    listener.setId("my-listener");
    listener.setPort(9090);
    webcontainer.addWebListener(listener);
    get("http://localhost:8080/cdi_ejb_jpa/BasicCDITestServlet", "All CDI beans have been injected.");
    deployer.undeploy(appname);
    glassfish.dispose();
}
Also used : ScatteredArchive(org.glassfish.embeddable.archive.ScatteredArchive) BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) WebContainer(org.glassfish.embeddable.web.WebContainer) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) URI(java.net.URI) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) Test(org.junit.Test)

Example 3 with HttpListener

use of org.glassfish.embeddable.web.HttpListener in project Payara by payara.

the class WebContainerImpl method bind.

private void bind(Port port, WebListener webListener, String vsId) {
    String protocol = Port.HTTP_PROTOCOL;
    final int portNumber = port.getPortNumber();
    final String defaultVS = vsId;
    final WebListener listener = webListener;
    if (webListener == null) {
        listenerName = getListenerName();
        webListener = new HttpListener();
        webListener.setId(listenerName);
        webListener.setPort(portNumber);
    } else {
        listenerName = webListener.getId();
        protocol = webListener.getProtocol();
    }
    listeners.add(webListener);
    if (protocol.equals(Port.HTTP_PROTOCOL)) {
        securityEnabled = "false";
    } else if (protocol.equals(Port.HTTPS_PROTOCOL)) {
        securityEnabled = "true";
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Protocols>() {

            public Object run(Protocols param) throws TransactionFailure {
                final Protocol protocol = param.createChild(Protocol.class);
                protocol.setName(listenerName);
                protocol.setSecurityEnabled(securityEnabled);
                param.getProtocol().add(protocol);
                final Http http = protocol.createChild(Http.class);
                http.setDefaultVirtualServer(defaultVS);
                http.setFileCache(http.createChild(FileCache.class));
                protocol.setHttp(http);
                return protocol;
            }
        }, networkConfig.getProtocols());
        ConfigSupport.apply(new ConfigCode() {

            public Object run(ConfigBeanProxy... params) throws TransactionFailure {
                NetworkListeners nls = (NetworkListeners) params[0];
                Transports transports = (Transports) params[1];
                final NetworkListener listener = nls.createChild(NetworkListener.class);
                listener.setName(listenerName);
                listener.setPort(Integer.toString(portNumber));
                listener.setProtocol(listenerName);
                listener.setThreadPool("http-thread-pool");
                if (listener.findThreadPool() == null) {
                    final ThreadPool pool = nls.createChild(ThreadPool.class);
                    pool.setName(listenerName);
                    listener.setThreadPool(listenerName);
                }
                listener.setTransport("tcp");
                if (listener.findTransport() == null) {
                    final Transport transport = transports.createChild(Transport.class);
                    transport.setName(listenerName);
                    listener.setTransport(listenerName);
                }
                nls.getNetworkListener().add(listener);
                return listener;
            }
        }, networkConfig.getNetworkListeners(), networkConfig.getTransports());
        if (webListener.getProtocol().equals("https")) {
            NetworkListener networkListener = networkConfig.getNetworkListener(listenerName);
            Protocol httpProtocol = networkListener.findHttpProtocol();
            ConfigSupport.apply(new SingleConfigCode<Protocol>() {

                public Object run(Protocol param) throws TransactionFailure {
                    Ssl newSsl = param.createChild(Ssl.class);
                    populateSslElement(newSsl, listener);
                    System.out.println("SSL " + newSsl.getKeyStore() + " " + newSsl.getKeyStorePassword() + " " + newSsl.getTrustStore() + " " + newSsl.getTrustStorePassword());
                    param.setSsl(newSsl);
                    return newSsl;
                }
            }, httpProtocol);
        }
        com.sun.enterprise.config.serverbeans.VirtualServer vs = httpService.getVirtualServerByName(config.getVirtualServerId());
        ConfigSupport.apply(new SingleConfigCode<com.sun.enterprise.config.serverbeans.VirtualServer>() {

            public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs) throws PropertyVetoException {
                avs.addNetworkListener(listenerName);
                return avs;
            }
        }, vs);
    } catch (Exception e) {
        if (listeners.contains(webListener)) {
            listeners.remove(webListener);
        }
        e.printStackTrace();
    }
}
Also used : Protocols(org.glassfish.grizzly.config.dom.Protocols) ThreadPool(org.glassfish.grizzly.config.dom.ThreadPool) Http(org.glassfish.grizzly.config.dom.Http) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) HttpListener(org.glassfish.embeddable.web.HttpListener) Protocol(org.glassfish.grizzly.config.dom.Protocol) Transports(org.glassfish.grizzly.config.dom.Transports) Ssl(org.glassfish.grizzly.config.dom.Ssl) VirtualServer(org.glassfish.embeddable.web.VirtualServer) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException) PropertyVetoException(java.beans.PropertyVetoException) WebListener(org.glassfish.embeddable.web.WebListener) Transport(org.glassfish.grizzly.config.dom.Transport) org.jvnet.hk2.config(org.jvnet.hk2.config) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 4 with HttpListener

use of org.glassfish.embeddable.web.HttpListener in project Payara by payara.

the class EmbeddedTest method setup.

@BeforeClass
public static void setup() throws GlassFishException {
    glassfish = GlassFishRuntime.bootstrap().newGlassFish();
    glassfish.start();
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    Collection<WebListener> listeners = webcontainer.getWebListeners();
    System.out.println("Network listener size before creation " + listeners.size());
    for (WebListener listener : listeners) {
        System.out.println("Network listener " + listener.getPort());
    }
    try {
        HttpListener listener = new HttpListener();
        listener.setPort(8080);
        listener.setId("embedded-listener-1");
        webcontainer.addWebListener(listener);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    listeners = webcontainer.getWebListeners();
    System.out.println("Network listener size after creation " + listeners.size());
    Assert.assertTrue(listeners.size() == 1);
    for (WebListener listener : listeners) {
        System.out.println("Network listener " + listener.getPort());
    }
}
Also used : WebListener(org.glassfish.embeddable.web.WebListener) WebContainer(org.glassfish.embeddable.web.WebContainer) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) BeforeClass(org.junit.BeforeClass)

Example 5 with HttpListener

use of org.glassfish.embeddable.web.HttpListener in project Payara by payara.

the class ScatteredArchiveTest method test.

@Test
public void test() throws Exception {
    GlassFishProperties props = new GlassFishProperties();
    props.setPort("http-listener", 8080);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
    glassfish.start();
    // Test Scattered Web Archive
    ScatteredArchive sa = new ScatteredArchive("scatteredarchive", ScatteredArchive.Type.WAR, new File("src/main/webapp"));
    sa.addClassPath(new File("target/classes"));
    sa.addClassPath(new File("src/main/resources"));
    URI warURI = sa.toURI();
    printContents(warURI);
    // Deploy archive
    Deployer deployer = glassfish.getDeployer();
    String appname = deployer.deploy(warURI);
    System.out.println("Deployed [" + appname + "]");
    Assert.assertEquals(appname, "scatteredarchive");
    // Now create a http listener and access the app.
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    HttpListener listener = new HttpListener();
    listener.setId("my-listener");
    listener.setPort(9090);
    webcontainer.addWebListener(listener);
    get("http://localhost:9090/satest", "Hi, my name is Bhavani. What's yours?");
    get("http://localhost:9090/satest/ScatteredArchiveTestServlet", "Hi from ScatteredArchiveTestServlet");
    get("http://localhost:8080/satest/ScatteredArchiveTestServlet", "Hi from ScatteredArchiveTestServlet");
    deployer.undeploy(appname);
    // Test Scattered RA
    ScatteredArchive rar = new ScatteredArchive("scatteredra", ScatteredArchive.Type.RAR);
    rar.addClassPath(new File("target/classes"));
    rar.addMetadata(new File("src/main/config/ra.xml"));
    URI rarURI = rar.toURI();
    printContents(rarURI);
    appname = deployer.deploy(rarURI);
    System.out.println("Deployed RAR [" + appname + "]");
    Assert.assertEquals(appname, "scatteredra");
    // Test Scattered Enterprise Archive.
    ScatteredEnterpriseArchive ear = new ScatteredEnterpriseArchive("sear");
    ear.addArchive(warURI, "sa.war");
    ear.addArchive(rarURI);
    ear.addMetadata(new File("src/main/config/application.xml"));
    URI earURI = ear.toURI();
    printContents(earURI);
    appname = deployer.deploy(earURI);
    System.out.println("Deployed [" + appname + "]");
    Assert.assertEquals(appname, "sear");
    get("http://localhost:9090/satest", "Hi, my name is Bhavani. What's yours?");
    get("http://localhost:9090/satest/ScatteredArchiveTestServlet", "Hi from ScatteredArchiveTestServlet");
    get("http://localhost:8080/satest/ScatteredArchiveTestServlet", "Hi from ScatteredArchiveTestServlet");
    glassfish.dispose();
}
Also used : ScatteredArchive(org.glassfish.embeddable.archive.ScatteredArchive) WebContainer(org.glassfish.embeddable.web.WebContainer) ScatteredEnterpriseArchive(org.glassfish.embeddable.archive.ScatteredEnterpriseArchive) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) URI(java.net.URI) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) Test(org.junit.Test)

Aggregations

HttpListener (org.glassfish.embeddable.web.HttpListener)5 WebContainer (org.glassfish.embeddable.web.WebContainer)4 File (java.io.File)3 URI (java.net.URI)3 JarFile (java.util.jar.JarFile)3 Deployer (org.glassfish.embeddable.Deployer)3 GlassFish (org.glassfish.embeddable.GlassFish)3 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)3 ScatteredArchive (org.glassfish.embeddable.archive.ScatteredArchive)3 Test (org.junit.Test)3 GlassFishException (org.glassfish.embeddable.GlassFishException)2 WebListener (org.glassfish.embeddable.web.WebListener)2 PropertyVetoException (java.beans.PropertyVetoException)1 NamingException (javax.naming.NamingException)1 BootstrapProperties (org.glassfish.embeddable.BootstrapProperties)1 ScatteredEnterpriseArchive (org.glassfish.embeddable.archive.ScatteredEnterpriseArchive)1 ConfigException (org.glassfish.embeddable.web.ConfigException)1 VirtualServer (org.glassfish.embeddable.web.VirtualServer)1 Http (org.glassfish.grizzly.config.dom.Http)1 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)1