Skip to content

Commit

Permalink
[BISERVER-14935] import-export.bat/.sh does not work on Pentaho 9.3 o…
Browse files Browse the repository at this point in the history
…r higher if requestParameterAuthenticationEnabled=true

Address the comments on the PR review
  • Loading branch information
smmribeiro committed Nov 1, 2023
1 parent 83fc88c commit 7116ba7
Showing 1 changed file with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private void performREST() throws ParseException, KettleException, URISyntaxExce
Messages.getInstance().getString( "CommandLineProcessor.INFO_REST_RESPONSE_STATUS", response.getStatus() );
message += "\n";

if ( logFile != null && !logFile.isEmpty() ) {
if ( StringUtils.isNotBlank( logFile ) ) {
message += Messages.getInstance().getString( "CommandLineProcessor.INFO_REST_FILE_WRITTEN", logFile );
System.out.println( message );
writeToFile( message, logFile );
Expand Down Expand Up @@ -722,7 +722,7 @@ private void logResponseMessage( String logFile, String path, ClientResponse res
response.getEntity( String.class ) ) );
}
System.out.println( message );
if ( logFile != null && !logFile.isEmpty() ) {
if ( StringUtils.isNotBlank( logFile ) ) {
writeToFile( message.toString(), logFile );
}
}
Expand Down Expand Up @@ -754,11 +754,7 @@ private void performBackup() throws ParseException, KettleException, URISyntaxEx
}

// Build the complete URL to use
String backupURL = contextURL + API_REPO_FILES_BACKUP;

// BISERVER-14935: In case "requestParameterAuthenticationEnabled" is set to 'true'
backupURL += ( "?userid=" + getUsername() + "&password=" + Encr.encryptPassword(
Encr.decryptPasswordOptionallyEncrypted( getPassword() ) ) );
String backupURL = buildURL( contextURL, API_REPO_FILES_BACKUP );

WebResource resource = client.resource( backupURL );

Expand All @@ -772,7 +768,7 @@ private void performBackup() throws ParseException, KettleException, URISyntaxEx
message += Messages.getInstance().getString( "CommandLineProcessor.INFO_RESPONSE_STATUS", response.getStatus() );
message += "\n";
message += Messages.getInstance().getString( "CommandLineProcessor.INFO_EXPORT_WRITTEN_TO", outputFile );
if ( logFile != null && !logFile.isEmpty() ) {
if ( StringUtils.isNotBlank( logFile ) ) {
System.out.println( message );
writeToFile( message, logFile );
}
Expand All @@ -781,6 +777,27 @@ private void performBackup() throws ParseException, KettleException, URISyntaxEx
}
}

/**
* Given the Context URL and an API, this method returns the corresponding complete URL adding the proper
* parameters for when the request Parameter Authentication is enabled.
*
* @param contextURL the context URL
* @param apiPath the API path
* @return Complete URL for the given API and already prepared for when the request Parameter Authentication is
* enabled
* @throws ParseException
* @throws KettleException
*/
private String buildURL( String contextURL, String apiPath ) throws ParseException, KettleException {
StringBuilder sb = new StringBuilder();
sb.append( contextURL ).append( apiPath )
.append( "?userid=" ).append( getUsername() )
.append( "&password=" )
.append( Encr.encryptPassword( Encr.decryptPasswordOptionallyEncrypted( getPassword() ) ) );

return sb.toString();
}

/**
* REST Service Restore
* --restore --url=http://localhost:8080/pentaho --username=admin --password=password --overwrite=true
Expand Down Expand Up @@ -872,7 +889,7 @@ private void performExport() throws ParseException, KettleException, URISyntaxEx
message += Messages.getInstance().getString( "CommandLineProcessor.INFO_RESPONSE_STATUS", response.getStatus() );
message += "\n";
message += Messages.getInstance().getString( "CommandLineProcessor.INFO_EXPORT_WRITTEN_TO", outputFile );
if ( logFile != null && !logFile.isEmpty() ) {
if ( StringUtils.isNotBlank( logFile ) ) {
System.out.println( message );
writeToFile( message, logFile );
}
Expand Down Expand Up @@ -900,7 +917,7 @@ private boolean isValidExportPath( String filePath, String logFile ) {
}
}

if ( !isValid && logFile != null && !logFile.isEmpty() ) {
if ( !isValid && StringUtils.isNotBlank( logFile ) ) {
writeToFile( "Invalid file-path:" + filePath, logFile );
}

Expand All @@ -924,18 +941,10 @@ private boolean isValidExportPath( String filePath, String logFile ) {
protected String getOptionValue( final String option, final boolean required, final boolean emptyOk )
throws ParseException {
final String value = StringUtils.trim( commandLine.getOptionValue( option ) );
if ( StringUtils.isEmpty( value ) ) {
// Is it required?
if ( required ) {
throw new ParseException( Messages.getInstance().getErrorString( "CommandLineProcessor.ERROR_0001_MISSING_ARG",
option ) );
}

// Ok, it's not required, but is it Ok to be empty?
if ( !emptyOk ) {
throw new ParseException( Messages.getInstance().getErrorString( "CommandLineProcessor.ERROR_0001_MISSING_ARG",
option ) );
}
if ( StringUtils.isEmpty( value ) && ( required || !emptyOk ) ) {
throw new ParseException( Messages.getInstance().getErrorString( "CommandLineProcessor.ERROR_0001_MISSING_ARG",
option ) );
}

return StringUtils.removeStart( value, "=" );
Expand Down

0 comments on commit 7116ba7

Please sign in to comment.