Add mode to web access

This commit is contained in:
Grahame Grieve 2024-11-09 18:39:12 +10:30
parent 765ba8baed
commit 60acdd0b88
21 changed files with 57 additions and 43 deletions

View File

@ -241,7 +241,7 @@ public class PackageVisitor {
File co = ManagedFileAccess.file(Utilities.path(cache, pid+"."+manifest.asString("date")+".tgz"));
if (!co.exists()) {
HTTPResult res = ManagedWebAccess.get(repo+"/package.tgz?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", repo+"/package.tgz?nocache=" + System.currentTimeMillis());
res.checkThrowException();
TextFile.bytesToFile(res.getContent(), co);
}
@ -338,7 +338,7 @@ public class PackageVisitor {
System.out.println("Feed "+str);
try {
HTTPResult res = ManagedWebAccess.get(str+"?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", str+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
Document xml = XMLUtil.parseToDom(res.getContent());
for (Element channel : XMLUtil.getNamedChildren(xml.getDocumentElement(), "channel")) {

View File

@ -394,7 +394,7 @@ public class ICD11Generator {
private JsonObject fetchJson(String source) throws IOException {
HTTPResult res = ManagedWebAccess.accessor().withHeader("API-Version", "v2").withHeader("Accept-Language", "en").get(source,"application/json");
HTTPResult res = ManagedWebAccess.accessor("web").withHeader("API-Version", "v2").withHeader("Accept-Language", "en").get(source,"application/json");
res.checkThrowException();
return JsonParser.parseObject(res.getContent());
}

View File

@ -1384,7 +1384,7 @@ public class ProfileComparer {
if (f.exists())
return TextFile.fileToString(f);
HTTPResult res = ManagedWebAccess.get(source);
HTTPResult res = ManagedWebAccess.get("web", source);
res.checkThrowException();
String result = TextFile.bytesToString(res.getContent());
TextFile.stringToFile(result, f);

View File

@ -79,7 +79,7 @@ public class TerminologyCacheManager {
try {
System.out.println("Initialise terminology cache from " + source);
HTTPResult res = ManagedWebAccess.get(source + "?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", source + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
unzip(new ByteArrayInputStream(res.getContent()), cacheFolder);
} catch (Exception e) {
@ -148,7 +148,7 @@ public class TerminologyCacheManager {
String url = "https://tx.fhir.org/post/tx-cache/" + ghOrg + "/" + ghRepo + "/" + ghBranch + ".zip";
System.out.println("Sending tx-cache to " + url + " (" + Utilities.describeSize(bs.toByteArray().length) + ")");
HTTPResult res = ManagedWebAccess.accessor()
HTTPResult res = ManagedWebAccess.accessor("web")
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.put(url, bs.toByteArray(), null, "application/zip");
if (res.getCode() >= 300) {

View File

@ -299,7 +299,7 @@ public class SHLParser extends ParserBase {
JsonObject j = new JsonObject();
j.add("recipient", "FHIR Validator");
HTTPResult res = ManagedWebAccess.post(url, org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json", "application/json");
HTTPResult res = ManagedWebAccess.post("web", url, org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json", "application/json");
res.checkThrowException();
return res;
}

View File

@ -78,7 +78,7 @@ public class TerminologyCacheManager {
try {
System.out.println("Initialise terminology cache from "+source);
HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
unzip(new ByteArrayInputStream(res.getContent()), cacheFolder);
} catch (Exception e) {
@ -148,7 +148,7 @@ public class TerminologyCacheManager {
// post it to
String url = "https://tx.fhir.org/post/tx-cache/"+ghOrg+"/"+ghRepo+"/"+ghBranch+".zip";
System.out.println("Sending tx-cache to "+url+" ("+Utilities.describeSize(bs.toByteArray().length)+")");
HTTPResult res = ManagedWebAccess.accessor()
HTTPResult res = ManagedWebAccess.accessor("web")
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.put(url, bs.toByteArray(), null, "application/zip");

View File

@ -45,7 +45,7 @@ public class ManagedFhirWebAccessor extends ManagedWebAccessorBase<ManagedFhirWe
public ManagedFhirWebAccessor(String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super(userAgent, serverAuthDetails);
super("fhir", userAgent, serverAuthDetails);
this.timeout = 5000;
this.timeoutUnit = TimeUnit.MILLISECONDS;
}
@ -88,7 +88,7 @@ public class ManagedFhirWebAccessor extends ManagedWebAccessorBase<ManagedFhirWe
}
}
} else {
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(httpRequest.getUrl().toString(), getServerAuthDetails());
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getMode(), httpRequest.getUrl().toString(), getServerAuthDetails());
if (settings != null) {
switch (settings.getAuthenticationType()) {
case "basic":

View File

@ -52,9 +52,9 @@ import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
public class ManagedWebAccess {
public interface IWebAccessor {
HTTPResult get(String url, String accept, Map<String, String> headers) throws IOException;
HTTPResult post(String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
HTTPResult put(String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
HTTPResult get(String mode, String url, String accept, Map<String, String> headers) throws IOException;
HTTPResult post(String mode, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
HTTPResult put(String mode, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
}
public interface IFhirWebAccessor {
@ -104,28 +104,28 @@ public class ManagedWebAccess {
ManagedWebAccess.userAgent = userAgent;
}
public static ManagedWebAccessor accessor() {
return new ManagedWebAccessor(userAgent, serverAuthDetails);
public static ManagedWebAccessor accessor(String mode) {
return new ManagedWebAccessor(mode, userAgent, serverAuthDetails);
}
public static ManagedFhirWebAccessor fhirAccessor() {
return new ManagedFhirWebAccessor(userAgent, serverAuthDetails);
}
public static HTTPResult get(String url) throws IOException {
return accessor().get(url);
public static HTTPResult get(String mode, String url) throws IOException {
return accessor(mode).get(url);
}
public static HTTPResult get(String url, String accept) throws IOException {
return accessor().get(url, accept);
public static HTTPResult get(String mode, String url, String accept) throws IOException {
return accessor(mode).get(url, accept);
}
public static HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException {
return accessor().post(url, content, contentType, accept);
public static HTTPResult post(String mode, String url, byte[] content, String contentType, String accept) throws IOException {
return accessor(mode).post(url, content, contentType, accept);
}
public static HTTPResult put(String url, byte[] content, String contentType, String accept) throws IOException {
return accessor().put(url, content, contentType, accept);
public static HTTPResult put(String mode, String url, byte[] content, String contentType, String accept) throws IOException {
return accessor(mode).put(url, content, contentType, accept);
}
public static HTTPResult httpCall(HTTPRequest httpRequest) throws IOException {

View File

@ -4,14 +4,18 @@ import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
public class ManagedWebAccessUtils {
public static ServerDetailsPOJO getServer(String url, Iterable<ServerDetailsPOJO> serverAuthDetails) {
public static ServerDetailsPOJO getServer(String mode, String url, Iterable<ServerDetailsPOJO> serverAuthDetails) {
if (serverAuthDetails != null) {
for (ServerDetailsPOJO t : serverAuthDetails) {
if (url.startsWith(t.getUrl())) {
if (url.startsWith(t.getUrl()) && modesMatch(mode, t.getMode())) {
return t;
}
}
}
return null;
}
private static boolean modesMatch(String criteria, String value) {
return criteria == null || value == null || criteria.equals(value);
}
}

View File

@ -14,8 +14,8 @@ import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
*/
public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccessor> {
public ManagedWebAccessor(String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super(userAgent, serverAuthDetails);
public ManagedWebAccessor(String mode, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super(mode, userAgent, serverAuthDetails);
}
private Map<String, String> newHeaders() {
@ -66,7 +66,7 @@ public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccesso
}
}
} else {
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(url, getServerAuthDetails());
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getMode(), url, getServerAuthDetails());
if (settings != null) {
switch (settings.getAuthenticationType()) {
case "basic" :
@ -101,7 +101,7 @@ public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccesso
SimpleHTTPClient client = setupClient(url);
return client.get(url, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().get(url, accept, newHeaders());
return ManagedWebAccess.getAccessor().get(getMode(), url, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
@ -119,7 +119,7 @@ public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccesso
SimpleHTTPClient client = setupClient(url);
return client.post(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().post(url, content, contentType, accept, newHeaders());
return ManagedWebAccess.getAccessor().post(getMode(), url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
@ -137,7 +137,7 @@ public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccesso
SimpleHTTPClient client = setupClient(url);
return client.put(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().put(url, content, contentType, accept, newHeaders());
return ManagedWebAccess.getAccessor().put(getMode(), url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:

View File

@ -8,6 +8,9 @@ import java.util.List;
import java.util.Map;
public abstract class ManagedWebAccessorBase<B extends ManagedWebAccessorBase<B>> {
@Getter
private final String mode;
@Getter
private final String userAgent;
@Getter
@ -24,7 +27,8 @@ public abstract class ManagedWebAccessorBase<B extends ManagedWebAccessorBase<B>
@Getter
private final Map<String, String> headers = new HashMap<>();
public ManagedWebAccessorBase(String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
public ManagedWebAccessorBase(String mode, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
this.mode = mode;
this.userAgent = userAgent;
this.serverAuthDetails = serverAuthDetails;
}

View File

@ -729,7 +729,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException {
try {
HTTPResult res = ManagedWebAccess.get(source);
HTTPResult res = ManagedWebAccess.get("web", source);
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
} catch (Exception e) {
@ -863,7 +863,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
private void loadFromBuildServer() throws IOException {
HTTPResult res = ManagedWebAccess.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", "https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
res.checkThrowException();
buildInfo = (JsonArray) JsonParser.parse(TextFile.bytesToString(res.getContent()));

View File

@ -1480,7 +1480,7 @@ public class NpmPackage {
}
public static NpmPackage fromUrl(String source) throws IOException {
HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("npm", source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return fromPackage(new ByteArrayInputStream(res.getContent()));
}

View File

@ -175,7 +175,7 @@ public class PackageClient {
}
private InputStream fetchUrl(String source, String accept) throws IOException {
ManagedWebAccessor webAccessor = ManagedWebAccess.accessor();
ManagedWebAccessor webAccessor = ManagedWebAccess.accessor("web");
if (server.getAuthenticationMode() == HTTPAuthenticationMode.TOKEN) {
webAccessor.withToken(server.getToken());
} else if (server.getAuthenticationMode() == HTTPAuthenticationMode.BASIC) {

View File

@ -11,6 +11,9 @@ import lombok.extern.jackson.Jacksonized;
@AllArgsConstructor
public class ServerDetailsPOJO {
// what kind of HTTP server this is - a FHIR tx server, an NPM server, or a general web server
String mode;
String url;
// possible values: none, basic, token, apikey

View File

@ -162,6 +162,7 @@ public class ManagedWebAccessAuthTests {
private ServerDetailsPOJO getBasicAuthServerPojo() {
return new ServerDetailsPOJO(
null,
server.url("").toString(),
"basic",
"dummyServerType",
@ -181,6 +182,7 @@ public void testTokenAuthFromSettings() throws IOException, InterruptedException
private ServerDetailsPOJO getTokenAuthServerPojo() {
return new ServerDetailsPOJO(
null,
server.url("").toString(),
"token",
"dummyServerType",
@ -200,6 +202,7 @@ public void testTokenAuthFromSettings() throws IOException, InterruptedException
private ServerDetailsPOJO getApiKeyAuthServerPojo() {
return new ServerDetailsPOJO(
null,
server.url("").toString(),
"apikey",
"dummyServerType",

View File

@ -430,7 +430,7 @@ public class IgLoader implements IValidationEngineLoader {
private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException, IOException {
try {
HTTPResult res = ManagedWebAccess.get(source + "?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", source + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
} catch (IOException e) {

View File

@ -319,7 +319,7 @@ public class Scanner {
}
protected void download(String address, String filename) throws IOException {
HTTPResult res = ManagedWebAccess.get(address);
HTTPResult res = ManagedWebAccess.get("web", address);
res.checkThrowException();
TextFile.bytesToFile(res.getContent(), filename);
}

View File

@ -942,7 +942,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
if (output.startsWith("http://")) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
handleOutputToStream(r, output, bs, version);
HTTPResult res = ManagedWebAccess.post(output, bs.toByteArray(), "application/fhir+xml", "application/fhir+xml");
HTTPResult res = ManagedWebAccess.post("web", output, bs.toByteArray(), "application/fhir+xml", "application/fhir+xml");
res.checkThrowException();
} else {
FileOutputStream s = ManagedFileAccess.outStream(output);
@ -1099,7 +1099,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
@Override
public byte[] fetchRaw(IResourceValidator validator, String source) throws IOException {
HTTPResult res = ManagedWebAccess.get(source);
HTTPResult res = ManagedWebAccess.get("web", source);
res.checkThrowException();
return res.getContent();
}

View File

@ -25,7 +25,7 @@ public class ProfileLoader {
private static byte[] loadProfileFromUrl(String src) throws FHIRException {
try {
HTTPResult res = ManagedWebAccess.get(src + "?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get("web", src + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return res.getContent();
} catch (Exception e) {

View File

@ -858,7 +858,7 @@ public class ValidationTests implements IEvaluationContext, IValidatorResourceFe
@Override
public byte[] fetchRaw(IResourceValidator validator, String source) throws MalformedURLException, IOException {
HTTPResult res = ManagedWebAccess.get(source);
HTTPResult res = ManagedWebAccess.get("web", source);
res.checkThrowException();
return res.getContent();
}