org.pentaho.di.www.PrepareExecutionTransServlet Maven / Gradle / Ivy
Show all versions of kettle-engine Show documentation
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2018 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.www;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.owasp.encoder.Encode;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransConfiguration;
import org.pentaho.di.trans.TransExecutionConfiguration;
import org.pentaho.di.www.cache.CarteStatusCache;
public class PrepareExecutionTransServlet extends BaseHttpServlet implements CartePluginInterface {
private static Class> PKG = PrepareExecutionTransServlet.class; // for i18n purposes, needed by Translator2!!
private static final long serialVersionUID = 3634806745372015720L;
public static final String CONTEXT_PATH = "/kettle/prepareExec";
private CarteStatusCache cache = CarteStatusCache.getInstance();
public PrepareExecutionTransServlet() {
}
public PrepareExecutionTransServlet( TransformationMap transformationMap ) {
super( transformationMap );
}
/**
/kettle/prepareExec
GET
Prepares previously loaded transformation for execution.
Method is used for preparing previously uploaded transformation for execution by its name.
Example Request:
GET /kettle/prepareExec/?name=dummy-trans2&xml=Y
Parameters
name
description
type
name
Name of the transformation to be prepared for execution.
query
xml
Boolean flag which sets the output format required. Use Y
to receive XML response.
boolean
id
Carte transformation ID of the transformation to be prepared for execution.
query, optional
Response Body
text:
HTML
media types:
text/xml, text/html
Response XML or HTML containing operation result. When using xml=Y result
field indicates whether
operation was successful (OK
) or not (ERROR
).
Example Response:
OK
Status Codes
code
description
200
Request was processed.
500
Internal server error occurs during request processing.
*/
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( "" );
out.println( "" );
out.println( ""
+ BaseMessages.getString( PKG, "PrepareExecutionTransServlet.TransPrepareExecution" ) + " " );
out.println( "" );
out.println( "" );
out.println( "" );
out.println( "" );
}
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() );
try {
trans.prepareExecution( null );
cache.remove( trans.getLogChannelId() );
if ( useXML ) {
out.println( WebResult.OK.getXML() );
} else {
out.println( ""
+ Encode.forHtml( BaseMessages.getString(
PKG, "PrepareExecutionTransServlet.TransPrepared", transName ) ) + "
" );
out.println( ""
+ BaseMessages.getString( PKG, "TransStatusServlet.BackToTransStatusPage" ) + "" );
}
} 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( "
"
+ Encode.forHtml( BaseMessages.getString(
PKG, "PrepareExecutionTransServlet.Log.TransNotInit", transName ) ) + "
" );
out.println( "" );
out.println( Encode.forHtml( logText ) );
out.println( Encode.forHtml( Const.getStackTracker( e ) ) );
out.println( "
" );
out.println( ""
+ BaseMessages.getString( PKG, "TransStatusServlet.BackToTransStatusPage" ) + "" );
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( "
"
+ Encode.forHtml( BaseMessages.getString(
PKG, "TransStatusServlet.Log.CoundNotFindTrans", transName ) ) + "
" );
out.println( ""
+ BaseMessages.getString( PKG, "TransStatusServlet.BackToStatusPage" ) + "" );
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( "
" );
out.println( "
" );
out.println( Encode.forHtml( Const.getStackTracker( ex ) ) );
out.println( "
" );
response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
}
}
if ( !useXML ) {
out.println( "" );
out.println( "" );
out.println( "" );
}
}
public String toString() {
return "Start transformation";
}
public String getService() {
return CONTEXT_PATH + " (" + toString() + ")";
}
public String getContextPath() {
return CONTEXT_PATH;
}
}