Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACKLOG-42356] Check if the file already exists before saving #5815

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
NJtwentyone marked this conversation as resolved.
Show resolved Hide resolved
NJtwentyone marked this conversation as resolved.
Show resolved Hide resolved
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
Loading