Skip to content

Commit

Permalink
TEIID-2800 fix for url encoding and user/password with TeiidDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Feb 5, 2014
1 parent 079983b commit da5f40f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
13 changes: 9 additions & 4 deletions client/src/main/java/org/teiid/jdbc/JDBCURL.java
Expand Up @@ -24,6 +24,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -209,10 +210,14 @@ public String getJDBCURL() {
Map.Entry entry = (Map.Entry)i.next();
if (entry.getValue() instanceof String) {
// get only the string properties, because a non-string property could not have been set on the url.
buf.append(';')
.append(entry.getKey())
.append('=')
.append(entry.getValue());
try {
buf.append(';')
.append(entry.getKey())
.append('=')
.append(URLEncoder.encode((String)entry.getValue(), "UTF-8")); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
buf.append(entry.getValue());
}
}
}
urlString = buf.toString();
Expand Down
17 changes: 11 additions & 6 deletions client/src/main/java/org/teiid/jdbc/TeiidDataSource.java
Expand Up @@ -114,10 +114,15 @@ public class TeiidDataSource extends BaseDataSource {
*/
private boolean encryptRequests;

private final TeiidDriver driver = new TeiidDriver();
private final TeiidDriver driver;

public TeiidDataSource() {
}
public TeiidDataSource() {
this.driver = new TeiidDriver();
}

TeiidDataSource(TeiidDriver driver) {
this.driver = driver;
}

// --------------------------------------------------------------------------------------------
// H E L P E R M E T H O D S
Expand Down Expand Up @@ -246,14 +251,14 @@ public Connection getConnection(String userName, String password) throws java.sq
if (getServerName() == null) {
super.validateProperties(userName, password);
final Properties props = buildEmbeddedProperties(userName, password);
String url = new JDBCURL(getDatabaseName(), null, props).getJDBCURL();
String url = new JDBCURL(getDatabaseName(), null, null).getJDBCURL();
return driver.connect(url, props);
}

// if not proceed with socket connection.
validateProperties(userName,password);

return driver.connect(buildURL().getJDBCURL(), null);
final Properties props = buildProperties(userName, password);
return driver.connect(new JDBCURL(this.getDatabaseName(), buildServerURL(), null).getJDBCURL(), props);
}

private Properties buildEmbeddedProperties(final String userName, final String password) {
Expand Down
5 changes: 3 additions & 2 deletions client/src/test/java/org/teiid/jdbc/TestJDBCURL.java
Expand Up @@ -45,11 +45,11 @@ public final void testCredentials() throws Exception {
}

public void testJDBCURLWithProperties() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password=***;logLevel=1;configFile=testdata/bqt/dqp_stmt_e2e.xmi;disableLocalTxn=true;autoFailover=false"; //$NON-NLS-1$
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=%25xyz;password=***;logLevel=1;configFile=testdata/bqt/dqp_stmt_e2e.xmi;disableLocalTxn=true;autoFailover=false"; //$NON-NLS-1$

Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "%xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("logLevel", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", "testdata/bqt/dqp_stmt_e2e.xmi"); //$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -59,6 +59,7 @@ public void testJDBCURLWithProperties() {
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
assertTrue(url.getJDBCURL().contains("user=%25xyz"));
}

public void testJDBCURLWithoutProperties() {
Expand Down
31 changes: 31 additions & 0 deletions client/src/test/java/org/teiid/jdbc/TestTeiidDataSource.java
Expand Up @@ -30,6 +30,8 @@

import junit.framework.TestCase;

import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.teiid.client.RequestMessage.ShowPlan;
import org.teiid.core.util.UnitTestUtil;

Expand Down Expand Up @@ -709,4 +711,33 @@ public void testInvalidDataSource2() {
// this is expected!
}
}

public void testUrlEncodedProperties() throws SQLException {
TeiidDriver td = Mockito.mock(TeiidDriver.class);
TeiidDataSource tds = new TeiidDataSource(td);
tds.setDatabaseName("y");
tds.setUser("%25user");
tds.setServerName("x");
tds.getConnection();

ArgumentCaptor<Properties> argument = ArgumentCaptor.forClass(Properties.class);
Mockito.verify(td).connect(Mockito.eq("jdbc:teiid:y@mm://x:0"), argument.capture());
Properties p = argument.getValue();
assertEquals("%25user", p.getProperty(BaseDataSource.USER_NAME));
}

public void testGetConnectionWithUser() throws SQLException {
TeiidDriver td = Mockito.mock(TeiidDriver.class);
TeiidDataSource tds = new TeiidDataSource(td);
tds.setDatabaseName("y");
tds.setUser("%25user");
tds.setServerName("x");
tds.getConnection("user", "password");

ArgumentCaptor<Properties> argument = ArgumentCaptor.forClass(Properties.class);
Mockito.verify(td).connect(Mockito.eq("jdbc:teiid:y@mm://x:0"), argument.capture());
Properties p = argument.getValue();
assertEquals("user", p.getProperty(BaseDataSource.USER_NAME));
}

}

0 comments on commit da5f40f

Please sign in to comment.