Skip to content

Commit

Permalink
[PDI-13072]: added matrix parameters support in REST
Browse files Browse the repository at this point in the history
[PDI-13072]: unit test is added

The origin PR: pentaho#817 has conflicts on merge.

Conflicts:
	engine/src/org/pentaho/di/trans/steps/rest/Rest.java
	ivy.xml

So this PR contains the changes from PR pentaho#817 with resolved conflicts.
  • Loading branch information
Vladimir Dolzhenko authored and TatsianaKasiankova committed Aug 31, 2015
1 parent f883ae4 commit d5953c6
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 55 deletions.
41 changes: 39 additions & 2 deletions engine/src/org/pentaho/di/trans/steps/rest/Rest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Arrays;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManagerFactory;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.apache.commons.httpclient.auth.AuthScope;
import org.pentaho.di.core.Const;
Expand Down Expand Up @@ -91,6 +93,8 @@ private Object[] callRest( Object[] rowData ) throws KettleException {
}
}



WebResource webResource = null;

Client client = null;
Expand All @@ -112,14 +116,27 @@ private Object[] callRest( Object[] rowData ) throws KettleException {
// used for calculating the responseTime
long startTime = System.currentTimeMillis();

if ( data.useMatrixParams ) {
// Add matrix parameters
UriBuilder builder = webResource.getUriBuilder();
for ( int i = 0; i < data.nrMatrixParams; i++ ) {
String value = data.inputRowMeta.getString( rowData, data.indexOfMatrixParamFields[i] );
if ( isDebug() ) {
logDebug( BaseMessages.getString( PKG, "Rest.Log.matrixParameterValue", data.matrixParamNames[i], value ) );
}
builder = builder.matrixParam( data.matrixParamNames[i], value );
}
webResource = client.resource( builder.build() );
}

if ( data.useParams ) {
// Add parameters
// Add query parameters
for ( int i = 0; i < data.nrParams; i++ ) {
MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
String value = data.inputRowMeta.getString( rowData, data.indexOfParamFields[i] );
queryParams.add( data.paramNames[i], value );
if ( isDebug() ) {
logDebug( BaseMessages.getString( PKG, "Rest.Log.parameterValue", data.paramNames[i], value ) );
logDebug( BaseMessages.getString( PKG, "Rest.Log.queryParameterValue", data.paramNames[i], value ) );
}
webResource = webResource.queryParams( queryParams );
}
Expand Down Expand Up @@ -302,6 +319,7 @@ public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws

Object[] r = getRow(); // Get row from input rowset & set row busy!

logBasic( Arrays.toString( r ) );
if ( r == null ) {
// no more input to be expected...
setOutputDone();
Expand Down Expand Up @@ -394,6 +412,25 @@ public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws
}
}

int nrmatrixmatrixparams = meta.getMatrixParameterField() == null ? 0 : meta.getMatrixParameterField().length;
if ( nrmatrixmatrixparams > 0 ) {
data.nrMatrixParams = nrmatrixmatrixparams;
data.matrixParamNames = new String[nrmatrixmatrixparams];
data.indexOfMatrixParamFields = new int[nrmatrixmatrixparams];
for ( int i = 0; i < nrmatrixmatrixparams; i++ ) {
data.matrixParamNames[i] = environmentSubstitute( meta.getMatrixParameterName()[i] );
String field = environmentSubstitute( meta.getMatrixParameterField()[i] );
if ( Const.isEmpty( field ) ) {
throw new KettleException( BaseMessages.getString( PKG, "Rest.Exception.MatrixParamFieldEmpty" ) );
}
data.indexOfMatrixParamFields[i] = data.inputRowMeta.indexOfValue( field );
if ( data.indexOfMatrixParamFields[i] < 0 ) {
throw new KettleException( BaseMessages.getString( PKG, "Rest.Exception.ErrorFindingField", field ) );
}
}
data.useMatrixParams = true;
}

// Do we need to set body
if ( RestMeta.isActiveBody( meta.getMethod() ) ) {
String field = environmentSubstitute( meta.getBodyField() );
Expand Down
14 changes: 12 additions & 2 deletions engine/src/org/pentaho/di/trans/steps/rest/RestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ public class RestData extends BaseStepData implements StepDataInterface {
/** Headers **/
public int[] indexOfHeaderFields;
public String[] headerNames;
/** parameters **/

/** query parameters **/
public int nrParams;
public int[] indexOfParamFields;
public String[] paramNames;

/** matrix parameters **/
public int nrMatrixParams;
public int[] indexOfMatrixParamFields;
public String[] matrixParamNames;

/** proxy **/
public String realProxyHost;
public int realProxyPort;
Expand All @@ -71,9 +77,12 @@ public class RestData extends BaseStepData implements StepDataInterface {
/** Flag set headers **/
public boolean useHeaders;

/** Flag set Parameters **/
/** Flag set Query Parameters **/
public boolean useParams;

/** Flag set Matrix Parameters **/
public boolean useMatrixParams;

/** Flag set body **/
public boolean useBody;

Expand Down Expand Up @@ -103,6 +112,7 @@ public RestData() {
this.resultResponseFieldName = null;
this.nrheader = 0;
this.nrParams = 0;
this.nrMatrixParams = 0;
this.method = null;
this.indexOfBodyField = -1;
this.indexOfMethod = -1;
Expand Down
Loading

0 comments on commit d5953c6

Please sign in to comment.