Search in sources :

Example 86 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class SslContextFactoryTest method testNoTsResourceKs.

@Test
public void testNoTsResourceKs() throws Exception {
    Resource keystoreResource = Resource.newSystemResource("keystore");
    cf.setKeyStoreResource(keystoreResource);
    cf.setKeyStorePassword("storepwd");
    cf.setKeyManagerPassword("keypwd");
    cf.setTrustStoreResource(keystoreResource);
    cf.setTrustStorePassword(null);
    cf.start();
    assertTrue(cf.getSslContext() != null);
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Test(org.junit.Test)

Example 87 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class SslContextFactoryTest method testResourceTsWrongPWResourceKs.

@Test
public void testResourceTsWrongPWResourceKs() throws Exception {
    Resource keystoreResource = Resource.newSystemResource("keystore");
    Resource truststoreResource = Resource.newSystemResource("keystore");
    cf.setKeyStoreResource(keystoreResource);
    cf.setTrustStoreResource(truststoreResource);
    cf.setKeyStorePassword("storepwd");
    cf.setKeyManagerPassword("keypwd");
    cf.setTrustStorePassword("wrong_storepwd");
    try (StacklessLogging stackless = new StacklessLogging(AbstractLifeCycle.class)) {
        cf.start();
        Assert.fail();
    } catch (IOException e) {
        Assert.assertThat(e.toString(), Matchers.containsString("java.io.IOException: Keystore was tampered with, or password was incorrect"));
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IOException(java.io.IOException) Test(org.junit.Test)

Example 88 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class AbstractWebAppObjectInSessionTest method testWebappObjectInSession.

@Test
public void testWebappObjectInSession() throws Exception {
    String contextName = "webappObjectInSessionTest";
    String contextPath = "/" + contextName;
    String servletMapping = "/server";
    File targetDir = new File(System.getProperty("basedir"), "target");
    File warDir = new File(targetDir, contextName);
    warDir.mkdir();
    File webInfDir = new File(warDir, "WEB-INF");
    webInfDir.mkdir();
    // Write web.xml
    File webXml = new File(webInfDir, "web.xml");
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" + "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" + "         version=\"2.4\">\n" + "\n" + "</web-app>";
    FileWriter w = new FileWriter(webXml);
    w.write(xml);
    w.close();
    File classesDir = new File(webInfDir, "classes");
    classesDir.mkdir();
    String packageName = WebAppObjectInSessionServlet.class.getPackage().getName();
    File packageDirs = new File(classesDir, packageName.replace('.', File.separatorChar));
    packageDirs.mkdirs();
    String resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + ".class";
    Resource resource = Resource.newResource(getClass().getResource(resourceName));
    //File sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
    File targetFile = new File(packageDirs, resourceName);
    //copy(sourceFile, targetFile);
    IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
    resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + "$" + WebAppObjectInSessionServlet.TestSharedStatic.class.getSimpleName() + ".class";
    resource = Resource.newResource(getClass().getResource(resourceName));
    //sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
    targetFile = new File(packageDirs, resourceName);
    //copy(sourceFile, targetFile);
    IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
    server1.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        TestServer server2 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
        server2.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
        try {
            server2.start();
            int port2 = server2.getPort();
            HttpClient client = new HttpClient();
            client.start();
            try {
                // Perform one request to server1 to create a session
                Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=set");
                request.method(HttpMethod.GET);
                ContentResponse response = request.send();
                assertEquals(HttpServletResponse.SC_OK, response.getStatus());
                String sessionCookie = response.getHeaders().get("Set-Cookie");
                assertTrue(sessionCookie != null);
                // Mangle the cookie, replacing Path with $Path, etc.
                sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
                // Perform a request to server2 using the session cookie from the previous request
                Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
                request2.method(HttpMethod.GET);
                request2.header("Cookie", sessionCookie);
                ContentResponse response2 = request2.send();
                assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            } finally {
                client.stop();
            }
        } finally {
            server2.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FileWriter(java.io.FileWriter) Resource(org.eclipse.jetty.util.resource.Resource) Request(org.eclipse.jetty.client.api.Request) FileOutputStream(java.io.FileOutputStream) HttpClient(org.eclipse.jetty.client.HttpClient) File(java.io.File) Test(org.junit.Test)

Example 89 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class PreconfigureStandardTestWar method main.

public static void main(String[] args) throws Exception {
    String target = "target/test-standard-preconfigured";
    File file = new File(target);
    if (file.exists())
        IO.delete(file);
    File realmPropertiesDest = new File("target/test-standard-realm.properties");
    if (realmPropertiesDest.exists())
        IO.delete(realmPropertiesDest);
    Resource realmPropertiesSrc = Resource.newResource("src/test/resources/realm.properties");
    realmPropertiesSrc.copyTo(realmPropertiesDest);
    System.setProperty("jetty.home", "target");
    PreconfigureQuickStartWar.main("target/test-standard.war", target, "src/test/resources/test.xml");
    LOG.info("Preconfigured in {}ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - __start));
// IO.copy(new FileInputStream("target/test-standard-preconfigured/WEB-INF/quickstart-web.xml"),System.out);
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) File(java.io.File)

Example 90 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class QuickStartTest method testStandardTestWar.

@Test
public void testStandardTestWar() throws Exception {
    PreconfigureStandardTestWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-standard-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-standard-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/test/dump/info");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    assertThat(IO.toString((InputStream) connection.getContent()), Matchers.containsString("Dump Servlet"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6