Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ See the [CONTRIBUTING guide](./CONTRIBUTING.md#Changelog) for instructions on ho
### Infrastructure
### Documentation
### Maintenance
*Remove deprecated URL(String) usage ([#795](https://github.com/opensearch-project/geospatial/pull/795))
### Refactoring
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -109,9 +110,9 @@ public ActionRequestValidationException validate() {
*/
private void validateEndpoint(final ActionRequestValidationException errors) {
try {
URL url = new URL(endpoint);
URL url = URI.create(endpoint).toURL();
url.toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException e) {
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
log.info("Invalid URL[{}] is provided", endpoint, e);
errors.addValidationError("Invalid URL format is provided");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.opensearch.geospatial.ip2geo.common.Ip2GeoLockService.LOCK_DURATION_IN_SECONDS;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Instant;
Expand Down Expand Up @@ -188,7 +189,7 @@ private void markDatasourceAsCreateFailed(final Datasource datasource) {
private void validateManifestFile(final PutDatasourceRequest request) {
DatasourceManifest manifest;
try {
URL url = new URL(request.getEndpoint());
URL url = URI.create(request.getEndpoint()).toURL();
manifest = DatasourceManifest.Builder.build(url);
} catch (Exception e) {
log.info("Error occurred while reading a file from {}", request.getEndpoint(), e);
Expand All @@ -198,8 +199,8 @@ private void validateManifestFile(final PutDatasourceRequest request) {
}

try {
new URL(manifest.getUrl()).toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException e) {
URI.create(manifest.getUrl()).toURL().toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
log.info("Invalid URL[{}] is provided for url field in the manifest file", manifest.getUrl(), e);
throw new IllegalArgumentException("Invalid URL format is provided for url field in the manifest file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

Expand Down Expand Up @@ -120,9 +121,9 @@ private void validateEndpoint(final ActionRequestValidationException errors) {
}

try {
URL url = new URL(endpoint);
URL url = URI.create(endpoint).toURL();
url.toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException e) {
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
log.info("Invalid URL[{}] is provided", endpoint, e);
errors.addValidationError("Invalid URL format is provided");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.InvalidParameterException;
Expand Down Expand Up @@ -194,7 +195,7 @@ private void validateUpdateIntervalIsLessThanValidForInDays(final UpdateDatasour
}

long validForInDays = isEndpointChanged(request, datasource)
? DatasourceManifest.Builder.build(new URL(request.getEndpoint())).getValidForInDays()
? DatasourceManifest.Builder.build(URI.create(request.getEndpoint()).toURL()).getValidForInDays()
: datasource.getDatabase().getValidForInDays();

long updateInterval = isUpdateIntervalChanged(request)
Expand Down Expand Up @@ -237,7 +238,7 @@ private void validateManifestFile(final UpdateDatasourceRequest request) {

DatasourceManifest manifest;
try {
URL url = new URL(request.getEndpoint());
URL url = URI.create(request.getEndpoint()).toURL();
manifest = DatasourceManifest.Builder.build(url);
} catch (Exception e) {
log.info("Error occurred while reading a file from {}", request.getEndpoint(), e);
Expand All @@ -247,8 +248,8 @@ private void validateManifestFile(final UpdateDatasourceRequest request) {
}

try {
new URL(manifest.getUrl()).toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException e) {
URI.create(manifest.getUrl()).toURL().toURI(); // Validate URL complies with RFC-2396
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
log.info("Invalid URL[{}] is provided for url field in the manifest file", manifest.getUrl(), e);
throw new IllegalArgumentException("Invalid URL format is provided for url field in the manifest file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package org.opensearch.geospatial.ip2geo.common;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -125,8 +125,8 @@ protected static class DatasourceEndpointValidator implements Setting.Validator<
@Override
public void validate(final String value) {
try {
new URL(value).toURI();
} catch (MalformedURLException | URISyntaxException e) {
URI.create(value).toURL().toURI();
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid URL format is provided");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
Expand Down Expand Up @@ -48,7 +49,7 @@ public URL toUrlIfNotInDenyList(final String url) {

@SuppressForbidden(reason = "Need to connect to http endpoint to read GeoIP database file")
private URL toUrlIfNotInDenyList(final String url, final List<String> denyList) throws UnknownHostException, MalformedURLException {
URL urlToReturn = new URL(url);
URL urlToReturn = URI.create(url).toURL();
if (isInDenyList(new IPAddressString(InetAddress.getByName(urlToReturn.getHost()).getHostAddress()), denyList)) {
throw new IllegalArgumentException(
"given endpoint is blocked by deny list in cluster setting " + Ip2GeoSettings.DATASOURCE_ENDPOINT_DENYLIST.getKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.geospatial.ip2geo.jobscheduler;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected void waitUntilAllShardsStarted(final String indexName, final int timeo
* @return header fields of geo data
*/
public List<String> getHeaderFields(String manifestUrl) throws IOException {
URL url = new URL(manifestUrl);
URL url = URI.create(manifestUrl).toURL();
DatasourceManifest manifest = DatasourceManifest.Builder.build(url);

try (CSVParser reader = geoIpDataDao.getDatabaseReader(manifest)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.mockito.Mockito.when;

import java.io.File;
import java.net.URL;
import java.net.URI;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -120,7 +120,7 @@ public void prepareIp2GeoTestCase() {
verifyingClient = spy(new VerifyingClient(this.getTestName()));
clusterSettings = new ClusterSettings(settings, new HashSet<>(Ip2GeoSettings.settings()));
ingestMetadata = new IngestMetadata(Collections.emptyMap());
when(urlDenyListChecker.toUrlIfNotInDenyList(anyString())).thenAnswer(i -> new URL(i.getArgument(0)));
when(urlDenyListChecker.toUrlIfNotInDenyList(anyString())).thenAnswer(i -> URI.create(i.getArgument(0)).toURL());
when(metadata.custom(IngestMetadata.TYPE)).thenReturn(ingestMetadata);
when(clusterService.getSettings()).thenReturn(Settings.EMPTY);
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testValidateNoRedirects_whenRedirectStatus_thenBlocked() {
HttpURLConnection connection = mock(HttpURLConnection.class);
when(connection.getResponseCode()).thenReturn(statusCode);
when(connection.getHeaderField("Location")).thenReturn("http://redirect-target.com");
when(connection.getURL()).thenReturn(new java.net.URL("https://example.com/test"));
when(connection.getURL()).thenReturn(java.net.URI.create("https://example.com/test").toURL());

IllegalArgumentException exception = expectThrows(
IllegalArgumentException.class,
Expand Down
Loading