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, "RunJobServlet.Log.RunJobRequested" ) );
    }

    // Options taken from PAN
    //
    String[] knownOptions = new String[] { "job", "level", };

    String transOption = request.getParameter( "job" );
    String levelOption = request.getParameter( "level" );

    response.setStatus( HttpServletResponse.SC_OK );

    PrintWriter out = response.getWriter();
    SlaveServerConfig serverConfig = transformationMap.getSlaveServerConfig();
    try {
      Repository slaveServerRepository = serverConfig.getRepository();
      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
      //
      String[] parameters = jobMeta.listParameters();
      Enumeration<?> parameterNames = request.getParameterNames();
      while ( parameterNames.hasMoreElements() ) {
        String parameter = (String) parameterNames.nextElement();
        String[] values = request.getParameterValues( parameter );

        // Ignore the known options. set the rest as variables
        //
        if ( Const.indexOfString( parameter, knownOptions ) < 0 ) {
          // If it's a trans parameter, set it, otherwise simply set the
          // variable
          //
          if ( Const.indexOfString( parameter, parameters ) < 0 ) {
            jobMeta.setVariable( parameter, values[0] );
          } else {
            jobMeta.setParameterValue( parameter, values[0] );
          }
        }
      }

      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 );

      // Create new repository connection for this job
      //
      final Repository repository = jobExecutionConfiguration.connectRepository(
          serverConfig.getRepositoryId(), serverConfig.getRepositoryUsername(), serverConfig.getRepositoryPassword() );

      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 transformation and store in the list...
      //
      final Job job = new Job( repository, jobMeta, servletLoggingObject );

      // Setting variables
      //
      job.initializeVariablesFrom( null );
      job.getJobMeta().setInternalKettleVariables( job );
      job.injectVariables( jobConfiguration.getJobExecutionConfiguration().getVariables() );

      // Also copy the parameters over...
      //
      job.copyParametersFrom( jobMeta );
      job.clearParameters();
      /*
       * String[] parameterNames = job.listParameters(); for (int idx = 0; idx < parameterNames.length; idx++) { // Grab
       * the parameter value set in the job entry // String thisValue =
       * jobExecutionConfiguration.getParams().get(parameterNames[idx]); if (!Utils.isEmpty(thisValue)) { // Set the
       * value as specified by the user in the job entry // jobMeta.setParameterValue(parameterNames[idx], thisValue); }
       * }
       */
      jobMeta.activateParameters();

      job.setSocketRepository( getSocketRepository() );

      JobMap jobMap = getJobMap();

      jobMap.addJob( job.getJobname(), carteObjectId, job, jobConfiguration );


      // Disconnect from the job's repository when the job finishes.
      //
      job.addJobListener( new JobAdapter() {
        public void jobFinished( Job job ) {
          repository.disconnect();
        }
      } );

      String message = "Job '" + job.getJobname() + "' was added to the list with id " + carteObjectId;
      logBasic( message );

      try {
        runJob( job );

        WebResult webResult = new WebResult( WebResult.STRING_OK, "Job started", carteObjectId );
        out.println( webResult.getXML() );
        out.flush();

      } catch ( Exception executionException ) {
        response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
        String logging = KettleLogStore.getAppender().getBuffer( job.getLogChannelId(), false ).toString();
        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 ) ) ) );
    }
  }
@Test
  public void doGetMissingMandatoryParamJobTest() throws Exception {

    HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
    HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );
    TransformationMap transformationMap = mock( TransformationMap.class );
    SlaveServerConfig slaveServerConfig = mock( SlaveServerConfig.class );
    Repository repository = mock( Repository.class );
    setInternalState( runJobServlet, "transformationMap", transformationMap );

    KettleLogStore.init();
    StringWriter out = new StringWriter();
    PrintWriter printWriter = new PrintWriter( out );

    when( mockHttpServletRequest.getParameter( "job" ) ).thenReturn( null );
    when( mockHttpServletRequest.getParameter( "level" ) ).thenReturn( "BASIC" );
    when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );
    when( transformationMap.getSlaveServerConfig() ).thenReturn( slaveServerConfig );
    when( slaveServerConfig.getRepository() ).thenReturn( repository );
    when( repository.isConnected() ).thenReturn( true );

    runJobServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );

    verify( mockHttpServletResponse ).setStatus( HttpServletResponse.SC_OK );
    verify( mockHttpServletResponse ).setStatus( HttpServletResponse.SC_BAD_REQUEST );
  }