Skip to content

Commit

Permalink
Allow ManagedWebAccess to fetch specific server types for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dotasek committed Nov 11, 2024
1 parent 4ee4a89 commit 5cbb45a
Show file tree
Hide file tree
Showing 28 changed files with 95 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -241,7 +236,7 @@ private void processCurrentPackage(String url, String pid, Set<String> cpidSet,
File co = ManagedFileAccess.file(Utilities.path(cache, pid+"."+manifest.asString("date")+".tgz"));
if (!co.exists()) {

HTTPResult res = ManagedWebAccess.get("web", repo+"/package.tgz?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), repo+"/package.tgz?nocache=" + System.currentTimeMillis());
res.checkThrowException();
TextFile.bytesToFile(res.getContent(), co);
}
Expand Down Expand Up @@ -338,7 +333,7 @@ private void processFeed(Set<String> list, String str) throws IOException, Parse
System.out.println("Feed "+str);
try {

HTTPResult res = ManagedWebAccess.get("web", str+"?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), str+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
Document xml = XMLUtil.parseToDom(res.getContent());
for (Element channel : XMLUtil.getNamedChildren(xml.getDocumentElement(), "channel")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -126,7 +127,7 @@ private void processArchetype(String id) throws Exception {

private Document loadXml(String address) throws Exception {

HTTPResult res = ManagedWebAccess.get("web", address, "application/xml");
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), address, "application/xml");
res.checkThrowException();
InputStream xml = new ByteArrayInputStream(res.getContent());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hl7.fhir.convertors.misc;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -394,7 +395,7 @@ private CodeSystem makeEntityCodeSystem() {


private JsonObject fetchJson(String source) throws IOException {
HTTPResult res = ManagedWebAccess.accessor("web").withHeader("API-Version", "v2").withHeader("Accept-Language", "en").get(source,"application/json");
HTTPResult res = ManagedWebAccess.accessor(Arrays.asList("web")).withHeader("API-Version", "v2").withHeader("Accept-Language", "en").get(source,"application/json");
res.checkThrowException();
return JsonParser.parseObject(res.getContent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRFormatError;
Expand Down Expand Up @@ -1384,7 +1379,7 @@ private String cachedFetch(String id, String source) throws IOException {
if (f.exists())
return TextFile.fileToString(f);

HTTPResult res = ManagedWebAccess.get("web", source);
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source);
res.checkThrowException();
String result = TextFile.bytesToString(res.getContent());
TextFile.stringToFile(result, f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void fillCache(String source) throws IOException {
try {
System.out.println("Initialise terminology cache from " + source);

HTTPResult res = ManagedWebAccess.get("web", source + "?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
unzip(new ByteArrayInputStream(res.getContent()), cacheFolder);
} catch (Exception e) {
Expand Down Expand Up @@ -148,7 +149,7 @@ public void commit(String token) throws IOException {
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("web")
HTTPResult res = ManagedWebAccess.accessor(Arrays.asList("web"))
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.put(url, bs.toByteArray(), null, "application/zip");
if (res.getCode() >= 300) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private ValidatedFragment addNamedElement(List<ValidatedFragment> res, String na


private HTTPResult fetchFile(String url, String ct) throws IOException {
HTTPResult res = ManagedWebAccess.get("web", url, ct);
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), url, ct);
res.checkThrowException();
return res;
}
Expand All @@ -299,7 +299,7 @@ private HTTPResult fetchManifest() throws IOException {

JsonObject j = new JsonObject();
j.add("recipient", "FHIR Validator");
HTTPResult res = ManagedWebAccess.post("web", url, org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json", "application/json");
HTTPResult res = ManagedWebAccess.post(Arrays.asList("web"), url, org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json", "application/json");
res.checkThrowException();
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -78,7 +79,7 @@ private void fillCache(String source) throws IOException {
try {
System.out.println("Initialise terminology cache from "+source);

HTTPResult res = ManagedWebAccess.get("web", source+"?nocache=" + System.currentTimeMillis());
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
unzip(new ByteArrayInputStream(res.getContent()), cacheFolder);
} catch (Exception e) {
Expand Down Expand Up @@ -148,7 +149,7 @@ public void commit(String token) throws IOException {
// 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("web")
HTTPResult res = ManagedWebAccess.accessor(Arrays.asList("web"))
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.put(url, bs.toByteArray(), null, "application/zip");

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ManagedFhirWebAccessor withLogger(ToolingClientLogger logger) {


public ManagedFhirWebAccessor(String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super("fhir", userAgent, serverAuthDetails);
super(Arrays.asList("fhir"), userAgent, serverAuthDetails);
this.timeout = 5000;
this.timeoutUnit = TimeUnit.MILLISECONDS;
}
Expand Down Expand Up @@ -88,7 +89,7 @@ protected HTTPRequest requestWithManagedHeaders(HTTPRequest httpRequest) {
}
}
} else {
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getMode(), httpRequest.getUrl().toString(), getServerAuthDetails());
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getServerTypes(), httpRequest.getUrl().toString(), getServerAuthDetails());
if (settings != null) {
switch (settings.getAuthenticationType()) {
case "basic":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
public class ManagedWebAccess {

public interface IWebAccessor {
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;
HTTPResult get(Iterable<String> serverTypes, String url, String accept, Map<String, String> headers) throws IOException;
HTTPResult post(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
HTTPResult put(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
}

public interface IFhirWebAccessor {
Expand Down Expand Up @@ -104,28 +104,28 @@ public static void setUserAgent(String userAgent) {
ManagedWebAccess.userAgent = userAgent;
}

public static ManagedWebAccessor accessor(String mode) {
return new ManagedWebAccessor(mode, userAgent, serverAuthDetails);
public static ManagedWebAccessor accessor(Iterable<String> serverTypes) {
return new ManagedWebAccessor(serverTypes, userAgent, serverAuthDetails);
}

public static ManagedFhirWebAccessor fhirAccessor() {
return new ManagedFhirWebAccessor(userAgent, serverAuthDetails);
}

public static HTTPResult get(String mode, String url) throws IOException {
return accessor(mode).get(url);
public static HTTPResult get(Iterable<String> serverTypes, String url) throws IOException {
return accessor(serverTypes).get(url);
}

public static HTTPResult get(String mode, String url, String accept) throws IOException {
return accessor(mode).get(url, accept);
public static HTTPResult get(Iterable<String> serverTypes, String url, String accept) throws IOException {
return accessor(serverTypes).get(url, 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 post(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException {
return accessor(serverTypes).post(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 put(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException {
return accessor(serverTypes).put(url, content, contentType, accept);
}

public static HTTPResult httpCall(HTTPRequest httpRequest) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

public class ManagedWebAccessUtils {

public static ServerDetailsPOJO getServer(String mode, String url, Iterable<ServerDetailsPOJO> serverAuthDetails) {
public static ServerDetailsPOJO getServer(Iterable<String> serverTypes, String url, Iterable<ServerDetailsPOJO> serverAuthDetails) {
if (serverAuthDetails != null) {
for (ServerDetailsPOJO t : serverAuthDetails) {
if (url.startsWith(t.getUrl()) && modesMatch(mode, t.getMode())) {
return t;
for (ServerDetailsPOJO serverDetails : serverAuthDetails) {
for (String serverType : serverTypes) {
if (url.startsWith(serverDetails.getUrl()) && typesMatch(serverType, serverDetails.getType())) {
return serverDetails;
}
}
}
}
return null;
}

private static boolean modesMatch(String criteria, String value) {
private static boolean typesMatch(String criteria, String value) {
return criteria == null || value == null || criteria.equals(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccessor> {

public ManagedWebAccessor(String mode, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super(mode, userAgent, serverAuthDetails);
public ManagedWebAccessor(Iterable<String> serverTypes, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
super(serverTypes, userAgent, serverAuthDetails);
}

private Map<String, String> newHeaders() {
Expand Down Expand Up @@ -66,7 +66,7 @@ private SimpleHTTPClient setupClient(String url) throws IOException {
}
}
} else {
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getMode(), url, getServerAuthDetails());
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getServerTypes(), url, getServerAuthDetails());
if (settings != null) {
switch (settings.getAuthenticationType()) {
case "basic" :
Expand Down Expand Up @@ -101,7 +101,7 @@ public HTTPResult get(String url, String accept) throws IOException {
SimpleHTTPClient client = setupClient(url);
return client.get(url, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().get(getMode(), url, accept, newHeaders());
return ManagedWebAccess.getAccessor().get(getServerTypes(), url, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
Expand All @@ -119,7 +119,7 @@ public HTTPResult post(String url, byte[] content, String contentType, String ac
SimpleHTTPClient client = setupClient(url);
return client.post(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().post(getMode(), url, content, contentType, accept, newHeaders());
return ManagedWebAccess.getAccessor().post(getServerTypes(), url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
Expand All @@ -137,7 +137,7 @@ public HTTPResult put(String url, byte[] content, String contentType, String acc
SimpleHTTPClient client = setupClient(url);
return client.put(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().put(getMode(), url, content, contentType, accept, newHeaders());
return ManagedWebAccess.getAccessor().put(getServerTypes(), url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public abstract class ManagedWebAccessorBase<B extends ManagedWebAccessorBase<B>> {
@Getter
private final String mode;
private final Iterable<String> serverTypes;

@Getter
private final String userAgent;
Expand All @@ -27,8 +27,8 @@ public abstract class ManagedWebAccessorBase<B extends ManagedWebAccessorBase<B>
@Getter
private final Map<String, String> headers = new HashMap<>();

public ManagedWebAccessorBase(String mode, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
this.mode = mode;
public ManagedWebAccessorBase(Iterable<String> serverTypes, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
this.serverTypes = serverTypes;
this.userAgent = userAgent;
this.serverAuthDetails = serverAuthDetails;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.Stack;

Expand Down Expand Up @@ -724,13 +725,13 @@ public static byte[] writeBytes(JsonObject json, boolean pretty) {
}

public static JsonObject fetchJson(String source) throws IOException {
HTTPResult res = ManagedWebAccess.get("web", source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
res.checkThrowException();
return parseJson(res.getContent());
}

public static JsonArray fetchJsonArray(String source) throws IOException {
HTTPResult res = ManagedWebAccess.get("web",source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"),source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
res.checkThrowException();
return parseJsonArray(res.getContent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import org.hl7.fhir.utilities.TextFile;
Expand Down Expand Up @@ -692,7 +693,7 @@ private void write(StringBuilder b, JsonElement e, boolean pretty, int indent) {

private static byte[] fetch(String source) throws IOException {
String murl = source.contains("?") ? source+"&nocache=" + System.currentTimeMillis() : source+"?nocache=" + System.currentTimeMillis();
HTTPResult res = ManagedWebAccess.get("web", murl, "application/json, application/fhir+json");
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), murl, "application/json, application/fhir+json");
res.checkThrowException();
return res.getContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,8 @@ private InputStreamWithSrc fetchSourceFromUrlSpecific(String url) {

private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException {
try {
HTTPResult res = ManagedWebAccess.get("web", source);
//FIXME: Could this be in disagreement with the credentials selected by ManagedWebAccess?
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source);
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
} catch (Exception e) {
Expand Down Expand Up @@ -862,8 +863,8 @@ private void checkBuildLoaded() {
}

private void loadFromBuildServer() throws IOException {

HTTPResult res = ManagedWebAccess.get("web", "https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
//FIXME: Could this be in disagreement with the credentials selected by ManagedWebAccess?
HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), "https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
res.checkThrowException();

buildInfo = (JsonArray) JsonParser.parse(TextFile.bytesToString(res.getContent()));
Expand Down
Loading

0 comments on commit 5cbb45a

Please sign in to comment.