Hi dev list,

I frequently noticed, that Resource.in() just creates non existing files and 
then returns an empty input stream. This almost ever causes further problems 
like https://github.com/geoserver/geoserver/pull/6216.

I think the root cause is the implementation of Resource.in() for UNDEFINED 
resources.
Looking at Files.ResourceAdaptor.in() I find:

    final File actualFile = file();
    if (!actualFile.exists()) {
        throw new IllegalStateException("Cannot access " + actualFile);
    }

Seems, the initial intention was to throw an exception, but since file() 
creates the missing file, it does not work as intended. I analyzed the former 
GSIP 106, but I did not find any hints about the expected behavior here.

This can be fixed easily and in think an Exception about a missing file is much 
more expressive than a message from a failed content parser.

Most use cases check for Type.RESOURCE in advance or have some elaborated 
exception handlers and are thus unaffected. Some usages however try to handle 
this condition later on by using inputStream.available() like 
EchoParametersDao.getEchoParameters(in) does and will fail then.

I'm working on a pull request to change the behavior this way, but this causes 
adaptions to other related areas. Even SpringResourceAdaptor is affected, since 
it relays on IOExcetion which have been wrapped into IllegalStateException (and 
for which I think an UncheckedIOException is the better choice).

Other solutions are:

return null (can be checked afterwards but not so nice)

return an empty InputStream (Java 11 even has some: 
InputStream.nullInputStream())

Do you think it is worth to propose a refactoring here or should I better keep 
hands off?

Dieter.
--
Dieter Stüken
Software Engineer
Nature Environment and Resources

T  +49 251 59689 403
d.stue...@conterra.de<mailto:d.stue...@conterra.de>

con terra GmbH
Martin-Luther-King-Weg 20
48155 Münster
conterra.de<https://www.conterra.com/>

Geschäftsführung: Karl Wiesmann, Uwe König
HRB 4149, Amtsgericht Münster
Privacy statements<https://www.con-terra.com/privacy-statements>

YouTube<https://www.youtube.com/conterrachannel> | 
Twitter<https://twitter.com/conterra> | 
LinkedIn<https://www.linkedin.com/company/con-terra-gmbh>

_______________________________________________
Geoserver-devel mailing list
Geoserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

Reply via email to