From d3bf8e26086fb4f633b099b63e8e32a143ef29fc Mon Sep 17 00:00:00 2001 From: SreejaSridhar Date: Tue, 17 Dec 2024 15:17:32 +0530 Subject: [PATCH] [BACKLOG-42356] Check if the file already exists before saving --- .../web/http/api/resources/FileResource.java | 34 +++++++++++++++++++ .../http/api/resources/FileResourceTest.java | 28 +++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/extensions/src/main/java/org/pentaho/platform/web/http/api/resources/FileResource.java b/extensions/src/main/java/org/pentaho/platform/web/http/api/resources/FileResource.java index c5f1f69f1a..2de26dc473 100755 --- a/extensions/src/main/java/org/pentaho/platform/web/http/api/resources/FileResource.java +++ b/extensions/src/main/java/org/pentaho/platform/web/http/api/resources/FileResource.java @@ -102,6 +102,7 @@ import java.io.OutputStream; import java.io.StringReader; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.channels.IllegalSelectorException; import java.security.GeneralSecurityException; @@ -490,6 +491,39 @@ public Response doGetFileOrDir( @PathParam ( "pathId" ) String pathId ) { return buildStatusResponse( Response.Status.FORBIDDEN ); } } + /** + * Checks if a file exists at the specified path in the repository. + * + *

Example Request:
+ * GET pentaho/api/repo/exists?pathId=%2Fhome%2Fuser%2Ftest_file.wtr + *

+ * + * @param pathId Encoded path of the repository file to check. Must be URL-encoded. + * + * @return A jax-rs Response object with the appropriate status code + */ + + @GET + @Path( "/exists" ) + @StatusCodes( { + @ResponseCode( code = 200, condition = "Successfully finds the file." ), + @ResponseCode( code = 404, condition = "Invalid Input." ), + @ResponseCode( code = 404, condition = "Failed to find the file." ), + @ResponseCode( code = 500, condition = "For any other exceptions." ) + } ) + public Response doesFileExists( @QueryParam ( "pathId" ) String pathId ) { + try { + boolean fileExists = fileService.doesExist( URLDecoder.decode( pathId, "UTF-8" ) ); + if ( !fileExists ) { + return buildStatusResponse( Response.Status.NOT_FOUND ); + } + } catch ( UnsupportedEncodingException e ) { + return buildStatusResponse( Response.Status.BAD_REQUEST ); + } catch ( Exception e ) { + return buildStatusResponse( Response.Status.INTERNAL_SERVER_ERROR ); + } + return buildOkResponse(); + } // Overloaded this method to try and minimize calls to the repo // Had to unmap this method since browsers ask for resources with Accepts="*/*" which will default to this method diff --git a/extensions/src/test/java/org/pentaho/platform/web/http/api/resources/FileResourceTest.java b/extensions/src/test/java/org/pentaho/platform/web/http/api/resources/FileResourceTest.java index a828cc8fef..d79289589d 100644 --- a/extensions/src/test/java/org/pentaho/platform/web/http/api/resources/FileResourceTest.java +++ b/extensions/src/test/java/org/pentaho/platform/web/http/api/resources/FileResourceTest.java @@ -69,6 +69,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URLDecoder; import java.nio.channels.IllegalSelectorException; import java.security.GeneralSecurityException; import java.security.InvalidParameterException; @@ -715,6 +716,33 @@ public void testDoIsParameterizable() throws Exception { verify( mockAttribElement, times( 1 ) ).attributeValue( "value" ); } + @Test + public void testDoesFileExists() throws Exception { + // Test Case 1: File exists, expect 200 (OK) + String validPathId = "validPathId"; + doReturn( true ).when( fileResource.fileService ).doesExist( URLDecoder.decode( + validPathId, "UTF-8" ) ); + + Response response = fileResource.doesFileExists( validPathId ); + assertEquals( Response.Status.OK.getStatusCode(), response.getStatus() ); + + // Test Case 2: File does not exist, expect 404 ( not found) + String invalidPathId = "invalidPathId"; + doReturn( false ).when( fileResource.fileService ).doesExist( URLDecoder.decode( + invalidPathId, "UTF-8" ) ); + + response = fileResource.doesFileExists( invalidPathId ); + assertEquals( Response.Status.NOT_FOUND.getStatusCode(), response.getStatus() ); + + // Test Case 3: Unexpected exception occurs, expect 500 (internal server error) + String pathIdForError = "pathForError"; + doThrow( new RuntimeException( "Unexpected error" ) ).when( fileResource.fileService ).doesExist( URLDecoder.decode( + pathIdForError, "UTF-8" ) ); + + response = fileResource.doesFileExists( pathIdForError ); + assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus( ) ); + } + @Test public void testDoIsParameterizableError() throws Exception {