Skip to content

Commit c2284e7

Browse files
socphillipross
andauthored
Treat unsupported URLs as specified in Driver#connect to avoid overwriting the "real" exception (#115)
* Treat unsupported URLs as specified in `Driver#connect` to avoid overwriting the "real" exception * Drop documentation on URL being changed * - FIX: connect() method modification to retain "mangled" url for connection - FIX: acceptsURL() method rework to avoid potentially invoking super method with null param Signed-off-by: Phillip Ross <[email protected]> --------- Signed-off-by: Phillip Ross <[email protected]> Co-authored-by: Phillip Ross <[email protected]>
1 parent f6ac473 commit c2284e7

File tree

5 files changed

+71
-63
lines changed

5 files changed

+71
-63
lines changed

postgis-jdbc-java2d/src/main/java/net/postgis/jdbc/java2d/Java2DWrapper.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
106106
* Mangles the PostGIS URL to return the original PostGreSQL URL
107107
*
108108
* @param url String containing the url to be "mangled"
109-
* @return "mangled" string
110-
* @throws SQLException when a SQLException occurs
109+
* @return "mangled" string or null if the URL is unsupported
111110
*/
112-
public static String mangleURL(String url) throws SQLException {
113-
if (url.startsWith(POSTGIS_PROTOCOL)) {
114-
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
115-
} else {
116-
throw new SQLException("Unknown protocol or subprotocol in url " + url);
117-
}
111+
public static String mangleURL(String url) {
112+
return url.startsWith(POSTGIS_PROTOCOL)
113+
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
114+
: null;
118115
}
119116

120117
/**
@@ -128,12 +125,8 @@ public static String mangleURL(String url) throws SQLException {
128125
* @return true if this driver accepts the given URL
129126
*/
130127
public boolean acceptsURL(String url) {
131-
try {
132-
url = mangleURL(url);
133-
} catch (SQLException e) {
134-
return false;
135-
}
136-
return super.acceptsURL(url);
128+
url = mangleURL(url);
129+
return url != null && super.acceptsURL(url);
137130
}
138131

139132
/**

postgis-jdbc-jts/src/main/java/net/postgis/jdbc/jts/JtsGisWrapper.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
100100
* Mangles the PostGIS URL to return the original PostGreSQL URL
101101
*
102102
* @param url String containing the url to be "mangled"
103-
* @return "mangled" string
104-
* @throws SQLException when a SQLException occurs
103+
* @return "mangled" string or null if the URL is unsupported
105104
*/
106-
public static String mangleURL(String url) throws SQLException {
107-
if (url.startsWith(POSTGIS_PROTOCOL)) {
108-
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
109-
} else {
110-
throw new SQLException("Unknown protocol or subprotocol in url " + url);
111-
}
105+
public static String mangleURL(String url) {
106+
return url.startsWith(POSTGIS_PROTOCOL)
107+
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
108+
: null;
112109
}
113110

114111
/**
@@ -122,12 +119,8 @@ public static String mangleURL(String url) throws SQLException {
122119
* @return true if this driver accepts the given URL
123120
*/
124121
public boolean acceptsURL(String url) {
125-
try {
126-
url = mangleURL(url);
127-
} catch (SQLException e) {
128-
return false;
129-
}
130-
return super.acceptsURL(url);
122+
url = mangleURL(url);
123+
return url != null && super.acceptsURL(url);
131124
}
132125

133126
/**

postgis-jdbc-jts/src/main/java/net/postgis/jdbc/jts/JtsWrapper.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,12 @@ public static void addGISTypes(PGConnection pgconn) throws SQLException {
109109
* Mangles the PostGIS URL to return the original PostGreSQL URL
110110
*
111111
* @param url String containing the url to be "mangled"
112-
* @return "mangled" string
113-
* @throws SQLException when a SQLException occurs
112+
* @return "mangled" string or null if the URL is unsupported
114113
*/
115-
public static String mangleURL(String url) throws SQLException {
116-
if (url.startsWith(POSTGIS_PROTOCOL)) {
117-
return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
118-
} else {
119-
throw new SQLException("Unknown protocol or subprotocol in url " + url);
120-
}
114+
public static String mangleURL(String url) {
115+
return url.startsWith(POSTGIS_PROTOCOL)
116+
? POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length())
117+
: null;
121118
}
122119

123120
/**
@@ -128,12 +125,8 @@ public static String mangleURL(String url) throws SQLException {
128125
* @return true if this driver accepts the given URL
129126
*/
130127
public boolean acceptsURL(String url) {
131-
try {
132-
url = mangleURL(url);
133-
} catch (SQLException e) {
134-
return false;
135-
}
136-
return super.acceptsURL(url);
128+
url = mangleURL(url);
129+
return url != null && super.acceptsURL(url);
137130
}
138131

139132
/**

postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,20 @@ private static TypesAdder loadTypesAdder(final String version) throws SQLExcepti
145145
/**
146146
* Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes().
147147
*
148-
* A side-effect of this method is that the specified url parameter may be be changed
149-
*
150-
* @param url the URL of the database to connect to (may be changed as a side-effect of this method)
148+
* @param url the URL of the database to connect to
151149
* @param info a list of arbitrary tag/value pairs as connection arguments
152-
* @return a connection to the URL or null if it isnt us
150+
* @return a connection to the URL or null if the driver does not support the subprotocol specified in the URL
153151
* @exception SQLException if a database access error occurs
154152
*
155153
* @see java.sql.Driver#connect
156154
* @see org.postgresql.Driver
157155
*/
158156
public java.sql.Connection connect(String url, final Properties info) throws SQLException {
159-
url = mangleURL(url);
157+
if (acceptsURL(url)) {
158+
url = mangleURL(url);
159+
} else {
160+
return null;
161+
}
160162
Connection result = super.connect(url, info);
161163
typesAdder.addGT(result, useLW(result));
162164
return result;
@@ -181,19 +183,17 @@ protected boolean useLW(final Connection result) {
181183
/**
182184
* Check whether the driver thinks he can handle the given URL.
183185
*
184-
* A side-effect of this method is that the specified url parameter may be be changed
185-
*
186186
* @see java.sql.Driver#acceptsURL
187-
* @param url the URL of the driver (may be changed as a side-effect of this method)
187+
* @param url the URL of the driver
188188
* @return true if this driver accepts the given URL
189189
*/
190190
public boolean acceptsURL(String url) {
191-
try {
192-
url = mangleURL(url);
193-
} catch (SQLException e) {
191+
String mangledURL = mangleURL(url);
192+
if (mangledURL == null) {
194193
return false;
194+
} else {
195+
return super.acceptsURL(mangledURL);
195196
}
196-
return super.acceptsURL(url);
197197
}
198198

199199

@@ -254,16 +254,13 @@ public static void addGISTypes72(final org.postgresql.PGConnection pgconn) throw
254254
* Mangles the PostGIS URL to return the original PostGreSQL URL
255255
*
256256
* @param url String containing the url to be "mangled"
257-
* @return "mangled" string
258-
* @throws SQLException when a SQLException occurs
257+
* @return "mangled" string or null if the URL is unsupported
259258
*/
260-
protected String mangleURL(final String url) throws SQLException {
261-
String myProgo = getProtoString();
262-
if (url.startsWith(myProgo)) {
263-
return POSTGRES_PROTOCOL + url.substring(myProgo.length());
264-
} else {
265-
throw new SQLException("Unknown protocol or subprotocol in url " + url);
266-
}
259+
protected String mangleURL(final String url) {
260+
String myProto = getProtoString();
261+
return url.startsWith(myProto)
262+
? POSTGRES_PROTOCOL + url.substring(myProto.length())
263+
: null;
267264
}
268265

269266

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.postgis.jdbc;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
9+
public class DriverConnectBehaviorTest {
10+
11+
@Test
12+
public void testThatPostGisDoesNotOverwriteSavedExceptionForUnsupportedConnectionString() {
13+
try {
14+
DriverManager.getConnection("jdbc:missing");
15+
} catch (SQLException e) {
16+
// This should not be "Unknown protocol or subprotocol in url jdbc:missing", which
17+
// would indicate that PostGIS threw an exception instead of returning `null` from
18+
// the `connect` method for an unsupported connection string.
19+
// (This is documented in `java.sql.Driver.connect`.)
20+
//
21+
// The former behavior is not desirable as throwing an exception causes a previously
22+
// saved exception from a "better fitting" driver to be overwritten by PostGis, despite
23+
// PostGis not actually being able to handle the connection.
24+
//
25+
// (Imagine an Oracle connection string with a wrong password, in which the Oracle
26+
// driver's exception regarding the wrong password would be replaced with a generic
27+
// nonsensical PostGis exception.)
28+
Assert.assertEquals("No suitable driver found for jdbc:missing", e.getMessage());
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)