Skip to content

Commit

Permalink
Replace special case handling in getRelativeCloudPath
Browse files Browse the repository at this point in the history
Instead URI.toNormalPath() uses the KeyValueAccess itself to convert
URIs to a "normalPath" that work with the KeyValueAccess.

This works for all KeyValueAccess implementations (GC, S3, file system).
  • Loading branch information
tpietzsch committed Nov 25, 2024
1 parent 5491ae5 commit b96f4b7
Showing 1 changed file with 27 additions and 42 deletions.
69 changes: 27 additions & 42 deletions src/main/java/util/URITools.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ else if ( URITools.isS3( xmlURI ) || URITools.isGC( xmlURI ) )
// fist make a copy of the XML and save it to not loose it
try
{
final String xmlFile = getRelativeCloudPath( xmlURI );
final String xmlFile = toNormalPath( kva, xmlURI );

if ( kva.exists( xmlFile ) )
{
Expand Down Expand Up @@ -478,47 +478,43 @@ else if ( SplitViewerImgLoader.class.isInstance( loader ) )
return false;
}

/**
* This is an abstraction that is only required because the Google cloud and AWS KeyValueAccesses behave differently
*
* @param uri - the URI for which to create the relative path
* @return the relative path
* @throws URISyntaxException
*/
public static String getRelativeCloudPath( final URI uri ) throws URISyntaxException
{
if ( URITools.isGC( uri ) )
return new URI( null, null, uri.getPath(), null ).toString();
else if ( URITools.isS3( uri ) )
return new URI( uri.getScheme(), uri.getHost(), uri.getPath(), null ).toString(); // TODO: this is a bug
else
{
if ( uri.getScheme() != null )
throw new RuntimeException( "Unsupported uri scheme: " + uri.getScheme() + " in '" + uri + "'." );
else
throw new RuntimeException( "Cannot get a relative cloud path for a relative path '" + uri + "'." );
}
}

public static BufferedReader openFileReadCloudReader( final KeyValueAccess kva, final URI uri ) throws IOException
{
return new BufferedReader(new InputStreamReader( openFileReadCloudStream( kva, uri )));
}

public static InputStream openFileReadCloudStream( final KeyValueAccess kva, final URI uri ) throws IOException
{
final String relativePath;
return kva.lockForReading( toNormalPath( kva, uri ) ).newInputStream();
}

/**
* Get the "normalPath" of the given {@code URI} for the given {@code KeyValueAccess}.
* <p>
* The {@code URI} can be absolute or relative.
* Relative {@code URI} are interpreted as relative to the root of the {@code KeyValueAccess}
* (e.g. {@code 'file:/'} for {@code FileSystemKeyValueAccess},
* {@code 's3://bucket-name/'} for {@code AmazonS3KeyValueAccess}, etc).
*
* @param kva a KeyValueAccess
* @param uri absolute or relative URI
* @return normalPath of {@code uri} for the given {@code KeyValueAccess}
* @throws IOException
*/
static String toNormalPath( final KeyValueAccess kva, final URI uri ) throws IOException
{
try
{
relativePath = getRelativeCloudPath( uri );
final URI root = kva.uri( "/" );
final URI relativeURI = uri.isAbsolute()
? new URI( "/" ).resolve( root.relativize( uri ) )
: uri;
return kva.compose( root, relativeURI.getPath() );
}
catch (URISyntaxException e)
catch ( URISyntaxException e )
{
throw new IOException( e.getMessage() );
throw new IOException( e );
}

return kva.lockForReading( relativePath ).newInputStream();
}

public static PrintWriter openFileWriteCloudWriter( final KeyValueAccess kva, final URI uri ) throws IOException
Expand All @@ -528,22 +524,11 @@ public static PrintWriter openFileWriteCloudWriter( final KeyValueAccess kva, fi

public static OutputStream openFileWriteCloudStream( final KeyValueAccess kva, final URI uri ) throws IOException
{
final String relativePath;

try
{
relativePath = getRelativeCloudPath( uri );
}
catch (URISyntaxException e)
{
throw new IOException( e.getMessage() );
}

return kva.lockForWriting( relativePath ).newOutputStream();
return kva.lockForWriting( toNormalPath( kva, uri ) ).newOutputStream();
}

/**
* Note: it is up to you to create the correct relative paths using getRelativeCloudPath()
* Note: it is up to you to create the correct relative paths using toNormalPath()
*
* @param kva
* @param relativeSrc
Expand Down

0 comments on commit b96f4b7

Please sign in to comment.