WIP move accept to parameter

This commit is contained in:
dotasek 2024-10-24 13:57:51 -04:00
parent ddb0a661dd
commit b33de12e49
7 changed files with 47 additions and 37 deletions

View File

@ -158,7 +158,7 @@ public class TerminologyCacheManager {
HTTPResult res = ManagedWebAccess.builder()
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.withAccept("application/zip").put(url, bs.toByteArray(), null);
.put(url, bs.toByteArray(), null, "application/zip");
if (res.getCode() >= 300) {
System.out.println("sending cache failed: " + res.getCode());
} else {

View File

@ -158,7 +158,7 @@ public class TerminologyCacheManager {
System.out.println("Sending tx-cache to "+url+" ("+Utilities.describeSize(bs.toByteArray().length)+")");
HTTPResult res = ManagedWebAccess.builder()
.withBasicAuth(token.substring(0, token.indexOf(':')), token.substring(token.indexOf(':') + 1))
.withAccept("application/zip").put(url, bs.toByteArray(), null);
.put(url, bs.toByteArray(), null, "application/zip");
if (res.getCode() >= 300) {
System.out.println("sending cache failed: "+res.getCode());

View File

@ -126,19 +126,19 @@ public class ManagedWebAccess {
}
public static HTTPResult get(String url, String accept) throws IOException {
return builder().withAccept(accept).get(url);
return builder().get(url, accept);
}
public static HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException {
return builder().withAccept(accept).post(url, content, contentType);
return builder().post(url, content, contentType, accept);
}
public static HTTPResult put(String url, byte[] content, String contentType, String accept) throws IOException {
return builder().withAccept(accept).put(url, content, contentType);
return builder().put(url, content, contentType, accept);
}
public static Response httpCall(Request.Builder httpRequest) throws IOException {
public static Response httpCall(FhirRequest httpRequest) throws IOException {
return fhirBuilder().httpCall(httpRequest);
}

View File

@ -21,10 +21,6 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
super(userAgent, serverAuthDetails);
}
public ManagedWebAccessBuilder withAccept(String accept) {
return super.withAccept(accept);
}
private Map<String, String> newHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.putAll(this.getHeaders());
@ -51,7 +47,8 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
if (getUserAgent() != null) {
client.addHeader("User-Agent", getUserAgent());
}
if (getAuthenticationMode() != null && getAuthenticationMode() != HTTPAuthenticationMode.NONE) {
if (getAuthenticationMode() != null) {
if (getAuthenticationMode() != HTTPAuthenticationMode.NONE) {
client.setAuthenticationMode(getAuthenticationMode());
switch (getAuthenticationMode()) {
case BASIC :
@ -65,8 +62,9 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
client.setApiKey(getToken());
break;
}
}
} else {
ServerDetailsPOJO settings = getServer(url);
ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(url, getServerAuthDetails());
if (settings != null) {
switch (settings.getAuthenticationType()) {
case "basic" :
@ -91,24 +89,17 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
return client;
}
private ServerDetailsPOJO getServer(String url) {
if (getServerAuthDetails() != null) {
for (ServerDetailsPOJO t : getServerAuthDetails()) {
if (url.startsWith(t.getUrl())) {
return t;
}
}
}
return null;
public HTTPResult get(String url) throws IOException {
return get(url, null);
}
public HTTPResult get(String url) throws IOException {
public HTTPResult get(String url, String accept) throws IOException {
switch (ManagedWebAccess.getAccessPolicy()) {
case DIRECT:
SimpleHTTPClient client = setupClient(url);
return client.get(url, getAccept());
return client.get(url, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().get(url, getAccept(), newHeaders());
return ManagedWebAccess.getAccessor().get(url, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
@ -117,12 +108,16 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
}
public HTTPResult post(String url, byte[] content, String contentType) throws IOException {
return post(url, content, contentType, null);
}
public HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException {
switch (ManagedWebAccess.getAccessPolicy()) {
case DIRECT:
SimpleHTTPClient client = setupClient(url);
return client.post(url, contentType, content, getAccept());
return client.post(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().post(url, content, contentType, getAccept(), newHeaders());
return ManagedWebAccess.getAccessor().post(url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:
@ -131,12 +126,16 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase<Managed
}
public HTTPResult put(String url, byte[] content, String contentType) throws IOException {
return put(url, content, contentType, null);
}
public HTTPResult put(String url, byte[] content, String contentType, String accept) throws IOException {
switch (ManagedWebAccess.getAccessPolicy()) {
case DIRECT:
SimpleHTTPClient client = setupClient(url);
return client.put(url, contentType, content, getAccept());
return client.put(url, contentType, content, accept);
case MANAGED:
return ManagedWebAccess.getAccessor().put(url, content, contentType, getAccept(), newHeaders());
return ManagedWebAccess.getAccessor().put(url, content, contentType, accept, newHeaders());
case PROHIBITED:
throw new IOException("Access to the internet is not allowed by local security policy");
default:

View File

@ -18,8 +18,7 @@ public abstract class ManagedWebAccessBuilderBase<B extends ManagedWebAccessBuil
private String password;
@Getter
private String token;
@Getter
private String accept;
@Getter
private final List<ServerDetailsPOJO> serverAuthDetails;
@Getter
@ -35,11 +34,6 @@ public abstract class ManagedWebAccessBuilderBase<B extends ManagedWebAccessBuil
return (B) this;
}
public B withAccept(String accept) {
this.accept = accept;
return self();
}
public B withHeader(String name, String value) {
headers.put(name, value);
return self();

View File

@ -0,0 +1,17 @@
package org.hl7.fhir.utilities.http;
import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
public class ManagedWebAccessUtils {
public static ServerDetailsPOJO getServer(String url, Iterable<ServerDetailsPOJO> serverAuthDetails) {
if (serverAuthDetails != null) {
for (ServerDetailsPOJO t : serverAuthDetails) {
if (url.startsWith(t.getUrl())) {
return t;
}
}
}
return null;
}
}

View File

@ -176,13 +176,13 @@ public class PackageClient {
}
private InputStream fetchUrl(String source, String accept) throws IOException {
ManagedWebAccessBuilder client = ManagedWebAccess.builder().withAccept(accept);
ManagedWebAccessBuilder client = ManagedWebAccess.builder();
if (server.getAuthenticationMode() == HTTPAuthenticationMode.TOKEN) {
client.withToken(server.getToken());
} else if (server.getAuthenticationMode() == HTTPAuthenticationMode.BASIC) {
client.withBasicAuth(server.getUsername(), server.getPassword());
}
HTTPResult res = client.get(source);
HTTPResult res = client.get(source, accept);
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
}