public boolean resourceExists( String resourceName )
throws TransferFailedException, AuthorizationException
{
String repositoryUrl = getRepository().getUrl();
String url = repositoryUrl + ( repositoryUrl.endsWith( "/" ) ? "" : "/" ) + resourceName;
HttpHead headMethod = new HttpHead( url );
HttpResponse response = null;
int statusCode;
try
{
response = execute( headMethod );
}
catch ( IOException e )
{
throw new TransferFailedException( e.getMessage(), e );
}
catch ( HttpException e )
{
throw new TransferFailedException( e.getMessage(), e );
}
try
{
statusCode = response.getStatusLine().getStatusCode();
String reasonPhrase = ", ReasonPhrase:" + response.getStatusLine().getReasonPhrase() + ".";
switch ( statusCode )
{
case HttpStatus.SC_OK:
return true;
case HttpStatus.SC_NOT_MODIFIED:
return true;
case SC_NULL:
throw new TransferFailedException( "Failed to transfer file: " + url + reasonPhrase );
case HttpStatus.SC_FORBIDDEN:
throw new AuthorizationException( "Access denied to: " + url + reasonPhrase );
case HttpStatus.SC_UNAUTHORIZED:
throw new AuthorizationException( "Not authorized" + reasonPhrase );
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
throw new AuthorizationException( "Not authorized by proxy" + reasonPhrase );
case HttpStatus.SC_NOT_FOUND:
return false;
//add more entries here
default:
throw new TransferFailedException(
"Failed to transfer file: " + url + ". Return code is: " + statusCode + reasonPhrase );
}
}
finally
{
headMethod.abort();
}
}