From 2b63c8cb146d910f037c41c99983cad09dde9855 Mon Sep 17 00:00:00 2001 From: Benjamin Morrise Date: Wed, 26 Aug 2015 09:39:45 -0400 Subject: [PATCH] [BACKLOG-4587] Relevant errors for data service connection issues --- core/src/org/pentaho/di/cluster/HttpUtil.java | 102 +++++++++++++----- .../org/pentaho/di/core/sql/SQLCondition.java | 8 +- 2 files changed, 83 insertions(+), 27 deletions(-) diff --git a/core/src/org/pentaho/di/cluster/HttpUtil.java b/core/src/org/pentaho/di/cluster/HttpUtil.java index 334e1e2fa67c..f172709f7709 100644 --- a/core/src/org/pentaho/di/cluster/HttpUtil.java +++ b/core/src/org/pentaho/di/cluster/HttpUtil.java @@ -26,11 +26,12 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.util.List; +import java.util.Map; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -39,8 +40,10 @@ import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.lang.StringUtils; import org.pentaho.di.core.Const; import org.pentaho.di.core.encryption.Encr; @@ -51,44 +54,91 @@ public class HttpUtil { public static final int ZIP_BUFFER_SIZE = 8192; - public static String execService( VariableSpace space, String hostname, String port, String webAppName, - String serviceAndArguments, String username, String password, String proxyHostname, String proxyPort, - String nonProxyHosts ) throws Exception { - // Prepare HTTP get - // + private static HttpClient getClient( VariableSpace space, String hostname, String port, String webAppName, + String username, String password, String proxyHostname, String proxyPort, String nonProxyHosts ) { + HttpClient client = SlaveConnectionManager.getInstance().createHttpClient(); addCredentials( client, space, hostname, port, webAppName, username, password ); addProxy( client, space, hostname, proxyHostname, proxyPort, nonProxyHosts ); + + return client; + } + + public static PostMethod execService( VariableSpace space, String hostname, String port, String webAppName, + String urlString, String username, String password, String proxyHostname, String proxyPort, + String nonProxyHosts, Iterable
headers, Map parameters, Map arguments ) + throws Exception { + + HttpClient + client = + getClient( space, hostname, port, webAppName, username, password, proxyHostname, proxyPort, nonProxyHosts ); + + client.getHttpConnectionManager().getParams().setConnectionTimeout( 0 ); + client.getHttpConnectionManager().getParams().setSoTimeout( 0 ); + + PostMethod method = new PostMethod( urlString ); + + method.setDoAuthentication( true ); + + for ( Header header : headers ) { + method.addRequestHeader( header ); + } + + for ( Map.Entry parameter : parameters.entrySet() ) { + method.getParams().setParameter( parameter.getKey(), parameter.getValue() ); + } + + for ( Map.Entry arg : arguments.entrySet() ) { + method.addParameter( arg.getKey(), arg.getValue() ); + } + + execMethod( client, method ); + return method; + } + + public static String execService( VariableSpace space, String hostname, String port, String webAppName, + String serviceAndArguments, String username, String password, String proxyHostname, String proxyPort, + String nonProxyHosts ) throws Exception { + + HttpClient + client = + getClient( space, hostname, port, webAppName, username, password, proxyHostname, proxyPort, nonProxyHosts ); + String urlString = constructUrl( space, hostname, port, webAppName, serviceAndArguments ); HttpMethod method = new GetMethod( urlString ); - // Execute request - // - InputStream inputStream = null; - BufferedInputStream bufferedInputStream = null; + try { + execMethod( client, method ); + return method.getResponseBodyAsString(); + } finally { + method.releaseConnection(); + } + + } + public static int execMethod( HttpClient client, HttpMethod method ) throws Exception { + int result; try { - int result = client.executeMethod( method ); - if ( result != 200 ) { - throw new KettleException( "Response code " + result + " received while querying " + urlString ); - } + result = client.executeMethod( method ); + } catch ( Exception e ) { + throw new KettleException( + "You don't seem to be getting a connection to the server. Check the host and port you're using and make sure the sever is up and running." ); + } - // the response - String body = method.getResponseBodyAsString(); + if ( result == 500 ) { + throw new KettleException( "There was an error reading data from the server." ); + } - return body; - } finally { - if ( bufferedInputStream != null ) { - bufferedInputStream.close(); - } - if ( inputStream != null ) { - inputStream.close(); - } + if ( result == 401 ) { + throw new KettleException( + "Nice try-but we couldn't log you in. Check your username and password and try again." ); + } - // Release current connection to the connection pool once you are done - method.releaseConnection(); + if ( result != 200 ) { + throw new KettleException( method.getResponseBodyAsString() ); } + return result; } /** diff --git a/core/src/org/pentaho/di/core/sql/SQLCondition.java b/core/src/org/pentaho/di/core/sql/SQLCondition.java index 9e714b60ed2b..c16914ed9118 100644 --- a/core/src/org/pentaho/di/core/sql/SQLCondition.java +++ b/core/src/org/pentaho/di/core/sql/SQLCondition.java @@ -250,7 +250,13 @@ private Condition parseAtomicCondition( String clause ) throws KettleSQLExceptio "Unfortunately support for conditions is still very rudimentary, only 1 simple condition is supported [" + clause + "]" ); } - String left = strings.get( 0 ); + String left = ""; + try { + left = strings.get( 0 ); + } catch ( Exception e ) { + throw new KettleSQLException( "Invalid SQL statement [" + clause + "]" ); + } + // See if this is not a having clause expression : // example: