Files
sopa/src/main/java/com/molnarandrei/sopa/wsdl/WsdlRemote.java
T
2026-11-16 00:00:39 +01:00

117 lines
4.6 KiB
Java

package com.molnarandrei.sopa.wsdl;
import com.molnarandrei.sopa.soap.MalformedEndpointException;
import com.molnarandrei.sopa.soap.SoapCall;
import javax.xml.XMLConstants;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import org.w3c.dom.Element;
public class WsdlRemote {
private static final Logger log = Logger.getLogger(WsdlRemote.class.getName());
private static final int IMPORT_TIMEOUT_SECONDS = 1;
public record Download(String wsdl, List<String> imports, List<String> failedImports) {}
public static Download download(String endpoint, int timeoutSeconds) throws IOException, InterruptedException {
requireValidEndpoint(endpoint);
var url = wsdlUrl(endpoint);
var wsdl = fetch(url, timeoutSeconds);
if (! WsdlValidations.isWsdl(wsdl))
throw new IOException("%s no devuelve un WSDL.".formatted(url));
var imports = new ArrayList<String>();
var failed = new ArrayList<String>();
var seen = new HashSet<>(Set.of(url));
fetchImportsRecursively(url, wsdl, seen, imports, failed);
return new Download(wsdl, imports, failed);
}
private static void requireValidEndpoint(String endpoint) throws IOException {
try {
SoapCall.validateEndpoint(endpoint);
} catch (MalformedEndpointException malformed) {
throw new IOException("Endpoint no válido: %s".formatted(malformed.getMessage()), malformed);
}
}
private static void fetchImportsRecursively(
String documentUrl, String document, Set<String> seen, List<String> imports, List<String> failed
) throws InterruptedException {
for (var location : schemaLocations(document)) {
String url;
try {
url = URI.create(documentUrl).resolve(location.strip()).toString();
SoapCall.validateEndpoint(url);
} catch (IllegalArgumentException | MalformedEndpointException notFetchable) {
log.warning("Cannot fetch import %s of %s.".formatted(location, documentUrl));
failed.add(location);
continue;
}
if (! seen.add(url)) continue;
try {
var schema = fetch(url, IMPORT_TIMEOUT_SECONDS);
if (! WsdlValidations.isSchema(schema))
throw new IOException("%s no devuelve un esquema.".formatted(url));
imports.add(schema);
fetchImportsRecursively(url, schema, seen, imports, failed);
} catch (IOException cannotFetch) {
log.warning("Cannot fetch import %s: %s".formatted(url, cannotFetch));
failed.add(url);
}
}
}
private static List<String> schemaLocations(String document) {
var locations = new ArrayList<String>();
var root = WsdlValidations.rootOrNull(document);
for (var name : List.of("import", "include")) {
var references = root.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, name);
for (int i = 0; i < references.getLength(); i++) {
var location = ((Element) references.item(i)).getAttribute("schemaLocation");
if (! location.isBlank()) locations.add(location);
}
}
return locations;
}
private static String fetch(String url, int timeoutSeconds) throws IOException, InterruptedException {
log.info("Downloading %s.".formatted(url));
var timeout = Duration.ofSeconds(timeoutSeconds);
try (
var client = HttpClient.newBuilder()
.connectTimeout(timeout)
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
) {
var request = HttpRequest.newBuilder()
.uri(URI.create(url))
.timeout(timeout)
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200)
throw new IOException("%s respondió con estado %d.".formatted(url, response.statusCode()));
return response.body();
}
}
private static String wsdlUrl(String endpoint) {
var url = endpoint.strip();
if (url.toLowerCase().endsWith("?wsdl")) return url;
return url + (url.contains("?") ? "&wsdl" : "?wsdl");
}
}