Search in sources :

Example 16 with UnknownParamException

use of org.pentaho.di.core.parameters.UnknownParamException in project pentaho-kettle by pentaho.

the class Trans method activateParameters.

/**
 * Activates all parameters by setting their values. If no values already exist, the method will attempt to set the
 * parameter to the default value. If no default value exists, the method will set the value of the parameter to the
 * empty string ("").
 *
 * @see org.pentaho.di.core.parameters.NamedParams#activateParameters()
 */
@Override
public void activateParameters() {
    String[] keys = listParameters();
    for (String key : keys) {
        String value;
        try {
            value = getParameterValue(key);
        } catch (UnknownParamException e) {
            value = "";
        }
        String defValue;
        try {
            defValue = getParameterDefault(key);
        } catch (UnknownParamException e) {
            defValue = "";
        }
        if (Utils.isEmpty(value)) {
            setVariable(key, Const.NVL(defValue, ""));
        } else {
            setVariable(key, Const.NVL(value, ""));
        }
    }
}
Also used : UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Example 17 with UnknownParamException

use of org.pentaho.di.core.parameters.UnknownParamException 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

UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)17 KettleException (org.pentaho.di.core.exception.KettleException)7 Date (java.util.Date)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 JobMeta (org.pentaho.di.job.JobMeta)4 TransMeta (org.pentaho.di.trans.TransMeta)4 IOException (java.io.IOException)3 TableItem (org.eclipse.swt.widgets.TableItem)3 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 Job (org.pentaho.di.job.Job)3 Repository (org.pentaho.di.repository.Repository)3 IParamInfo (org.pentaho.metaverse.api.model.IParamInfo)3 ParamInfo (org.pentaho.metaverse.impl.model.ParamInfo)3 Timestamp (java.sql.Timestamp)2 KettleClientEnvironment (org.pentaho.di.core.KettleClientEnvironment)2 Result (org.pentaho.di.core.Result)2 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)2