Skip to content

Commit

Permalink
Fix ServletContext#getResource to resolves to URL if available #24083
Browse files Browse the repository at this point in the history
Signed-off-by:Ondro Mihalyi <mihalyi@omnifish.ee>
  • Loading branch information
OndroMih committed Nov 26, 2022
1 parent 078f1b6 commit 1a604cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
import static org.apache.catalina.util.RequestUtil.urlDecode;
import static org.apache.naming.resources.ProxyDirContext.CONTEXT;
import static org.apache.naming.resources.ProxyDirContext.HOST;
import org.apache.naming.resources.UrlResource;
import static org.glassfish.web.loader.ServletContainerInitializerUtil.getInitializerList;
import static org.glassfish.web.loader.ServletContainerInitializerUtil.getInterestList;

Expand Down Expand Up @@ -6651,10 +6652,14 @@ public URL getResource(String path) throws MalformedURLException {
}

if (resources != null) {
String fullPath = getName() + path;
String hostName = getParent().getName();
try {
resources.lookup(path);
Object resource = resources.lookup(path);
if (resource instanceof UrlResource) {
UrlResource urlResource = (UrlResource)resource;
return urlResource.getUrl();
}
String fullPath = getName() + path;
String hostName = getParent().getName();
return new URL("jndi", "", 0, getJNDIUri(hostName, fullPath),
new DirContextURLStreamHandler(resources));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.naming.Binding;
import javax.naming.NameClassPair;
Expand Down Expand Up @@ -991,7 +993,7 @@ protected ArrayList<NamingEntry> list(File file) {
* This specialized resource implementation avoids opening the InputStream
* to the file right away (which would put a lock on the file).
*/
protected static class FileResource extends Resource {
protected static class FileResource extends Resource implements UrlResource {


// -------------------------------------------------------- Constructor
Expand Down Expand Up @@ -1029,6 +1031,11 @@ public InputStream streamContent()
return super.streamContent();
}

@Override
public URL getUrl() throws MalformedURLException {
return file.toURI().toURL();
}


}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2022 Contributors to Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.apache.naming.resources;

import java.net.MalformedURLException;
import java.net.URL;

public interface UrlResource {
URL getUrl() throws MalformedURLException ;
}

0 comments on commit 1a604cf

Please sign in to comment.