You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bu...@apache.org on 2011/04/23 04:07:32 UTC

DO NOT REPLY [Bug 51110] New: Ambiguous behaviour of URLResource isExists(boolean)

https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

           Summary: Ambiguous behaviour of URLResource isExists(boolean)
           Product: Ant
           Version: 1.8.2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Core
        AssignedTo: notifications@ant.apache.org
        ReportedBy: acwwat@gmail.com


= Overview =
When using a <url> as the nested resource the <resourceexists> condition, my
expectation is that the condition should evaluate to true if the remote file
referred to by the URL does not exist. However, the sample test case below
demonstrates otherwise. Looking at the URLResource code, it seems like
URLConnection.connect() is used for the evaluation. I've done some test and
notice that this method returns true as long as the connection to the server
can be made.

I am not sure whether this is by design, but I find that in order to truly
determine if the remote file exists, the URLConnection.getContent() method must
be called. It is when the method returns FileNotFoundException as expected (at
least for files . I assume that a call to conn.getContent() can be added to
isExists() in URLResource, but I am not sure what to do with the
UnknownServiceException that it throws. Perhaps someone can comment more on
this to determine what the best solution is.

= Steps to Reproduce =
Run the following sample Ant build file on a system with Internet access.

<project name="build" default="all">
  <target name="all">
    <fail message="Resource does not exist.">
      <condition>
        <not>
          <resourceexists>
            <url url="http://ant.apache.org/nosuchfile.html" />
          </resourceexists>
        </not>
      </condition>
    </fail>
  </target>
</project>

= Actual Result =
Ant build fails with the message "Resource does not exist."

= Expected Result =
Ant build completes "successfully".

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

Stefan Bodewig <bo...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #28330|0                           |1
        is obsolete|                            |

--- Comment #10 from Stefan Bodewig <bo...@apache.org> 2012-02-14 11:58:31 UTC ---
Created attachment 28331
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28331
Combining both approaches

At least for me this patch works for http, ftp, file and jar URLs (at least jar
URLs on top of file URLs).

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #8 from Stefan Bodewig <bo...@apache.org> 2012-02-14 11:35:20 UTC ---
Created attachment 28330
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28330
Alternative patch using the HTTP status code

Thanks again.

I think we have a problem, though.  The whole point of the closeConnection
parameter is to avoid opening more connections to the server than necessary. 
With your patch we could simply ignore the flag and always close the connection
as the result would be the same.

OTOH your experiments show there is no way to be absolutely sure the URL
actually is connected to an existing resource without using a method that makes
the connection unusable for subsequent calls.  Is this correct?

I have extended your example to cover file and jar URLs as well and the current
code works for those.  I need to dig out an ftp-server to verify there.

For HTTP URLs we could check the status-code and in fact the patch I'll attach
does just that and makes your test pass.  In my simplistic patch I assume the
URLConnection already handles redirects so any 1xx, 2xx and 3xx should mean the
resource exists.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #1 from Stefan Bodewig <bo...@apache.org> 2011-05-13 15:15:27 UTC ---
would getHeaderFields work as well?

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #3 from Anthony Wat <ac...@gmail.com> 2012-02-13 02:09:16 UTC ---
Created attachment 28307
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28307
Patch to add check for true existence of a URL resource

The patch is for
src/main/org/apache/tools/ant/types/resources/URLResource.java.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #6 from Anthony Wat <ac...@gmail.com> 2012-02-13 19:34:39 UTC ---
Created attachment 28324
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28324
Updated patch with better connection state handling

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

Anthony Wat <ac...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Ambiguous expected result   |[PATCH] Ambiguous expected
                   |from the resourceexists     |result from the
                   |condition with URLResource  |resourceexists condition
                   |                            |with URLResource

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #4 from Anthony Wat <ac...@gmail.com> 2012-02-13 02:11:22 UTC ---
It seems that URLConnection.getContent() and URLConnection.getInputStream() are
the only methods that can generate FileNotFoundException when the resource
really does not exist. I've created a patch to use the latter, which will avoid
unused data from being downloaded. Please review and commit if everything looks
OK. Thanks a lot.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

Anthony Wat <ac...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |acwwat@gmail.com

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #9 from Stefan Bodewig <bo...@apache.org> 2012-02-14 11:40:39 UTC ---
My patch (and thus Ant's current code) doesn't work for FTP.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

[Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

Stefan Bodewig <bo...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |1.9.4

--- Comment #11 from Stefan Bodewig <bo...@apache.org> ---
svn revision 1577796

-- 
You are receiving this mail because:
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #7 from Anthony Wat <ac...@gmail.com> 2012-02-13 19:35:59 UTC ---
(In reply to comment #6)
> Created attachment 28324 [details]
> Updated patch with better connection state handling

Thanks Stefan for your review. I've improved the patch to reconnect after the
InputStream verification. Please take another look at your convenience and let
me know if it looks better now.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

Anthony Wat <ac...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Ambiguous behaviour of      |Ambiguous expected result
                   |URLResource                 |from the resourceexists
                   |isExists(boolean)           |condition with URLResource

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #2 from Anthony Wat <ac...@gmail.com> 2011-05-13 19:14:38 UTC ---
(In reply to comment #1)
> would getHeaderFields work as well?

I just did some test with the URL API and getHeaderFields() won't fail even if
the resource does not exist. It seems that only getContent() and
getOutputStream() would fail.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 51110] [PATCH] Ambiguous expected result from the resourceexists condition with URLResource

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=51110

--- Comment #5 from Stefan Bodewig <bo...@apache.org> 2012-02-13 16:58:09 UTC ---
Thanks Anthony.

I'm not sure what happens if the closeConnection to isExists is false.  With
your patch we may call getInputStream, close the stream and call it again
later.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.