Skip to content

Commit

Permalink
Merge pull request #41 from scylladb/dk/java-switch-url-to-uri
Browse files Browse the repository at this point in the history
Java: remove URL
  • Loading branch information
dkropachev authored Nov 14, 2024
2 parents 57bfcdc + 6d5593d commit 4c296a6
Showing 1 changed file with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.scylladb.alternator;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
Expand Down Expand Up @@ -81,18 +78,16 @@ public URI nextAsURI() {
try {
return new URI(alternatorScheme, null, nextNode(), alternatorPort, "", null, null);
} catch (URISyntaxException e) {
// Can't happen with the empty path and other nulls we used above...
logger.log(Level.WARNING, "nextAsURI", e);
return null;
}
}

public URL nextAsURL(String file) {
public URI nextAsURI(String file) {
try {
return new URL(alternatorScheme, nextNode(), alternatorPort, file);
} catch (MalformedURLException e) {
// Can only happen if alternatorScheme is an unknown one.
logger.log(Level.WARNING, "nextAsURL", e);
return new URI(alternatorScheme, "", nextNode(), alternatorPort, file, "", "");
} catch (URISyntaxException e) {
logger.log(Level.WARNING, "nextAsURI", e);
return null;
}
}
Expand All @@ -106,11 +101,11 @@ private static String streamToString(java.io.InputStream is) {

private void updateLiveNodes() {
List<String> newHosts = new ArrayList<>();
URL url = nextAsURL("/localnodes");
URI uri = nextAsURI("/localnodes");
try {
// Note that despite this being called HttpURLConnection, it actually
// supports HTTPS as well.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Expand All @@ -126,7 +121,7 @@ private void updateLiveNodes() {
}
}
} catch (IOException e) {
logger.log(Level.FINE, "Request failed: " + url, e);
logger.log(Level.FINE, "Request failed: " + uri, e);
}
if (!newHosts.isEmpty()) {
liveNodes.set(newHosts);
Expand Down

0 comments on commit 4c296a6

Please sign in to comment.