Search in sources :

Example 11 with NetworkConnector

use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.

the class WebInfConfiguration method getCanonicalNameForWebAppTmpDir.

/**
     * Create a canonical name for a webapp temp directory.
     * <p>
     * The form of the name is:
     * 
     * <pre>"jetty-"+host+"-"+port+"-"+resourceBase+"-_"+context+"-"+virtualhost+"-"+randomdigits+".dir"</pre>
     *
     *  host and port uniquely identify the server
     *  context and virtual host uniquely identify the webapp
     *  randomdigits ensure every tmp directory is unique
     *  
     * @param context the context to get the canonical name from 
     * @return the canonical name for the webapp temp directory
     */
public static String getCanonicalNameForWebAppTmpDir(WebAppContext context) {
    StringBuffer canonicalName = new StringBuffer();
    canonicalName.append("jetty-");
    //get the host and the port from the first connector
    Server server = context.getServer();
    if (server != null) {
        Connector[] connectors = context.getServer().getConnectors();
        if (connectors.length > 0) {
            //Get the host
            String host = null;
            int port = 0;
            if (connectors != null && (connectors[0] instanceof NetworkConnector)) {
                NetworkConnector connector = (NetworkConnector) connectors[0];
                host = connector.getHost();
                port = connector.getLocalPort();
                if (port < 0)
                    port = connector.getPort();
            }
            if (host == null)
                host = "0.0.0.0";
            canonicalName.append(host);
            //Get the port
            canonicalName.append("-");
            //if not available (eg no connectors or connector not started),
            //try getting one that was configured.
            canonicalName.append(port);
            canonicalName.append("-");
        }
    }
    //Resource  base
    try {
        Resource resource = context.getBaseResource();
        if (resource == null) {
            if (context.getWar() == null || context.getWar().length() == 0)
                throw new IllegalStateException("No resourceBase or war set for context");
            // Set dir or WAR
            resource = context.newResource(context.getWar());
        }
        String tmp = URIUtil.decodePath(resource.getURL().getPath());
        if (tmp.endsWith("/"))
            tmp = tmp.substring(0, tmp.length() - 1);
        if (tmp.endsWith("!"))
            tmp = tmp.substring(0, tmp.length() - 1);
        //get just the last part which is the filename
        int i = tmp.lastIndexOf("/");
        canonicalName.append(tmp.substring(i + 1, tmp.length()));
        canonicalName.append("-");
    } catch (Exception e) {
        LOG.warn("Can't generate resourceBase as part of webapp tmp dir name: " + e);
        LOG.debug(e);
    }
    //Context name
    String contextPath = context.getContextPath();
    contextPath = contextPath.replace('/', '_');
    contextPath = contextPath.replace('\\', '_');
    canonicalName.append(contextPath);
    //Virtual host (if there is one)
    canonicalName.append("-");
    String[] vhosts = context.getVirtualHosts();
    if (vhosts == null || vhosts.length <= 0)
        canonicalName.append("any");
    else
        canonicalName.append(vhosts[0]);
    // sanitize
    for (int i = 0; i < canonicalName.length(); i++) {
        char c = canonicalName.charAt(i);
        if (!Character.isJavaIdentifierPart(c) && "-.".indexOf(c) < 0)
            canonicalName.setCharAt(i, '.');
    }
    canonicalName.append("-");
    return canonicalName.toString();
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 12 with NetworkConnector

use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStringContent.

@Test
public void testServerWithHttpClientStringContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new BytesContentProvider(__message.getBytes("UTF8")));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(__message, _received);
        Assert.assertEquals(200, response.getStatus());
    } finally {
        client.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 13 with NetworkConnector

use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.

the class JspAndDefaultWithAliasesTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    server = new Server(0);
    // Configure LoginService
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    File realmFile = MavenTestingUtils.getTestResourceFile("realm.properties");
    login.setConfig(realmFile.getAbsolutePath());
    server.addBean(login);
    // Configure WebApp
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    File webappBase = MavenTestingUtils.getTestResourceDir("docroots/jsp");
    context.setResourceBase(webappBase.getAbsolutePath());
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    // add default servlet
    ServletHolder defaultServHolder = context.addServlet(DefaultServlet.class, "/");
    // important! must be TRUE
    defaultServHolder.setInitParameter("aliases", "true");
    // add jsp
    ServletHolder jsp = new ServletHolder(new FakeJspServlet());
    context.addServlet(jsp, "*.jsp");
    jsp.setInitParameter("classpath", context.getClassPath());
    // add context
    server.setHandler(context);
    server.start();
    int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
    serverURI = new URI("http://localhost:" + port + "/");
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 14 with NetworkConnector

use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.

the class JspAndDefaultWithoutAliasesTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    server = new Server(0);
    // Configure LoginService
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    File realmFile = MavenTestingUtils.getTestResourceFile("realm.properties");
    login.setConfig(realmFile.getAbsolutePath());
    server.addBean(login);
    // Configure WebApp
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    File webappBase = MavenTestingUtils.getTestResourceDir("docroots/jsp");
    context.setResourceBase(webappBase.getAbsolutePath());
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    // add default servlet
    ServletHolder defaultServHolder = context.addServlet(DefaultServlet.class, "/");
    // important! must be FALSE
    defaultServHolder.setInitParameter("aliases", "false");
    // add jsp
    ServletHolder jsp = new ServletHolder(new FakeJspServlet());
    context.addServlet(jsp, "*.jsp");
    jsp.setInitParameter("classpath", context.getClassPath());
    // add context
    server.setHandler(context);
    server.start();
    int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
    serverURI = new URI("http://localhost:" + port + "/");
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 15 with NetworkConnector

use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.

the class JettyHttpServer method bind.

@Override
public void bind(InetSocketAddress addr, int backlog) throws IOException {
    // check if there is already a connector listening
    Collection<NetworkConnector> connectors = _server.getBeans(NetworkConnector.class);
    if (connectors != null) {
        for (NetworkConnector connector : connectors) {
            if (connector.getPort() == addr.getPort()) {
                if (LOG.isDebugEnabled())
                    LOG.debug("server already bound to port " + addr.getPort() + ", no need to rebind");
                return;
            }
        }
    }
    if (_serverShared)
        throw new IOException("jetty server is not bound to port " + addr.getPort());
    this._addr = addr;
    if (LOG.isDebugEnabled())
        LOG.debug("binding server to port " + addr.getPort());
    ServerConnector connector = new ServerConnector(_server);
    connector.setPort(addr.getPort());
    connector.setHost(addr.getHostName());
    _server.addConnector(connector);
    _connectors.put(addr.getHostName() + addr.getPort(), connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) IOException(java.io.IOException)

Aggregations

NetworkConnector (org.eclipse.jetty.server.NetworkConnector)20 Server (org.eclipse.jetty.server.Server)9 Test (org.junit.Test)8 Connector (org.eclipse.jetty.server.Connector)7 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)6 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 URI (java.net.URI)5 IOException (java.io.IOException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpClient (org.eclipse.jetty.client.HttpClient)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 Request (org.eclipse.jetty.client.api.Request)3 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)2 File (java.io.File)2 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 MessageDigest (java.security.MessageDigest)2 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)2 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)2 HashLoginService (org.eclipse.jetty.security.HashLoginService)2