Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty 12 - Resource resolve() and newResource() return null on resources that do not exist #8702

Merged

Conversation

joakime
Copy link
Contributor

@joakime joakime commented Oct 10, 2022

Cleaned up version of PR #8597

@joakime joakime requested review from gregw, sbordet and lorban October 10, 2022 17:35
@joakime joakime self-assigned this Oct 10, 2022
@joakime joakime changed the title Resource resolve() and newResource() return null on resources that do not exist Jetty 12 - Resource resolve() and newResource() return null on resources that do not exist Oct 10, 2022
Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I'm still not convinced that we really want to scatter null checks all over the code base. I also really don't like the implied suggestion that a non null resource does exist. That might be true soon after resolved, but it might not be true for a resource taken out of a collection some time after the resolution.

So I think regardless of returning null or not, I think we need some static methods that will do the null checking:

    interface Resources
    {
        public static boolean exists(Resource r)
        {
            return r != null && r.exists();
        }
        public static boolean isDirectory(Resource r)
        {
            return r != null && r.isDirectory();
        }
    }

This style mimics the statics in Paths and Files (although I don't mind if they are on Resource rather than a Resources interface).

I'm also wondering if perhaps an Optional would be better than a null return. So resolve would always return a non null resource (even if it is a BadResource), but then we'd have a new method:

    public Optional<Resource> resolveExisting(String subPath)
    {
        Resource resource = resolve(subPath);
        if (resource.exists())
            return Optional.of(resource);
        return Optional.empty();
    }

Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the statics... but a few niggles

Comment on lines 261 to 265
public boolean isReadable()
{
// always a directory, never readable
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A directory can be readable, in that it's contents can be listed.

I can see an interpretation of isReadable that means there is content available, but this is a change in semantics that I don't think we need. It is a valid check to ask a directory if it is readable or not. Perhaps this check could be made in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asking "isReadable()" should be different than "isDirectory()".
Meaning if you are isDirectory() == true then you have no need to call isReadable() anyway.

If you look at usage, the isReadable() checks are typically for places where we expect a non-directory that can be accessed and be read (eg: a file)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that we used to check some directories for readability. I don't really see the need for changing this semantic. What does a directory that exists but is not actually readable on the file system look like? If all our directory handling code expects directories to not be readable, then we won't be able to distinguish this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps rename to hasContent or hasReadableContent if you want your semantic, to distinguish from the Files method that can apply to a directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it should be canBeOpenForRead() instead.
As it's not meant for "can we access" or "do we have read permissions".

@joakime joakime merged commit 259deea into jetty-12.0.x Oct 19, 2022
@joakime joakime deleted the fix/jetty-12-resource-resolve-new-null-on-notexist branch October 19, 2022 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants