Skip to content

Commit

Permalink
Merge pull request #5815 from SreejaSridhar/file-exist
Browse files Browse the repository at this point in the history
[BACKLOG-42356] Check if the file already exists before saving
  • Loading branch information
NJtwentyone authored Dec 20, 2024
2 parents 9038db5 + d3bf8e2 commit 2c60be9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
* <p><b>Example Request:</b><br />
* GET pentaho/api/repo/exists?pathId=%2Fhome%2Fuser%2Ftest_file.wtr
* </p>
*
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 2c60be9

Please sign in to comment.