Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Treat unsupported URLs as specified in Driver#connect to avoid over…
…writing the "real" exception
  • Loading branch information
soc authored and phillipross committed Dec 7, 2023
commit 7c1717fb47218c801bf2433b6781667a3cd893ec
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
* Mangles the PostGIS URL to return the original PostGreSQL URL
*
* @param url String containing the url to be "mangled"
* @return "mangled" string
* @throws SQLException when a SQLException occurs
* @return "mangled" string or null if the URL is unsupported
*/
public static String mangleURL(String url) throws SQLException {
if (url.startsWith(POSTGIS_PROTOCOL)) {
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
} else {
throw new SQLException("Unknown protocol or subprotocol in url " + url);
}
public static String mangleURL(String url) {
return url.startsWith(POSTGIS_PROTOCOL)
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
: null;
}

/**
Expand All @@ -128,12 +125,8 @@ public static String mangleURL(String url) throws SQLException {
* @return true if this driver accepts the given URL
*/
public boolean acceptsURL(String url) {
try {
url = mangleURL(url);
} catch (SQLException e) {
return false;
}
return super.acceptsURL(url);
url = mangleURL(url);
return url != null && super.acceptsURL(url);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
* Mangles the PostGIS URL to return the original PostGreSQL URL
*
* @param url String containing the url to be "mangled"
* @return "mangled" string
* @throws SQLException when a SQLException occurs
* @return "mangled" string or null if the URL is unsupported
*/
public static String mangleURL(String url) throws SQLException {
if (url.startsWith(POSTGIS_PROTOCOL)) {
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
} else {
throw new SQLException("Unknown protocol or subprotocol in url " + url);
}
public static String mangleURL(String url) {
return url.startsWith(POSTGIS_PROTOCOL)
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
: null;
}

/**
Expand All @@ -122,12 +119,8 @@ public static String mangleURL(String url) throws SQLException {
* @return true if this driver accepts the given URL
*/
public boolean acceptsURL(String url) {
try {
url = mangleURL(url);
} catch (SQLException e) {
return false;
}
return super.acceptsURL(url);
url = mangleURL(url);
return url != null && super.acceptsURL(url);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
* Mangles the PostGIS URL to return the original PostGreSQL URL
*
* @param url String containing the url to be "mangled"
* @return "mangled" string
* @throws SQLException when a SQLException occurs
* @return "mangled" string or null if the URL is unsupported
*/
public static String mangleURL(String url) throws SQLException {
if (url.startsWith(POSTGIS_PROTOCOL)) {
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
} else {
throw new SQLException("Unknown protocol or subprotocol in url " + url);
}
public static String mangleURL(String url) {
return url.startsWith(POSTGIS_PROTOCOL)
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
: null;
}

/**
Expand All @@ -128,12 +125,8 @@ public static String mangleURL(String url) throws SQLException {
* @return true if this driver accepts the given URL
*/
public boolean acceptsURL(String url) {
try {
url = mangleURL(url);
} catch (SQLException e) {
return false;
}
return super.acceptsURL(url);
url = mangleURL(url);
return url != null && super.acceptsURL(url);
}

/**
Expand Down
31 changes: 13 additions & 18 deletions postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,20 @@ private static TypesAdder loadTypesAdder(final String version) throws SQLExcepti
/**
* Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes().
*
* A side-effect of this method is that the specified url parameter may be be changed
* A side-effect of this method is that the specified url parameter may be changed
*
* @param url the URL of the database to connect to (may be changed as a side-effect of this method)
* @param info a list of arbitrary tag/value pairs as connection arguments
* @return a connection to the URL or null if it isnt us
* @return a connection to the URL or null if the driver does not support the subprotocol specified in the URL
* @exception SQLException if a database access error occurs
*
* @see java.sql.Driver#connect
* @see org.postgresql.Driver
*/
public java.sql.Connection connect(String url, final Properties info) throws SQLException {
url = mangleURL(url);
if (!acceptsURL(url)) {
return null;
}
Connection result = super.connect(url, info);
typesAdder.addGT(result, useLW(result));
return result;
Expand Down Expand Up @@ -188,12 +190,8 @@ protected boolean useLW(final Connection result) {
* @return true if this driver accepts the given URL
*/
public boolean acceptsURL(String url) {
try {
url = mangleURL(url);
} catch (SQLException e) {
return false;
}
return super.acceptsURL(url);
url = mangleURL(url);
return url != null && super.acceptsURL(url);
}


Expand Down Expand Up @@ -254,16 +252,13 @@ public static void addGISTypes72(final org.postgresql.PGConnection pgconn) throw
* Mangles the PostGIS URL to return the original PostGreSQL URL
*
* @param url String containing the url to be "mangled"
* @return "mangled" string
* @throws SQLException when a SQLException occurs
* @return "mangled" string or null if the URL is unsupported
*/
protected String mangleURL(final String url) throws SQLException {
String myProgo = getProtoString();
if (url.startsWith(myProgo)) {
return POSTGRES_PROTOCOL + url.substring(myProgo.length());
} else {
throw new SQLException("Unknown protocol or subprotocol in url " + url);
}
protected String mangleURL(final String url) {
String myProto = getProtoString();
return url.startsWith(myProto)
? POSTGRES_PROTOCOL + url.substring(myProto.length())
: null;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.postgis.jdbc;

import org.junit.Assert;
import org.junit.Test;

import java.sql.DriverManager;
import java.sql.SQLException;

public class DriverConnectBehaviorTest {

@Test
public void testThatPostGisDoesNotOverwriteSavedExceptionForUnsupportedConnectionString() {
try {
DriverManager.getConnection("jdbc:missing");
} catch (SQLException e) {
// This should not be "Unknown protocol or subprotocol in url jdbc:missing", which
// would indicate that PostGIS threw an exception instead of returning `null` from
// the `connect` method for an unsupported connection string.
// (This is documented in `java.sql.Driver.connect`.)
//
// The former behavior is not desirable as throwing an exception causes a previously
// saved exception from a "better fitting" driver to be overwritten by PostGis, despite
// PostGis not actually being able to handle the connection.
//
// (Imagine an Oracle connection string with a wrong password, in which the Oracle
// driver's exception regarding the wrong password would be replaced with a generic
// nonsensical PostGis exception.)
Assert.assertEquals("No suitable driver found for jdbc:missing", e.getMessage());
}
}

}