Permalink
Browse files

First version, with no new functionnalities

  • Loading branch information...
0 parents commit 9e47dc6b49dcc1fe7c9332c236df6652b2f4df0c Bertrand Paquet committed Jul 20, 2011
@@ -0,0 +1,7 @@
+bin
+*.jar
+build
+dist
+.classpath
+.settings
+.project
@@ -0,0 +1,46 @@
+<project default="compile">
+ <property environment="env" />
+ <property name="pdi_home" location="${env.PDI_HOME}" />
+
+ <property name="plugin_name" value="AdvancedHttp" />
+ <property name="plugin_jar" value="advanced_http.jar" />
+
+ <property name="src" location="src" />
+ <property name="build" location="build" />
+ <property name="dist" location="dist" />
+
+ <path id="pdi_classpath">
+ <fileset dir="${pdi_home}/lib">
+ <include name="*.jar" />
+ </fileset>
+ <filelist>
+ <file name="${pdi_home}/libswt/osx/swt.jar" />
+ <file name="${pdi_home}/libext/commons/commons-httpclient-3.0.1.jar" />
+ </filelist>
+ </path>
+
+ <target name="init">
+ <mkdir dir="${build}" />
+ <echo message="Using PDI_HOME : ${pdi_home}" />
+ </target>
+
+ <target name="compile" depends="init">
+ <javac srcdir="${src}" destdir="${build}">
+ <classpath refid="pdi_classpath" />
+ </javac>
+ </target>
+
+ <target name="dist" depends="compile">
+ <mkdir dir="${dist}" />
+ <copy todir="${dist}/${plugin_name}">
+ <fileset dir="plugins/${plugin_name}" />
+ </copy>
+ <jar jarfile="${dist}/${plugin_name}/${plugin_jar}" basedir="${build}"/>
+ </target>
+
+ <target name="clean">
+ <delete dir="${build}" />
+ <delete dir="${dist}" />
+ </target>
+
+</project>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<plugin
+ id="AdvancedHTTP"
+ iconfile="HTTP.png"
+ description="Advanced HTTP Plugin"
+ tooltip="Plugin for advanced http calls"
+ category="Lookup"
+ classname="com.bpa.pentaho.plugins.AdvancedHTTPMeta">
+ <libraries>
+ <library name="advanced_http.jar"/>
+ </libraries>
+
+ <localized_category>
+ <category locale="en_US">Lookup</category>
+ </localized_category>
+ <localized_description>
+ <description locale="en_US">Advanced HTTP</description>
+ </localized_description>
+ <localized_tooltip>
+ <tooltip locale="en_US">Plugin for advanced http calls</tooltip>
+ </localized_tooltip>
+
+</plugin>
@@ -0,0 +1,249 @@
+package com.bpa.pentaho.plugins;
+
+import java.io.InputStream;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.util.URIUtil;
+import org.pentaho.di.core.Const;
+import org.pentaho.di.core.exception.KettleException;
+import org.pentaho.di.core.exception.KettleStepException;
+import org.pentaho.di.core.exception.KettleValueException;
+import org.pentaho.di.core.row.RowDataUtil;
+import org.pentaho.di.core.row.RowMetaInterface;
+import org.pentaho.di.trans.Trans;
+import org.pentaho.di.trans.TransMeta;
+import org.pentaho.di.trans.step.BaseStep;
+import org.pentaho.di.trans.step.StepDataInterface;
+import org.pentaho.di.trans.step.StepInterface;
+import org.pentaho.di.trans.step.StepMeta;
+import org.pentaho.di.trans.step.StepMetaInterface;
+
+public class AdvancedHTTP extends BaseStep implements StepInterface
+{
+ private AdvancedHTTPMeta meta;
+ private AdvancedHTTPData data;
+
+ public AdvancedHTTP(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
+ {
+ super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
+ }
+
+ private Object[] execHttp(RowMetaInterface rowMeta, Object[] row) throws KettleException
+ {
+ if (first)
+ {
+ first=false;
+ data.argnrs=new int[meta.getArgumentField().length];
+
+ for (int i=0;i<meta.getArgumentField().length;i++)
+ {
+ data.argnrs[i]=rowMeta.indexOfValue(meta.getArgumentField()[i]);
+ if (data.argnrs[i]<0)
+ {
+ logError(Messages.getString("HTTP.Log.ErrorFindingField")+meta.getArgumentField()[i]+"]"); //$NON-NLS-1$ //$NON-NLS-2$
+ throw new KettleStepException(Messages.getString("HTTP.Exception.CouldnotFindField",meta.getArgumentField()[i])); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ }
+ }
+
+ return callHttpService(rowMeta, row);
+ }
+
+ private Object[] callHttpService(RowMetaInterface rowMeta, Object[] rowData) throws KettleException
+ {
+ String url = determineUrl(rowMeta, rowData);
+ try
+ {
+ if(log.isDetailed()) logDetailed(Messages.getString("HTTP.Log.Connecting",url));
+
+ // Prepare HTTP get
+ //
+ HttpClient httpclient = new HttpClient();
+ HttpMethod method = new GetMethod(environmentSubstitute(url));
+
+ // Execute request
+ //
+ try
+ {
+ int result = httpclient.executeMethod(method);
+
+ // The status code
+ if (log.isDebug()) log.logDebug(toString(), Messages.getString("HTTP.Log.ResponseStatusCode", ""+result));
+
+ // the response
+ InputStream inputStream = method.getResponseBodyAsStream();
+ StringBuffer bodyBuffer = new StringBuffer();
+ int c;
+ while ( (c=inputStream.read())!=-1) bodyBuffer.append((char)c);
+ inputStream.close();
+
+ String body = bodyBuffer.toString();
+ if (log.isDebug()) log.logDebug(toString(), "Response body: "+body);
+
+ return RowDataUtil.addValueData(rowData, rowMeta.size(), body);
+ }
+ finally
+ {
+ // Release current connection to the connection pool once you are done
+ method.releaseConnection();
+ }
+ }
+ catch(Exception e)
+ {
+ throw new KettleException(Messages.getString("HTTP.Log.UnableGetResult",url), e);
+ }
+ }
+
+ private String determineUrl(RowMetaInterface outputRowMeta, Object[] row) throws KettleValueException, KettleException
+ {
+ try
+ {
+ if(meta.isUrlInField())
+ {
+ // get dynamic url
+ data.realUrl=outputRowMeta.getString(row,data.indexOfUrlField);
+ }
+ StringBuffer url = new StringBuffer(data.realUrl); // the base URL with variable substitution
+
+ for (int i=0;i<data.argnrs.length;i++)
+ {
+ if (i==0 && url.indexOf("?")<0)
+ {
+ url.append('?');
+ }
+ else
+ {
+ url.append('&');
+ }
+
+ url.append(URIUtil.encodeWithinQuery(meta.getArgumentParameter()[i]));
+ url.append('=');
+ String s = outputRowMeta.getString(row, data.argnrs[i]);
+ if ( s != null )
+ s = URIUtil.encodeWithinQuery(s);
+ url.append(s);
+ }
+
+ return url.toString();
+ }
+ catch(Exception e)
+ {
+ throw new KettleException(Messages.getString("HTTP.Log.UnableCreateUrl"), e);
+ }
+ }
+
+ public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
+ {
+ meta=(AdvancedHTTPMeta)smi;
+ data=(AdvancedHTTPData)sdi;
+
+ boolean sendToErrorRow=false;
+ String errorMessage = null;
+
+ Object[] r=getRow(); // Get row from input rowset & set row busy!
+ if (r==null) // no more input to be expected...
+ {
+ setOutputDone();
+ return false;
+ }
+
+ if ( first )
+ {
+ data.outputRowMeta = getInputRowMeta().clone();
+ meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
+
+ if(meta.isUrlInField())
+ {
+ if(Const.isEmpty(meta.getUrlField()))
+ {
+ logError(Messages.getString("HTTP.Log.NoField"));
+ throw new KettleException(Messages.getString("HTTP.Log.NoField"));
+ }
+
+ // cache the position of the field
+ if (data.indexOfUrlField<0)
+ {
+ String realUrlfieldName=environmentSubstitute(meta.getUrlField());
+ data.indexOfUrlField =getInputRowMeta().indexOfValue(realUrlfieldName);
+ if (data.indexOfUrlField<0)
+ {
+ // The field is unreachable !
+ logError(Messages.getString("HTTP.Log.ErrorFindingField",realUrlfieldName));
+ throw new KettleException(Messages.getString("HTTP.Exception.ErrorFindingField",realUrlfieldName));
+ }
+ }
+ }else
+ {
+ data.realUrl=environmentSubstitute(meta.getUrl());
+ }
+ }
+
+ try
+ {
+ Object[] outputRowData = execHttp(getInputRowMeta(), r); // add new values to the row
+ putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s);
+
+ if (checkFeedback(getLinesRead()))
+ {
+ if(log.isDetailed()) logDetailed(Messages.getString("HTTP.LineNumber")+getLinesRead()); //$NON-NLS-1$
+ }
+ }
+ catch(KettleException e)
+ {
+ if (getStepMeta().isDoingErrorHandling())
+ {
+ sendToErrorRow = true;
+ errorMessage = e.toString();
+ }
+ else
+ {
+ logError(Messages.getString("HTTP.ErrorInStepRunning")+e.getMessage()); //$NON-NLS-1$
+ setErrors(1);
+ stopAll();
+ setOutputDone(); // signal end to receiver(s)
+ return false;
+ }
+ if (sendToErrorRow)
+ {
+ // Simply add this row to the error row
+ putError(getInputRowMeta(), r, 1, errorMessage, null, "HTTP001");
+ }
+ }
+
+ return true;
+ }
+
+ public boolean init(StepMetaInterface smi, StepDataInterface sdi)
+ {
+ meta=(AdvancedHTTPMeta)smi;
+ data=(AdvancedHTTPData)sdi;
+
+ if (super.init(smi, sdi))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ public void dispose(StepMetaInterface smi, StepDataInterface sdi)
+ {
+ meta = (AdvancedHTTPMeta)smi;
+ data = (AdvancedHTTPData)sdi;
+
+ super.dispose(smi, sdi);
+ }
+
+ public String toString()
+ {
+ return this.getClass().getName();
+ }
+
+ //
+ // Run is were the action happens!
+ public void run()
+ {
+ BaseStep.runStepThread(this, meta, data);
+ }
+}
@@ -0,0 +1,22 @@
+package com.bpa.pentaho.plugins;
+
+import org.pentaho.di.core.row.RowMetaInterface;
+import org.pentaho.di.trans.step.BaseStepData;
+import org.pentaho.di.trans.step.StepDataInterface;
+
+public class AdvancedHTTPData extends BaseStepData implements StepDataInterface
+{
+ public int argnrs[];
+ public RowMetaInterface outputRowMeta;
+ public int indexOfUrlField;
+ public String realUrl;
+
+ /**
+ * Default constructor.
+ */
+ public AdvancedHTTPData()
+ {
+ super();
+ indexOfUrlField=-1;
+ }
+}
Oops, something went wrong.

0 comments on commit 9e47dc6

Please sign in to comment.