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