Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new URI API #11824

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ recipeList:
# - org.openrewrite.staticanalysis.ExplicitInitialization

- org.openrewrite.java.migrate.io.ReplaceFileInOrOutputStreamFinalizeWithClose
- org.openrewrite.java.migrate.lang.StringFormatted
- org.openrewrite.java.migrate.net.JavaNetAPIs
- org.openrewrite.java.migrate.net.URLConstructorsToURIRecipes
- org.openrewrite.java.migrate.util.SequencedCollection
- org.openrewrite.java.migrate.lang.StringFormatted

- org.openrewrite.java.RemoveObjectsIsNull
- org.openrewrite.java.ShortenFullyQualifiedTypeReferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -241,7 +242,7 @@ private List<LinkedFileViewModel> findAssociatedNotLinkedFiles(BibEntry entry) {

public boolean downloadFile(String urlText) {
try {
URL url = new URL(urlText);
URL url = URI.create(urlText).toURL();
addFromURLAndDownload(url);
return true;
} catch (MalformedURLException exception) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/fieldeditors/URLUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.fieldeditors;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -38,7 +39,7 @@ public static String cleanGoogleSearchURL(String url) {
}
// Extract destination URL
try {
URL searchURL = new URL(url);
URL searchURL = URI.create(url).toURL();
// URL parameters
String query = searchURL.getQuery();
// no parameters
Expand Down Expand Up @@ -76,7 +77,7 @@ public static String cleanGoogleSearchURL(String url) {
*/
public static boolean isURL(String url) {
try {
new URL(url);
URI.create(url).toURL();
return true;
} catch (MalformedURLException e) {
return false;
Expand All @@ -95,7 +96,7 @@ public static Optional<String> getSuffix(final String link, ExternalApplications
String strippedLink = link;
try {
// Try to strip the query string, if any, to get the correct suffix:
URL url = new URL(link);
URL url = URI.create(link).toURL();
if ((url.getQuery() != null) && (url.getQuery().length() < (link.length() - 1))) {
strippedLink = link.substring(0, link.length() - url.getQuery().length() - 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.linkedfile;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

Expand Down Expand Up @@ -60,7 +61,7 @@ public void execute() {
}

try {
URL url = new URL(urlforDownload.get());
URL url = URI.create(urlforDownload.get()).toURL();
LinkedFileViewModel onlineFile = new LinkedFileViewModel(
new LinkedFile(url, ""),
entry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.linkedfile;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;
Expand Down Expand Up @@ -139,7 +140,7 @@ public LinkedFile getNewLinkedFile() {

if (LinkedFile.isOnlineLink(link.getValue())) {
try {
return new LinkedFile(description.getValue(), new URL(link.getValue()), fileType, sourceUrl.getValue());
return new LinkedFile(description.getValue(), URI.create(link.getValue()).toURL(), fileType, sourceUrl.getValue());
} catch (MalformedURLException e) {
return new LinkedFile(description.getValue(), link.getValue(), fileType, sourceUrl.getValue());
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/importer/fetcher/ACS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -52,7 +53,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {

if (link != null) {
LOGGER.info("Fulltext PDF found @ ACS.");
return Optional.of(new URL(source.replaceFirst("/abs/", "/pdf/")));
return Optional.of(URI.create(source.replaceFirst("/abs/", "/pdf/")).toURL());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
Expand Down Expand Up @@ -47,7 +48,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
if (code == 200) {
LOGGER.info("Fulltext PDF found @ APS.");
try {
return Optional.of(new URL(pdfRequestUrl));
return Optional.of(URI.create(pdfRequestUrl).toURL());
} catch (MalformedURLException e) {
LOGGER.warn("APS returned malformed URL, cannot find PDF.");
}
Expand Down Expand Up @@ -76,7 +77,7 @@ private Optional<String> getId(String doi) {

URLConnection con;
try {
con = new URL(doiRequest).openConnection();
con = URI.create(doiRequest).toURL().openConnection();
con.connect();
con.getInputStream();
String[] urlParts = con.getURL().toString().split("abstract/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -709,7 +710,7 @@ public ArXivEntry(Node item) {
if (linkTitle.equals(Optional.of("pdf"))) {
pdfUrlParsed = XMLUtil.getAttributeContent(linkNode, "href").map(url -> {
try {
return new URL(url);
return URI.create(url).toURL();
} catch (MalformedURLException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

Expand Down Expand Up @@ -36,7 +37,7 @@ public static Optional<BibEntry> getEntry(String entryUrl, ImportFormatPreferenc
String cleanURL = entryUrl.replace("%", "%25").replace(":", "%3A").replace("/", "%2F").replace("?", "%3F")
.replace("&", "%26").replace("=", "%3D");

URL url = new URL(BibsonomyScraper.BIBSONOMY_SCRAPER + cleanURL + BibsonomyScraper.BIBSONOMY_SCRAPER_POST);
URL url = URI.create(BibsonomyScraper.BIBSONOMY_SCRAPER + cleanURL + BibsonomyScraper.BIBSONOMY_SCRAPER_POST).toURL();
String bibtex = new URLDownload(url).asString();
return BibtexParser.singleFromString(bibtex, importFormatPreferences);
} catch (IOException | FetcherException ex) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/logic/importer/fetcher/CiteSeer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -102,13 +103,13 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException, FetcherExc
Optional<String> id = entry.getField(StandardField.DOI);
if (id.isPresent()) {
String source = PDF_URL.formatted(id.get());
return Optional.of(new URL(source));
return Optional.of(URI.create(source).toURL());
}

// if using id fails, we can try the source URL
Optional<String> urlString = entry.getField(StandardField.URL);
if (urlString.isPresent()) {
return Optional.of(new URL(urlString.get()));
return Optional.of(URI.create(urlString.get()).toURL());
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
Expand Down Expand Up @@ -121,7 +122,7 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc

URL doiURL;
try {
doiURL = new URL(doi.get().getURIAsASCIIString());
doiURL = URI.create(doi.get().getURIAsASCIIString()).toURL();
} catch (MalformedURLException e) {
throw new FetcherException("Malformed URL", e);
}
Expand Down Expand Up @@ -218,7 +219,7 @@ public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
public Optional<String> getAgency(DOI doi) throws FetcherException, MalformedURLException {
Optional<String> agency = Optional.empty();
try {
URLDownload download = getUrlDownload(new URL(DOI.AGENCY_RESOLVER + "/" + doi.getDOI()));
URLDownload download = getUrlDownload(URI.create(DOI.AGENCY_RESOLVER + "/" + doi.getDOI()).toURL());
JSONObject response = new JSONArray(download.asString()).getJSONObject(0);
if (response != null) {
agency = Optional.ofNullable(response.optString("RA"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {

String doiLink;
if (doiPreferences.isUseCustom()) {
base = new URL(doiPreferences.getDefaultBaseURI());
base = URI.create(doiPreferences.getDefaultBaseURI()).toURL();
doiLink = doi.get()
.getExternalURIWithCustomBase(base.toString())
.map(URI::toASCIIString)
Expand Down Expand Up @@ -110,11 +110,11 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
// See https://github.com/lehner/LocalCopy for more scrape ideas
// link with "PDF" in title tag
if (element.attr("title").toLowerCase(Locale.ENGLISH).contains("pdf") && new URLDownload(href).isPdf()) {
return Optional.of(new URL(href));
return Optional.of(URI.create(href).toURL());
}

if (href.contains("pdf") || hrefText.contains("pdf") && new URLDownload(href).isPdf()) {
links.add(new URL(href));
links.add(URI.create(href).toURL());
}
}

Expand All @@ -128,7 +128,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
} catch (UnsupportedMimeTypeException type) {
// this might be the PDF already as we follow redirects
if (type.getMimeType().startsWith("application/pdf")) {
return Optional.of(new URL(type.getUrl()));
return Optional.of(URI.create(type.getUrl()).toURL());
}
LOGGER.warn("DoiResolution fetcher failed: ", type);
} catch (IOException e) {
Expand All @@ -148,7 +148,7 @@ private Optional<URL> citationMetaTag(Document html) {

if (citationPdfUrl.isPresent()) {
try {
return Optional.of(new URL(citationPdfUrl.get()));
return Optional.of(URI.create(citationPdfUrl.get()).toURL());
} catch (MalformedURLException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.StringReader;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -107,7 +108,7 @@ private Optional<URL> search(String url) throws IOException {
// TODO: check title inside pdf + length?
// TODO: report error function needed?! query -> result
LOGGER.info("Fulltext PDF found @ Google: {}", target);
pdfLink = Optional.of(new URL(target));
pdfLink = Optional.of(URI.create(target).toURL());
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/logic/importer/fetcher/IEEE.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -118,7 +119,7 @@ private static BibEntry parseJsonResponse(JSONObject jsonEntry, Character keywor
entry.setField(StandardField.ISSN, jsonEntry.optString("issn"));
entry.setField(StandardField.ISSUE, jsonEntry.optString("issue"));
try {
entry.addFile(new LinkedFile(new URL(jsonEntry.optString("pdf_url")), "PDF"));
entry.addFile(new LinkedFile(URI.create(jsonEntry.optString("pdf_url")).toURL(), "PDF"));
} catch (MalformedURLException e) {
LOGGER.error("Fetched PDF URL String is malformed.");
}
Expand Down Expand Up @@ -203,7 +204,7 @@ public Optional<URL> findFullText(BibEntry entry) throws FetcherException {
LOGGER.debug("Full text document found on IEEE Xplore");
URL value;
try {
value = new URL(matcher.group(1));
value = URI.create(matcher.group(1)).toURL();
} catch (MalformedURLException e) {
throw new FetcherException("Malformed URL", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException, FetcherExc
// getRequiredValueBetween refuses to match across the line break.
fulltextLinkAsInHtml = fulltextLinkAsInHtml.replaceFirst(".*href=\"/", "").trim();
String fulltextLink = FULLTEXT_URL_PREFIX + fulltextLinkAsInHtml + ".pdf";
return Optional.of(new URL(fulltextLink));
return Optional.of(URI.create(fulltextLink).toURL());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -65,10 +66,10 @@ public URL getUrlForIdentifier(String identifier) throws MalformedURLException {

if (identifier.contains("/")) {
// if identifier links to a entry with a valid doi
return new URL(start + identifier);
return URI.create(start + identifier).toURL();
}
// else use default doi start.
return new URL(start + "10.2307/" + identifier);
return URI.create(start + "10.2307/" + identifier).toURL();
}

@Override
Expand Down Expand Up @@ -125,7 +126,7 @@ public Optional<URL> findFullText(BibEntry entry) throws FetcherException, IOExc
}

String url = elements.getFirst().attr("href");
return Optional.of(new URL(url));
return Optional.of(URI.create(url).toURL());
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/importer/fetcher/Medra.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.importer.fetcher;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -103,7 +104,7 @@ public URLDownload getUrlDownload(URL url) {

@Override
public URL getUrlForIdentifier(String identifier) throws URISyntaxException, MalformedURLException {
return new URL(API_URL + "/" + identifier);
return URI.create(API_URL + "/" + identifier).toURL();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -66,7 +67,7 @@ public Optional<URL> findFullText(DOI doi) throws UnirestException {
.map(location -> location.optString("url"))
.flatMap(url -> {
try {
return Optional.of(new URL(url));
return Optional.of(URI.create(url).toURL());
} catch (MalformedURLException e) {
LOGGER.debug("Could not determine URL to fetch full text from", e);
return Optional.empty();
Expand Down
Loading
Loading