This is an automated email from the ASF dual-hosted git repository.

svenmeier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new d8e4fcb  WICKET-6714 resources from META-INF/resources
d8e4fcb is described below

commit d8e4fcb228452882ecdc917d44967a0bda9e4fcf
Author: Sven Meier <svenme...@apache.org>
AuthorDate: Sat Dec 7 23:07:41 2019 +0100

    WICKET-6714 resources from META-INF/resources
    
    even without web root
---
 .../protocol/http/mock/MockServletContext.java     | 83 ++++++----------------
 .../MetaInfStaticResourceReferenceTest.java        |  2 +-
 2 files changed, 23 insertions(+), 62 deletions(-)

diff --git 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
index 8904e96..6a0f8e6 100755
--- 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
@@ -17,8 +17,6 @@
 package org.apache.wicket.protocol.http.mock;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationHandler;
@@ -407,25 +405,15 @@ public class MockServletContext implements ServletContext
        @Override
        public String getRealPath(String name)
        {
-               if (webappRoot == null)
-               {
-                       return null;
-               }
-
-               if (name.startsWith("/"))
-               {
-                       name = name.substring(1);
-               }
-
-               File f = new File(webappRoot, name);
-               if (!f.exists())
-               {
-                       return null;
-               }
-               else
-               {
-                       return f.getPath();
+               try {
+                       URL url = getResource(name);
+                       if (url != null) {
+                               return url.getFile();
+                       }
+               } catch (IOException e) {
+                       log.error(e.getMessage(), e);
                }
+               return null;
        }
 
        /**
@@ -468,30 +456,21 @@ public class MockServletContext implements ServletContext
        @Override
        public URL getResource(String name) throws MalformedURLException
        {
-               if (webappRoot == null)
-               {
-                       return null;
-               }
-
-               URL result = null;
-
                if (name.startsWith("/"))
                {
                        name = name.substring(1);
                }
 
-               File f = new File(webappRoot, name);
-               if (f.exists())
+               if (webappRoot != null)
                {
-                       result = f.toURI().toURL();
-               }
-
-               if (result == null)
-               {
-                       result = 
getClass().getClassLoader().getResource("META-INF/resources/" + name);
+                       File f = new File(webappRoot, name);
+                       if (f.exists())
+                       {
+                               return f.toURI().toURL();
+                       }
                }
 
-               return result;
+               return 
getClass().getClassLoader().getResource("META-INF/resources/" + name);
        }
 
        /**
@@ -504,33 +483,15 @@ public class MockServletContext implements ServletContext
        @Override
        public InputStream getResourceAsStream(String name)
        {
-               if (webappRoot == null)
-               {
-                       return null;
-               }
-
-               if (name.startsWith("/"))
-               {
-                       name = name.substring(1);
-               }
-
-               File f = new File(webappRoot, name);
-               if (!f.exists())
-               {
-                       return null;
-               }
-               else
-               {
-                       try
-                       {
-                               return new FileInputStream(f);
-                       }
-                       catch (FileNotFoundException e)
-                       {
-                               log.error(e.getMessage(), e);
-                               return null;
+               try {
+                       URL url = getResource(name);
+                       if (url != null) {
+                               return url.openStream();
                        }
+               } catch (IOException e) {
+                       log.error(e.getMessage(), e);
                }
+               return null;
        }
 
        /**
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java
index dd899ca..135e42f 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java
@@ -52,7 +52,7 @@ class MetaInfStaticResourceReferenceTest
        void testWithServlet30() throws MalformedURLException
        {
                MockApplication application = new MockApplication();
-               MockServletContext servletContext = new 
MockServletContext(application, "/");
+               MockServletContext servletContext = new 
MockServletContext(application, null);
                BaseWicketTester tester = new BaseWicketTester(application, 
servletContext);
 
                MetaInfStaticResourceReference metaRes = new 
MetaInfStaticResourceReference(getClass(),

Reply via email to