Search in sources :

Example 1 with MalformedURLException

use of java.net.MalformedURLException in project jetty.project by eclipse.

the class WebAppContext method getResource.

/* ------------------------------------------------------------ */
@Override
public Resource getResource(String uriInContext) throws MalformedURLException {
    if (uriInContext == null || !uriInContext.startsWith(URIUtil.SLASH))
        throw new MalformedURLException(uriInContext);
    IOException ioe = null;
    Resource resource = null;
    int loop = 0;
    while (uriInContext != null && loop++ < 100) {
        try {
            resource = super.getResource(uriInContext);
            if (resource != null && resource.exists())
                return resource;
            uriInContext = getResourceAlias(uriInContext);
        } catch (IOException e) {
            LOG.ignore(e);
            if (ioe == null)
                ioe = e;
        }
    }
    if (ioe != null && ioe instanceof MalformedURLException)
        throw (MalformedURLException) ioe;
    return resource;
}
Also used : MalformedURLException(java.net.MalformedURLException) Resource(org.eclipse.jetty.util.resource.Resource) IOException(java.io.IOException)

Example 2 with MalformedURLException

use of java.net.MalformedURLException in project jetty.project by eclipse.

the class ResourceAliasTest method testNullCharEndingFilename.

/* ------------------------------------------------------------ */
@Test
public void testNullCharEndingFilename() throws Exception {
    File file = new File(__dir, "test.txt");
    Assert.assertFalse(file.exists());
    Assert.assertTrue(file.createNewFile());
    Assert.assertTrue(file.exists());
    File file0 = new File(__dir, "test.txt\0");
    if (!file0.exists())
        // this file system does not suffer this problem
        return;
    // This is an alias!
    Assert.assertTrue(file0.exists());
    Resource dir = Resource.newResource(__dir);
    // Test not alias paths
    Resource resource = Resource.newResource(file);
    Assert.assertTrue(resource.exists());
    Assert.assertNull(resource.getAlias());
    resource = Resource.newResource(file.getAbsoluteFile());
    Assert.assertTrue(resource.exists());
    Assert.assertNull(resource.getAlias());
    resource = Resource.newResource(file.toURI());
    Assert.assertTrue(resource.exists());
    Assert.assertNull(resource.getAlias());
    resource = Resource.newResource(file.toURI().toString());
    Assert.assertTrue(resource.exists());
    Assert.assertNull(resource.getAlias());
    resource = dir.addPath("test.txt");
    Assert.assertTrue(resource.exists());
    Assert.assertNull(resource.getAlias());
    // Test alias paths
    resource = Resource.newResource(file0);
    Assert.assertTrue(resource.exists());
    Assert.assertNotNull(resource.getAlias());
    resource = Resource.newResource(file0.getAbsoluteFile());
    Assert.assertTrue(resource.exists());
    Assert.assertNotNull(resource.getAlias());
    resource = Resource.newResource(file0.toURI());
    Assert.assertTrue(resource.exists());
    Assert.assertNotNull(resource.getAlias());
    resource = Resource.newResource(file0.toURI().toString());
    Assert.assertTrue(resource.exists());
    Assert.assertNotNull(resource.getAlias());
    try {
        resource = dir.addPath("test.txt\0");
        Assert.assertTrue(resource.exists());
        Assert.assertNotNull(resource.getAlias());
    } catch (MalformedURLException e) {
        Assert.assertTrue(true);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) File(java.io.File) Test(org.junit.Test)

Example 3 with MalformedURLException

use of java.net.MalformedURLException in project jetty.project by eclipse.

the class FileSystemResourceTest method testAddRootPath.

@Test
public void testAddRootPath() throws Exception {
    Path dir = testdir.getPath().normalize().toRealPath();
    Path subdir = dir.resolve("sub");
    Files.createDirectories(subdir);
    String readableRootDir = findRootDir(dir.getFileSystem());
    assumeThat("Readable Root Dir found", readableRootDir, notNullValue());
    try (Resource base = newResource(dir.toFile())) {
        Resource sub = base.addPath("sub");
        assertThat("sub", sub.isDirectory(), is(true));
        try {
            Resource rrd = sub.addPath(readableRootDir);
            // valid path for unix and OSX
            assertThat("Readable Root Dir", rrd.exists(), is(false));
        } catch (MalformedURLException | InvalidPathException e) {
        // valid path on Windows
        }
    }
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) Matchers.containsString(org.hamcrest.Matchers.containsString) InvalidPathException(java.nio.file.InvalidPathException) Test(org.junit.Test)

Example 4 with MalformedURLException

use of java.net.MalformedURLException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testMalformedUrlNotMaven.

public void testMalformedUrlNotMaven() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    // has two colons, so it appears similar to maven coordinates
    MalformedURLException e = expectThrows(MalformedURLException.class, () -> installPlugin("://host:1234", env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("no protocol"));
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) Environment(org.elasticsearch.env.Environment)

Example 5 with MalformedURLException

use of java.net.MalformedURLException in project vert.x by eclipse.

the class NestedJarFileResolverTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // This folder is inside the embedded jar file called nested.jar, inside webroot4.jar
    webRoot = "webroot4";
    prevCL = Thread.currentThread().getContextClassLoader();
    URL webroot4URL = prevCL.getResource("webroot4.jar");
    ClassLoader loader = new ClassLoader(prevCL = Thread.currentThread().getContextClassLoader()) {

        @Override
        public URL getResource(String name) {
            try {
                if (name.startsWith("lib/")) {
                    return new URL("jar:" + webroot4URL + "!/" + name);
                } else if (name.startsWith("webroot4")) {
                    return new URL("jar:" + webroot4URL + "!/lib/nested.jar!/" + name.substring(7));
                }
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
            return super.getResource(name);
        }
    };
    Thread.currentThread().setContextClassLoader(loader);
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)3822 URL (java.net.URL)2872 IOException (java.io.IOException)1186 File (java.io.File)905 ArrayList (java.util.ArrayList)370 InputStream (java.io.InputStream)362 HttpURLConnection (java.net.HttpURLConnection)293 URISyntaxException (java.net.URISyntaxException)270 URI (java.net.URI)239 InputStreamReader (java.io.InputStreamReader)226 BufferedReader (java.io.BufferedReader)208 HashMap (java.util.HashMap)199 Map (java.util.Map)166 URLClassLoader (java.net.URLClassLoader)165 URLConnection (java.net.URLConnection)147 FileNotFoundException (java.io.FileNotFoundException)137 Matcher (java.util.regex.Matcher)132 Test (org.junit.Test)129 UnsupportedEncodingException (java.io.UnsupportedEncodingException)118 Pattern (java.util.regex.Pattern)113