Skip to content

Commit

Permalink
[PDI-17006] Issues with Carte Jobs API runJob endpoint in Pentaho RES…
Browse files Browse the repository at this point in the history
…T API (pentaho#7592)

* [PDI-17006] Issues with Carte Jobs API runJob endpoint in Pentaho REST API

* [PDI-17006] unit tests

* [PDI-17006] Sonar Issues

* [PDI-17006] removed spaces from new messages
  • Loading branch information
ssamora authored Aug 27, 2020
1 parent 27f9309 commit 59b76fc
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 8 deletions.
73 changes: 65 additions & 8 deletions engine/src/main/java/org/pentaho/di/www/RunJobServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2002-2020 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -32,6 +32,7 @@
import javax.servlet.http.HttpServletResponse;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.IdNotFoundException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.LogLevel;
Expand All @@ -56,6 +57,9 @@ public class RunJobServlet extends BaseHttpServlet implements CartePluginInterfa

public static final String CONTEXT_PATH = "/kettle/runJob";

private static final String UNAUTHORIZED_ACCESS_TO_REPOSITORY = "The server sent HTTP status code 401";
private static final String UNABLE_TO_LOAD_JOB = "Unable to load job";

public RunJobServlet() {
}

Expand Down Expand Up @@ -135,6 +139,18 @@ <td>Logging level to be used for job execution (i.e. Debug).</td>
<td>200</td>
<td>Request was processed.</td>
</tr>
<tr>
<td>400</td>
<td>Bad Request: Mandatory parameter job missing</td>
</tr>
<tr>
<td>401</td>
<td>Unauthorized access to the repository</td>
</tr>
<tr>
<td>404</td>
<td>Not found: Job not found</td>
</tr>
<tr>
<td>500</td>
<td>Internal server error occurs during request processing.</td>
Expand Down Expand Up @@ -163,14 +179,23 @@ public void doGet( HttpServletRequest request, HttpServletResponse response ) th
response.setStatus( HttpServletResponse.SC_OK );

PrintWriter out = response.getWriter();

SlaveServerConfig serverConfig = transformationMap.getSlaveServerConfig();
try {

SlaveServerConfig serverConfig = transformationMap.getSlaveServerConfig();
Repository slaveServerRepository = serverConfig.getRepository();
if ( slaveServerRepository == null ) {
throw new KettleException( "Unable to connect to repository in Slave Server Config: " + serverConfig.getRepositoryId() );
if ( slaveServerRepository == null || !slaveServerRepository.isConnected() ) {
response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.UnableToConnectToRepository", serverConfig.getRepositoryId() ) ) );
return;
}

if ( transOption == null ) {
response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.MissingMandatoryParameterJob" ) ) );
return;
}

final JobMeta jobMeta = loadJob( slaveServerRepository, transOption );

// Set the servlet parameters as variables in the transformation
Expand All @@ -196,6 +221,12 @@ public void doGet( HttpServletRequest request, HttpServletResponse response ) th
}

JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration();
if ( levelOption != null && !isValidLogLevel( levelOption ) ) {
response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.InvalidLogLevel" ) ) );
return;
}
LogLevel logLevel = LogLevel.getLogLevelForCode( levelOption );
jobExecutionConfiguration.setLogLevel( logLevel );

Expand Down Expand Up @@ -261,11 +292,28 @@ public void jobFinished( Job job ) {
out.flush();

} catch ( Exception executionException ) {
response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
String logging = KettleLogStore.getAppender().getBuffer( job.getLogChannelId(), false ).toString();
throw new KettleException( "Error executing Job: " + logging, executionException );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.ErrorExecutingJob", serverConfig.getRepositoryId(), logging ) ) );
}
} catch ( IdNotFoundException idEx ) {
response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.UnableToRunJob", serverConfig.getRepositoryId() ) ) );
} catch ( Exception ex ) {

if ( ex.getMessage().contains( UNAUTHORIZED_ACCESS_TO_REPOSITORY ) ) {
response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.UnableToConnectToRepository", serverConfig.getRepositoryId() ) ) );
return;
} else if ( ex.getMessage().contains( UNABLE_TO_LOAD_JOB ) ) {
response.setStatus( HttpServletResponse.SC_NOT_FOUND );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.UnableToFindJob", serverConfig.getRepositoryId() ) ) );
return;
}
response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
out.println( new WebResult( WebResult.STRING_ERROR, BaseMessages.getString(
PKG, "RunJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker( ex ) ) ) );
}
Expand Down Expand Up @@ -309,6 +357,15 @@ private JobMeta loadJob( Repository repository, String job ) throws KettleExcept
}
}

private boolean isValidLogLevel( String levelOption ) {
for ( LogLevel level : LogLevel.values() ) {
if ( level.getCode().equals( levelOption ) ) {
return true;
}
}
return false;
}

public String toString() {
return "Run Job";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ ExecuteJobServlet.Log.ExecuteJobRequested=Execution of job requested through ser
LisDataServicesServlet.ListRequested=Processing request for a list of services
RunJobServlet.Log.RunJobRequested=Request received to run a job
RunJobServlet.Error.UnexpectedError=Unexpected error running job: {0}
RunJobServlet.Error.UnableToConnectToRepository=Unable to connect to repository with Slave Server Config: {0}
RunJobServlet.Error.MissingMandatoryParameterJob=Missing mandatory parameter job
RunJobServlet.Error.InvalidLogLevel=Log level is invalid
RunJobServlet.Error.UnableToFindJob=Unable to find job in repository with Slave Server Config: {0}
RunJobServlet.Error.UnableToRunJob=Unable to run job in repository with Slave Server Config: {0}. User does not have permission
RunJobServlet.Error.ErrorExecutingJob=Error executing job in repository with Slave Server Config: {0}. {1}
GetTransImageServlet.Log.TransImageRequested=Image of transformation requested
TransStatusServlet.GetTransImage=Show an image of the transformation
GetJobImageServlet.Log.JobImageRequested=Image of job requested
Expand Down
Loading

0 comments on commit 59b76fc

Please sign in to comment.