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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public <T> T loadURI(
.content();
}

private ExternalResourceHandler fileResource(String pathStr) {
protected ExternalResourceHandler fileResource(String pathStr) {
Path path = Path.of(pathStr);
if (path.isAbsolute()) {
return new FileResource(path);
Expand All @@ -62,16 +62,24 @@ private ExternalResourceHandler fileResource(String pathStr) {
}
}

protected ExternalResourceHandler httpResource(URI uri, Optional<String> auth) {
try {
return new HttpResource(uri.toURL(), auth);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}

protected URI handleURI(URI uri) {
return GitHubHelper.handleURL(uri);
}

private ExternalResourceHandler buildFromURI(URI uri, Optional<String> auth) {
String scheme = uri.getScheme();
if (scheme == null || scheme.equalsIgnoreCase("file")) {
return fileResource(uri.getPath());
} else if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
try {
return new HttpResource(uri.toURL(), auth);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
return httpResource(handleURI(uri), auth);
} else {
throw new UnsupportedOperationException("Unsupported scheme " + scheme);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,34 @@
*/
package org.openworkflow.sdk.impl.resources;

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

public class GitHubHelper {

private GitHubHelper() {}

private static final String BLOB = "blob/";

public static URL handleURL(URL url) {
if (url.getHost().equals("github.com")) {
public static URI handleURL(URI url) {
if ("github.com".equals(url.getHost())) {
String path = url.getPath();
Comment thread
fjtirado marked this conversation as resolved.
if (path.startsWith(BLOB)) {
path = path.substring(BLOB.length());
}
try {
String path = url.getPath();
if (path.startsWith(BLOB)) {
path = path.substring(BLOB.length());
}
return new URL(url.getProtocol(), "raw.githubusercontent.com", url.getPort(), path);
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
return new URI(
url.getScheme(),
url.getUserInfo(),
"raw.githubusercontent.com",
url.getPort(),
path,
url.getQuery(),
url.getFragment());
} catch (URISyntaxException e) {
return url;
}

} else {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class HttpResource implements ExternalResourceHandler {
private final Optional<String> auth;

public HttpResource(URL url, Optional<String> auth) {
this.url = GitHubHelper.handleURL(url);
this.url = url;
this.auth = auth;
}

Expand Down
Loading