Search in sources :

Example 1 with KettleRepositoryNotFoundException

use of org.pentaho.di.repository.KettleRepositoryNotFoundException in project pentaho-kettle by pentaho.

the class ExecuteJobServlet method openRepository.

@VisibleForTesting
Repository openRepository(String repositoryName, String user, String pass) throws KettleException {
    if (Utils.isEmpty(repositoryName)) {
        return null;
    }
    RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
    repositoriesMeta.readData();
    RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repositoryName);
    if (repositoryMeta == null) {
        String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnableToFindRepository", repositoryName);
        throw new KettleRepositoryNotFoundException(message);
    }
    PluginRegistry registry = PluginRegistry.getInstance();
    Repository repository = registry.loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
    repository.init(repositoryMeta);
    repository.connect(user, pass);
    return repository;
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) Repository(org.pentaho.di.repository.Repository) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) KettleRepositoryNotFoundException(org.pentaho.di.repository.KettleRepositoryNotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with KettleRepositoryNotFoundException

use of org.pentaho.di.repository.KettleRepositoryNotFoundException in project pentaho-kettle by pentaho.

the class ExecuteJobServlet method doGet.

/**
 * <div id="mindtouch">
 *    <h1>/kettle/executeJob</h1>
 *    <a name="GET"></a>
 *    <h2>GET</h2>
 *    <p>Executes job from the specified repository.
 *  Connects to the repository provided as a parameter, loads the job from it and executes it.
 *  Empty response is returned or response contains output of an error happened during the job execution.
 *  Response contains <code>ERROR</code> result if error happened during job execution.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    GET /kettle/executeJob/?rep=my_repository&user=my_user&pass=my_password&job=my_job&level=INFO
 *    </pre>
 *
 *    </p>
 *    <h3>Parameters</h3>
 *    <table class="pentaho-table">
 *    <tbody>
 *    <tr>
 *      <th>name</th>
 *      <th>description</th>
 *      <th>type</th>
 *    </tr>
 *    <tr>
 *    <td>rep</td>
 *    <td>Repository id to connect to.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>user</td>
 *    <td>User name to be used to connect to repository.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>pass</td>
 *    <td>User password to be used to connect to repository.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>job</td>
 *    <td>Job name to be loaded and executed.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>level</td>
 *    <td>Logging level to be used for job execution (i.e. Debug).</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>*any name*</td>
 *    <td>All the other parameters will be sent to the job for using as variables.
 *  When necessary you can add custom parameters to the request.
 *  They will be used to set the job variables values.</td>
 *    <td>query</td>
 *    </tr>
 *    </tbody>
 *    </table>
 *
 *  <h3>Response Body</h3>
 *
 *  <table class="pentaho-table">
 *    <tbody>
 *      <tr>
 *        <td align="right">element:</td>
 *        <td>(custom)</td>
 *      </tr>
 *      <tr>
 *        <td align="right">media types:</td>
 *        <td>application/xml</td>
 *      </tr>
 *    </tbody>
 *  </table>
 *    <p>Response contains error output of the job executed or Carte object Id
 *  if the execution was successful.</p>
 *
 *    <p><b>Example Error Response:</b></p>
 *    <pre function="syntax.xml">
 *  <webresult>
 *    <result>OK</result>
 *    <message>Job started</message>
 *    <id>74d96aa6-f29a-4bac-a26a-06a8c8f107e5</id>
 *  </webresult>
 *    </pre>
 *
 *    <h3>Status Codes</h3>
 *    <table class="pentaho-table">
 *  <tbody>
 *    <tr>
 *      <th>code</th>
 *      <th>description</th>
 *    </tr>
 *    <tr>
 *      <td>200</td>
 *      <td>Request was processed.</td>
 *    </tr>
 *    <tr>
 *      <td>500</td>
 *      <td>Internal server error occurs during request processing.</td>
 *    </tr>
 *  </tbody>
 *</table>
 *</div>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
        return;
    }
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "ExecuteJobServlet.Log.ExecuteJobRequested"));
    }
    // Options taken from PAN
    // 
    String[] knownOptions = new String[] { "rep", "user", "pass", "job", "level" };
    String repOption = request.getParameter("rep");
    String userOption = request.getParameter("user");
    String passOption = Encr.decryptPasswordOptionallyEncrypted(request.getParameter("pass"));
    String jobOption = request.getParameter("job");
    String levelOption = request.getParameter("level");
    PrintWriter out = response.getWriter();
    Repository repository;
    try {
        repository = openRepository(repOption, userOption, passOption);
    } catch (KettleRepositoryNotFoundException krnfe) {
        // Repository not found.
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnableToFindRepository", repOption);
        out.println(new WebResult(WebResult.STRING_ERROR, message));
        return;
    } catch (KettleException ke) {
        // Authentication Error.
        if (ke.getCause() instanceof ExecutionException) {
            ExecutionException ee = (ExecutionException) ke.getCause();
            if (ee.getCause() instanceof KettleAuthenticationException) {
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.Authentication", getContextPath());
                out.println(new WebResult(WebResult.STRING_ERROR, message));
                return;
            }
        }
        // Something unexpected occurred.
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ke));
        out.println(new WebResult(WebResult.STRING_ERROR, message));
        return;
    }
    String encoding = System.getProperty("KETTLE_DEFAULT_SERVLET_ENCODING", null);
    if (encoding != null && !Utils.isEmpty(encoding.trim())) {
        response.setCharacterEncoding(encoding);
        response.setContentType("text/html; charset=" + encoding);
    }
    JobMeta jobMeta;
    try {
        jobMeta = loadJob(repository, jobOption);
    } catch (KettleException ke) {
        // Job not found in repository.
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        out.println(new WebResult(WebResult.STRING_ERROR, ke.getMessage()));
        return;
    }
    // Set the servlet parameters as variables in the job
    // 
    String[] parameters = jobMeta.listParameters();
    Enumeration<?> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String parameter = (String) parameterNames.nextElement();
        String[] values = request.getParameterValues(parameter);
        // 
        if (Const.indexOfString(parameter, knownOptions) < 0) {
            // 
            if (Const.indexOfString(parameter, parameters) < 0) {
                jobMeta.setVariable(parameter, values[0]);
            } else {
                try {
                    jobMeta.setParameterValue(parameter, values[0]);
                } catch (UnknownParamException upe) {
                    // Unknown parameter is unexpected.
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(upe));
                    out.println(new WebResult(WebResult.STRING_ERROR, message));
                }
            }
        }
    }
    JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration();
    LogLevel logLevel = LogLevel.getLogLevelForCode(levelOption);
    jobExecutionConfiguration.setLogLevel(logLevel);
    JobConfiguration jobConfiguration = new JobConfiguration(jobMeta, jobExecutionConfiguration);
    String carteObjectId = UUID.randomUUID().toString();
    SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
    servletLoggingObject.setContainerObjectId(carteObjectId);
    servletLoggingObject.setLogLevel(logLevel);
    // Create the job and store in the list...
    // 
    final Job job = new Job(repository, jobMeta, servletLoggingObject);
    job.setRepository(repository);
    job.setSocketRepository(getSocketRepository());
    getJobMap().addJob(jobMeta.getName(), carteObjectId, job, jobConfiguration);
    job.setContainerObjectId(carteObjectId);
    if (repository != null) {
        // The repository connection is open: make sure we disconnect from the repository once we
        // are done with this job.
        // 
        Repository finalRepository = repository;
        job.addJobListener(new JobAdapter() {

            @Override
            public void jobFinished(Job job) {
                finalRepository.disconnect();
            }
        });
    }
    try {
        runJob(job);
        WebResult webResult = new WebResult(WebResult.STRING_OK, "Job started", carteObjectId);
        out.println(webResult.getXML());
        out.flush();
    } catch (Exception executionException) {
        // Something went wrong while running the job.
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        String logging = KettleLogStore.getAppender().getBuffer(job.getLogChannelId(), false).toString();
        String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.WhileExecutingJob", jobOption, logging);
        out.println(new WebResult(WebResult.STRING_ERROR, message));
        return;
    }
    // Everything went well till the end.
    response.setStatus(HttpServletResponse.SC_OK);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleAuthenticationException(org.pentaho.di.repository.KettleAuthenticationException) JobMeta(org.pentaho.di.job.JobMeta) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) JobExecutionConfiguration(org.pentaho.di.job.JobExecutionConfiguration) JobAdapter(org.pentaho.di.job.JobAdapter) LogLevel(org.pentaho.di.core.logging.LogLevel) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) KettleRepositoryNotFoundException(org.pentaho.di.repository.KettleRepositoryNotFoundException) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KettleAuthenticationException(org.pentaho.di.repository.KettleAuthenticationException) Repository(org.pentaho.di.repository.Repository) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleRepositoryNotFoundException(org.pentaho.di.repository.KettleRepositoryNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) Job(org.pentaho.di.job.Job) JobConfiguration(org.pentaho.di.job.JobConfiguration) PrintWriter(java.io.PrintWriter)

Aggregations

KettleRepositoryNotFoundException (org.pentaho.di.repository.KettleRepositoryNotFoundException)2 Repository (org.pentaho.di.repository.Repository)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1 KettleException (org.pentaho.di.core.exception.KettleException)1 LogLevel (org.pentaho.di.core.logging.LogLevel)1 SimpleLoggingObject (org.pentaho.di.core.logging.SimpleLoggingObject)1 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)1 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)1 Job (org.pentaho.di.job.Job)1 JobAdapter (org.pentaho.di.job.JobAdapter)1 JobConfiguration (org.pentaho.di.job.JobConfiguration)1 JobExecutionConfiguration (org.pentaho.di.job.JobExecutionConfiguration)1 JobMeta (org.pentaho.di.job.JobMeta)1 KettleAuthenticationException (org.pentaho.di.repository.KettleAuthenticationException)1 RepositoriesMeta (org.pentaho.di.repository.RepositoriesMeta)1 RepositoryMeta (org.pentaho.di.repository.RepositoryMeta)1