Search in sources :

Example 11 with TransConfiguration

use of org.pentaho.di.trans.TransConfiguration in project pentaho-kettle by pentaho.

the class ExecuteTransServlet method doGet.

/**
 * <div id="mindtouch">
 *    <h1>/kettle/executeTrans</h1>
 *    <a name="GET"></a>
 *    <h2>GET</h2>
 *    <p>Executes transformation from the specified repository.
 *  Connects to the repository provided as a parameter, loads the transformation from it and executes it.
 *  Empty response is returned or response contains output of an error happened during the transformation execution.
 *  Response contains <code>ERROR</code> result if error happened during transformation execution.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    GET /kettle/executeTrans/?rep=my_repository&user=my_user&pass=my_password&trans=my_trans&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>trans</td>
 *    <td>Transfromation name to be loaded and executed.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>level</td>
 *    <td>Logging level to be used for transformation execution (i.e. Debug).</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>*any name*</td>
 *    <td>All the other parameters will be sent to the transformation for using as variables.
 *  When necessary you can add custom parameters to the request.
 *  They will be used to set the transformation 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 transformation executed or nothing
 *  if the execution was successful.</p>
 *
 *    <p><b>Example Error Response:</b></p>
 *    <pre function="syntax.xml">
 *  <webresult>
 *    <result>ERROR</result>
 *    <message>Unexpected error executing the transformation&#x3a;
 *    &#xd;&#xa;org.pentaho.di.core.exception.KettleException&#x3a;
 *    &#xd;&#xa;Unable to find transformation &#x27;dummy-trans.ktr&#x27; in directory
 *    &#x3a;&#x2f;home&#x2f;admin&#xd;&#xa;&#xd;&#xa; at
 *    org.pentaho.di.www.ExecuteTransServlet.loadTransformation&#x28;ExecuteTransServlet.java&#x3a;214&#x29;&#xd;&#xa;
 *    at org.pentaho.di.www.ExecuteTransServlet.doGet&#x28;ExecuteTransServlet.java&#x3a;104&#x29;&#xd;&#xa;
 *    at javax.servlet.http.HttpServlet.service&#x28;HttpServlet.java&#x3a;707&#x29;&#xd;&#xa;
 *    at javax.servlet.http.HttpServlet.service&#x28;HttpServlet.java&#x3a;820&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.servlet.ServletHolder.handle&#x28;ServletHolder.java&#x3a;511&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.servlet.ServletHandler.handle&#x28;ServletHandler.java&#x3a;390&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.servlet.SessionHandler.handle&#x28;SessionHandler.java&#x3a;182&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.handler.ContextHandler.handle&#x28;ContextHandler.java&#x3a;765&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.handler.ContextHandlerCollection.handle&#x28;ContextHandlerCollection.java&#x3a;230&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.handler.HandlerCollection.handle&#x28;HandlerCollection.java&#x3a;114&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.handler.HandlerWrapper.handle&#x28;HandlerWrapper.java&#x3a;152&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.Server.handle&#x28;Server.java&#x3a;326&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.HttpConnection.handleRequest&#x28;HttpConnection.java&#x3a;536&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.HttpConnection&#x24;RequestHandler.headerComplete&#x28;HttpConnection.java&#x3a;915&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.HttpParser.parseNext&#x28;HttpParser.java&#x3a;539&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.HttpParser.parseAvailable&#x28;HttpParser.java&#x3a;212&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.HttpConnection.handle&#x28;HttpConnection.java&#x3a;405&#x29;&#xd;&#xa;
 *    at org.mortbay.jetty.bio.SocketConnector&#x24;Connection.run&#x28;SocketConnector.java&#x3a;228&#x29;&#xd;&#xa;
 *    at org.mortbay.thread.QueuedThreadPool&#x24;PoolThread.run&#x28;QueuedThreadPool.java&#x3a;582&#x29;&#xd;&#xa;
 *    </message>
 *    <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>400</td>
 *      <td>When missing mandatory params repo and trans</td>
 *    </tr>
 *    <tr>
 *      <td>401</td>
 *      <td>When authentication to repository fails</td>
 *    </tr>
 *    <tr>
 *      <td>404</td>
 *      <td>When transformation is not found</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, "ExecuteTransServlet.Log.ExecuteTransRequested"));
    }
    // Options taken from PAN
    // 
    String[] knownOptions = new String[] { REP, USER, PASS, TRANS, LEVEL };
    String repOption = request.getParameter(REP);
    String userOption = request.getParameter(USER);
    String passOption = Encr.decryptPasswordOptionallyEncrypted(request.getParameter(PASS));
    String transOption = request.getParameter(TRANS);
    String levelOption = request.getParameter(LEVEL);
    response.setStatus(HttpServletResponse.SC_OK);
    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);
    }
    PrintWriter out = response.getWriter();
    if (repOption == null || transOption == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.MissingMandatoryParameter", repOption == null ? REP : TRANS)));
        return;
    }
    try {
        final Repository repository = openRepository(repOption, userOption, passOption);
        final TransMeta transMeta = loadTransformation(repository, transOption);
        // Set the servlet parameters as variables in the transformation
        // 
        String[] parameters = transMeta.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) {
                    transMeta.setVariable(parameter, values[0]);
                } else {
                    transMeta.setParameterValue(parameter, values[0]);
                }
            }
        }
        TransExecutionConfiguration transExecutionConfiguration = new TransExecutionConfiguration();
        LogLevel logLevel = LogLevel.getLogLevelForCode(levelOption);
        transExecutionConfiguration.setLogLevel(logLevel);
        TransConfiguration transConfiguration = new TransConfiguration(transMeta, transExecutionConfiguration);
        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 Trans trans = new Trans(transMeta, servletLoggingObject);
        trans.setRepository(repository);
        trans.setSocketRepository(getSocketRepository());
        getTransformationMap().addTransformation(transMeta.getName(), carteObjectId, trans, transConfiguration);
        trans.setContainerObjectId(carteObjectId);
        if (repository != null) {
            // The repository connection is open: make sure we disconnect from the repository once we
            // are done with this transformation.
            // 
            trans.addTransListener(new TransAdapter() {

                @Override
                public void transFinished(Trans trans) {
                    repository.disconnect();
                }
            });
        }
        // Pass the servlet print writer to the transformation...
        // 
        trans.setServletPrintWriter(out);
        trans.setServletReponse(response);
        trans.setServletRequest(request);
        try {
            // Execute the transformation...
            // 
            executeTrans(trans);
            String logging = KettleLogStore.getAppender().getBuffer(trans.getLogChannelId(), false).toString();
            if (trans.isFinishedOrStopped() && trans.getErrors() > 0) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.ErrorExecutingTrans", logging)));
            }
            out.flush();
        } catch (Exception executionException) {
            String logging = KettleLogStore.getAppender().getBuffer(trans.getLogChannelId(), false).toString();
            throw new KettleException(BaseMessages.getString(PKG, "ExecuteTransServlet.Error.ErrorExecutingTrans", logging), executionException);
        }
    } catch (Exception ex) {
        // When we get to this point KettleAuthenticationException has already been wrapped in an Execution Exception
        // and that in a KettleException
        Throwable kettleExceptionCause = ex.getCause();
        if (kettleExceptionCause != null && kettleExceptionCause instanceof ExecutionException) {
            Throwable executionExceptionCause = kettleExceptionCause.getCause();
            if (executionExceptionCause != null && executionExceptionCause instanceof KettleAuthenticationException) {
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.Authentication", getContextPath())));
            }
        } else if (ex.getMessage().contains(UNABLE_TO_FIND_TRANS)) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.UnableToFindTransformation", transOption)));
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleAuthenticationException(org.pentaho.di.repository.KettleAuthenticationException) TransMeta(org.pentaho.di.trans.TransMeta) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) TransConfiguration(org.pentaho.di.trans.TransConfiguration) LogLevel(org.pentaho.di.core.logging.LogLevel) TransAdapter(org.pentaho.di.trans.TransAdapter) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KettleAuthenticationException(org.pentaho.di.repository.KettleAuthenticationException) TransExecutionConfiguration(org.pentaho.di.trans.TransExecutionConfiguration) Repository(org.pentaho.di.repository.Repository) ExecutionException(java.util.concurrent.ExecutionException) Trans(org.pentaho.di.trans.Trans) PrintWriter(java.io.PrintWriter)

Example 12 with TransConfiguration

use of org.pentaho.di.trans.TransConfiguration in project pentaho-kettle by pentaho.

the class PrepareExecutionTransServlet method doGet.

/**
 *<div id="mindtouch">
 *    <h1>/kettle/prepareExec</h1>
 *    <a name="GET"></a>
 *    <h2>GET</h2>
 *    <p>Prepares previously loaded transformation for execution.
 *  Method is used for preparing previously uploaded transformation for execution by its name.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    GET /kettle/prepareExec/?name=dummy-trans2&xml=Y
 *    </pre>
 *
 *    </p>
 *    <h3>Parameters</h3>
 *    <table class="pentaho-table">
 *    <tbody>
 *    <tr>
 *      <th>name</th>
 *      <th>description</th>
 *      <th>type</th>
 *    </tr>
 *    <tr>
 *    <td>name</td>
 *    <td>Name of the transformation to be prepared for execution.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>xml</td>
 *    <td>Boolean flag which sets the output format required. Use <code>Y</code> to receive XML response.</td>
 *    <td>boolean</td>
 *    </tr>
 *    <tr>
 *    <td>id</td>
 *    <td>Carte transformation ID of the transformation to be prepared for execution.</td>
 *    <td>query, optional</td>
 *    </tr>
 *    </tbody>
 *    </table>
 *
 *  <h3>Response Body</h3>
 *
 *  <table class="pentaho-table">
 *    <tbody>
 *      <tr>
 *        <td align="right">text:</td>
 *        <td>HTML</td>
 *      </tr>
 *      <tr>
 *        <td align="right">media types:</td>
 *        <td>text/xml, text/html</td>
 *      </tr>
 *    </tbody>
 *  </table>
 *    <p>Response XML or HTML containing operation result. When using xml=Y <code>result</code> field indicates whether
 *  operation was successful (<code>OK</code>) or not (<code>ERROR</code>).</p>
 *
 *    <p><b>Example Response:</b></p>
 *  <pre function="syntax.xml">
 *  <?xml version="1.0" encoding="UTF-8"?>
 *  <webresult>
 *    <result>OK</result>
 *    <message/>
 *    <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, "PrepareExecutionTransServlet.TransPrepareExecutionRequested"));
    }
    String transName = request.getParameter("name");
    String id = request.getParameter("id");
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter out = response.getWriter();
    if (useXML) {
        response.setContentType("text/xml");
        out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
    } else {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>" + BaseMessages.getString(PKG, "PrepareExecutionTransServlet.TransPrepareExecution") + "</TITLE>");
        out.println("<META http-equiv=\"Refresh\" content=\"2;url=" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(transName, "UTF-8") + "\">");
        out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("</HEAD>");
        out.println("<BODY>");
    }
    try {
        // ID is optional...
        // 
        Trans trans;
        CarteObjectEntry entry;
        if (Utils.isEmpty(id)) {
            // get the first transformation that matches...
            // 
            entry = getTransformationMap().getFirstCarteObjectEntry(transName);
            if (entry == null) {
                trans = null;
            } else {
                id = entry.getId();
                trans = getTransformationMap().getTransformation(entry);
            }
        } else {
            // Take the ID into account!
            // 
            entry = new CarteObjectEntry(transName, id);
            trans = getTransformationMap().getTransformation(entry);
        }
        TransConfiguration transConfiguration = getTransformationMap().getConfiguration(entry);
        if (trans != null && transConfiguration != null) {
            TransExecutionConfiguration executionConfiguration = transConfiguration.getTransExecutionConfiguration();
            // Set the appropriate logging, variables, arguments, replay date, ...
            // etc.
            trans.setArguments(executionConfiguration.getArgumentStrings());
            trans.setReplayDate(executionConfiguration.getReplayDate());
            trans.setSafeModeEnabled(executionConfiguration.isSafeModeEnabled());
            trans.setGatheringMetrics(executionConfiguration.isGatheringMetrics());
            trans.injectVariables(executionConfiguration.getVariables());
            trans.setPreviousResult(executionConfiguration.getPreviousResult());
            trans.setExecutingClustered(executionConfiguration.isExecutingClustered());
            try {
                trans.prepareExecution(null);
                cache.remove(trans.getLogChannelId());
                if (useXML) {
                    out.println(WebResult.OK.getXML());
                } else {
                    out.println("<H1>" + Encode.forHtml(BaseMessages.getString(PKG, "PrepareExecutionTransServlet.TransPrepared", transName)) + "</H1>");
                    out.println("<a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToTransStatusPage") + "</a><p>");
                }
            } catch (Exception e) {
                String logText = KettleLogStore.getAppender().getBuffer(trans.getLogChannel().getLogChannelId(), true).toString();
                if (useXML) {
                    out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "PrepareExecutionTransServlet.Error.TransInitFailed", Const.CR + logText + Const.CR + Const.getStackTracker(e))));
                } else {
                    out.println("<H1>" + Encode.forHtml(BaseMessages.getString(PKG, "PrepareExecutionTransServlet.Log.TransNotInit", transName)) + "</H1>");
                    out.println("<pre>");
                    out.println(Encode.forHtml(logText));
                    out.println(Encode.forHtml(Const.getStackTracker(e)));
                    out.println("</pre>");
                    out.println("<a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToTransStatusPage") + "</a><p>");
                    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                }
            }
        } else {
            if (useXML) {
                out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "TransStatusServlet.Log.CoundNotFindSpecTrans", transName)));
            } else {
                out.println("<H1>" + Encode.forHtml(BaseMessages.getString(PKG, "TransStatusServlet.Log.CoundNotFindTrans", transName)) + "</H1>");
                out.println("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    } catch (Exception ex) {
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "PrepareExecutionTransServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
        } else {
            out.println("<p>");
            out.println("<pre>");
            out.println(Encode.forHtml(Const.getStackTracker(ex)));
            out.println("</pre>");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
    if (!useXML) {
        out.println("<p>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}
Also used : TransExecutionConfiguration(org.pentaho.di.trans.TransExecutionConfiguration) Trans(org.pentaho.di.trans.Trans) TransConfiguration(org.pentaho.di.trans.TransConfiguration) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 13 with TransConfiguration

use of org.pentaho.di.trans.TransConfiguration in project pentaho-kettle by pentaho.

the class RegisterTransServlet method generateBody.

@Override
WebResult generateBody(HttpServletRequest request, HttpServletResponse response, boolean useXML) throws IOException, KettleException {
    final String xml = IOUtils.toString(request.getInputStream());
    try {
        // Parse the XML, create a transformation configuration
        validateTransformation(new ByteArrayInputStream(xml.getBytes()));
        TransConfiguration transConfiguration = TransConfiguration.fromXML(xml);
        Trans trans = createTrans(transConfiguration);
        String message = "Transformation '" + trans.getName() + "' was added to Carte with id " + trans.getContainerObjectId();
        return new WebResult(WebResult.STRING_OK, message, trans.getContainerObjectId());
    } catch (KettleXMLException | SAXException ex) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new WebResult(WebResult.STRING_ERROR, ex.getMessage(), "");
    } catch (Exception ex) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return new WebResult(WebResult.STRING_ERROR, ex.getMessage(), "");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) TransConfiguration(org.pentaho.di.trans.TransConfiguration) Trans(org.pentaho.di.trans.Trans) XPathExpressionException(javax.xml.xpath.XPathExpressionException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 14 with TransConfiguration

use of org.pentaho.di.trans.TransConfiguration in project pentaho-kettle by pentaho.

the class AddTransServlet method addTransformation.

private void addTransformation(HttpServletRequest request, HttpServletResponse response, boolean useXML, PrintWriter out, BufferedReader in) {
    String realLogFilename = null;
    TransExecutionConfiguration transExecutionConfiguration = null;
    try {
        // First read the complete transformation in memory from the request
        // 
        StringBuilder xml = new StringBuilder(request.getContentLength());
        int c;
        while ((c = in.read()) != -1) {
            xml.append((char) c);
        }
        // Parse the XML, create a transformation configuration
        // 
        validateTransformation(new ByteArrayInputStream(xml.toString().getBytes()));
        TransConfiguration transConfiguration = TransConfiguration.fromXML(xml.toString());
        TransMeta transMeta = transConfiguration.getTransMeta();
        transExecutionConfiguration = transConfiguration.getTransExecutionConfiguration();
        transMeta.setLogLevel(transExecutionConfiguration.getLogLevel());
        if (log.isDetailed()) {
            logDetailed("Logging level set to " + log.getLogLevel().getDescription());
        }
        transMeta.injectVariables(transExecutionConfiguration.getVariables());
        // Also copy the parameters over...
        // 
        Map<String, String> params = transExecutionConfiguration.getParams();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            transMeta.setParameterValue(entry.getKey(), entry.getValue());
        }
        // If there was a repository, we know about it at this point in time.
        // 
        final Repository repository = transExecutionConfiguration.getRepository();
        String carteObjectId = UUID.randomUUID().toString();
        SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
        servletLoggingObject.setContainerObjectId(carteObjectId);
        servletLoggingObject.setLogLevel(transExecutionConfiguration.getLogLevel());
        // Create the transformation and store in the list...
        // 
        final Trans trans = new Trans(transMeta, servletLoggingObject);
        if (transExecutionConfiguration.isSetLogfile()) {
            realLogFilename = transExecutionConfiguration.getLogFileName();
            setupLogChannelWriter(realLogFilename, transExecutionConfiguration, servletLoggingObject, trans);
        }
        trans.setRepository(repository);
        trans.setSocketRepository(getSocketRepository());
        getTransformationMap().addTransformation(transMeta.getName(), carteObjectId, trans, transConfiguration);
        trans.setContainerObjectId(carteObjectId);
        if (repository != null) {
            // The repository connection is open: make sure we disconnect from the repository once we
            // are done with this transformation.
            // 
            trans.addTransListener(new TransAdapter() {

                @Override
                public void transFinished(Trans trans) {
                    repository.disconnect();
                }
            });
        }
        String message = Encode.forHtml("Transformation '" + trans.getName() + "' was added to Carte with id " + carteObjectId);
        if (useXML) {
            // Return the log channel id as well
            // 
            out.println(new WebResult(WebResult.STRING_OK, message, carteObjectId));
        } else {
            out.println("<H1>" + message + "</H1>");
            out.println("<p><a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + Encode.forUriComponent(trans.getName()) + "&id=" + carteObjectId + "\">Go to the transformation status page</a><p>");
        }
    } catch (KettleXMLException | SAXException ex) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        printError(useXML, out, ex);
    } catch (Exception ex) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        printError(useXML, out, ex);
    }
}
Also used : TransMeta(org.pentaho.di.trans.TransMeta) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) TransConfiguration(org.pentaho.di.trans.TransConfiguration) TransAdapter(org.pentaho.di.trans.TransAdapter) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) TransExecutionConfiguration(org.pentaho.di.trans.TransExecutionConfiguration) Repository(org.pentaho.di.repository.Repository) ByteArrayInputStream(java.io.ByteArrayInputStream) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Map(java.util.Map) Trans(org.pentaho.di.trans.Trans)

Example 15 with TransConfiguration

use of org.pentaho.di.trans.TransConfiguration in project pentaho-kettle by pentaho.

the class StartTransServlet method doGet.

/**
 *  <div id="mindtouch">
 *      <h1>/kettle/startTrans</h1>
 *      <a name="GET"></a>
 *      <h2>GET</h2>
 *      <p>Executes transformation previously uploaded to Carte server.</p>
 *
 *      <p><b>Example Request:</b><br />
 *      <pre function="syntax.xml">
 *      GET /kettle/startTrans/?name=dummy-trans&xml=Y
 *      </pre>
 *
 *      </p>
 *      <h3>Parameters</h3>
 *      <table class="pentaho-table">
 *      <tbody>
 *      <tr>
 *        <th>name</th>
 *        <th>description</th>
 *        <th>type</th>
 *      </tr>
 *      <tr>
 *      <td>name</td>
 *      <td>Name of the transformation to be executed.</td>
 *      <td>query</td>
 *      </tr>
 *      <tr>
 *      <td>xml</td>
 *      <td>Boolean flag which sets the output format required. Use <code>Y</code> to receive XML response.</td>
 *      <td>boolean, optional</td>
 *      </tr>
 *      <tr>
 *      <td>id</td>
 *      <td>Carte transformation ID of the transformation to be executed. This parameter is optional when xml=Y is used.</td>
 *      <td>query, optional</td>
 *      </tr>
 *      </tbody>
 *      </table>
 *
 *    <h3>Response Body</h3>
 *
 *    <table class="pentaho-table">
 *      <tbody>
 *        <tr>
 *          <td align="right">text:</td>
 *          <td>HTML</td>
 *        </tr>
 *        <tr>
 *          <td align="right">media types:</td>
 *          <td>text/xml, text/html</td>
 *        </tr>
 *      </tbody>
 *    </table>
 *    <p>Response XML or HTML containing operation result. When using xml=Y <code>result</code> field indicates whether
 *    operation was successful (<code>OK</code>) or not (<code>ERROR</code>).</p>
 *
 *      <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <webresult>
 *      <result>OK</result>
 *      <message>Transformation &#x5b;dummy-trans&#x5d; was started.</message>
 *      <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, "StartTransServlet.Log.StartTransRequested"));
    }
    String transName = request.getParameter("name");
    String id = request.getParameter("id");
    if (StringUtils.isEmpty(transName)) {
        transName = "";
    }
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter out = response.getWriter();
    if (useXML) {
        response.setContentType("text/xml");
        response.setCharacterEncoding(Const.XML_ENCODING);
        out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
    } else {
        response.setContentType("text/html;charset=UTF-8");
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>" + BaseMessages.getString(PKG, "StartTransServlet.Log.StartOfTrans") + "</TITLE>");
        out.println("<META http-equiv=\"Refresh\" content=\"2;url=" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(transName, "UTF-8") + "\">");
        out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("</HEAD>");
        out.println("<BODY>");
    }
    try {
        // ID is optional...
        // 
        Trans trans;
        CarteObjectEntry entry;
        if (Utils.isEmpty(id)) {
            // get the first transformation that matches...
            // 
            entry = getTransformationMap().getFirstCarteObjectEntry(transName);
            if (entry == null) {
                trans = null;
            } else {
                id = entry.getId();
                trans = getTransformationMap().getTransformation(entry);
            }
        } else {
            // Take the ID into account!
            // 
            entry = new CarteObjectEntry(transName, id);
            trans = getTransformationMap().getTransformation(entry);
        }
        try {
            getTransformationMap().deallocateServerSocketPorts(transName, id);
        } catch (Exception e) {
        // nothing to do
        }
        if (trans != null) {
            TransConfiguration transConfiguration = getTransformationMap().getConfiguration(entry);
            if (transConfiguration != null) {
                TransExecutionConfiguration executionConfiguration = transConfiguration.getTransExecutionConfiguration();
                // Set the appropriate logging, variables, arguments, replay date, ...
                // etc.
                trans.setArguments(executionConfiguration.getArgumentStrings());
                trans.setReplayDate(executionConfiguration.getReplayDate());
                trans.setSafeModeEnabled(executionConfiguration.isSafeModeEnabled());
                trans.setGatheringMetrics(executionConfiguration.isGatheringMetrics());
                trans.injectVariables(executionConfiguration.getVariables());
                trans.setPreviousResult(executionConfiguration.getPreviousResult());
            }
            cache.remove(trans.getLogChannelId());
            trans.cleanup();
            // Discard old log lines from old transformation runs
            // 
            KettleLogStore.discardLines(trans.getLogChannelId(), true);
            String carteObjectId = UUID.randomUUID().toString();
            SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
            servletLoggingObject.setContainerObjectId(carteObjectId);
            servletLoggingObject.setLogLevel(trans.getLogLevel());
            trans.setParent(servletLoggingObject);
            executeTrans(trans);
            String message = BaseMessages.getString(PKG, "StartTransServlet.Log.TransStarted", transName);
            if (useXML) {
                out.println(new WebResult(WebResult.STRING_OK, message).getXML());
            } else {
                out.println("<H1>" + Encode.forHtml(message) + "</H1>");
                out.println("<a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
            }
        } else {
            String message = BaseMessages.getString(PKG, "TransStatusServlet.Log.CoundNotFindSpecTrans", transName);
            if (useXML) {
                out.println(new WebResult(WebResult.STRING_ERROR, message, id));
            } else {
                out.println("<H1>" + Encode.forHtml(message) + "</H1>");
                out.println("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    } catch (Exception ex) {
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "StartTransServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
        } else {
            out.println("<p>");
            out.println("<pre>");
            out.println(Encode.forHtml(Const.getStackTracker(ex)));
            out.println("</pre>");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
    if (!useXML) {
        out.println("<p>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}
Also used : TransExecutionConfiguration(org.pentaho.di.trans.TransExecutionConfiguration) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) Trans(org.pentaho.di.trans.Trans) TransConfiguration(org.pentaho.di.trans.TransConfiguration) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

TransConfiguration (org.pentaho.di.trans.TransConfiguration)15 TransExecutionConfiguration (org.pentaho.di.trans.TransExecutionConfiguration)13 Trans (org.pentaho.di.trans.Trans)12 IOException (java.io.IOException)8 KettleException (org.pentaho.di.core.exception.KettleException)8 TransMeta (org.pentaho.di.trans.TransMeta)8 PrintWriter (java.io.PrintWriter)6 ServletException (javax.servlet.ServletException)6 SimpleLoggingObject (org.pentaho.di.core.logging.SimpleLoggingObject)6 Repository (org.pentaho.di.repository.Repository)4 Map (java.util.Map)3 TransAdapter (org.pentaho.di.trans.TransAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 Test (org.junit.Test)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)2