001package org.hl7.fhir.utilities.http; 002 003import java.io.IOException; 004import java.nio.charset.StandardCharsets; 005import java.util.Base64; 006import java.util.HashMap; 007import java.util.List; 008import java.util.Map; 009 010import org.hl7.fhir.utilities.settings.ServerDetailsPOJO; 011 012/** 013 * Simple HTTP client for making requests to a server. 014 */ 015public class ManagedWebAccessor extends ManagedWebAccessorBase<ManagedWebAccessor> { 016 017 public ManagedWebAccessor(Iterable<String> serverTypes, String userAgent, List<ServerDetailsPOJO> serverAuthDetails) { 018 super(serverTypes, userAgent, serverAuthDetails); 019 } 020 021 private Map<String, String> newHeaders() { 022 Map<String, String> headers = new HashMap<String, String>(this.getHeaders()); 023 if (getAuthenticationMode() == HTTPAuthenticationMode.TOKEN) { 024 headers.put("Authorization", "Bearer " + getToken()); 025 } else if (getAuthenticationMode() == HTTPAuthenticationMode.BASIC) { 026 String auth = getUsername() +":"+ getPassword(); 027 byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8)); 028 headers.put("Authorization", "Basic " + new String(encodedAuth)); 029 } else if (getAuthenticationMode() == HTTPAuthenticationMode.APIKEY) { 030 headers.put("Api-Key", getToken()); 031 } 032 033 if (getUserAgent() != null) { 034 headers.put("User-Agent", getUserAgent()); 035 } 036 return headers; 037 } 038 039 private SimpleHTTPClient setupClient(String url) throws IOException { 040 if (!ManagedWebAccess.inAllowedPaths(url)) { 041 throw new IOException("The pathname '"+url+"' cannot be accessed by policy"); 042 } 043 SimpleHTTPClient client = new SimpleHTTPClient(); 044 045 for (Map.Entry<String, String> entry : this.getHeaders().entrySet()) { 046 client.addHeader(entry.getKey(), entry.getValue()); 047 } 048 049 if (getUserAgent() != null) { 050 client.addHeader("User-Agent", getUserAgent()); 051 } 052 if (getAuthenticationMode() != null) { 053 if (getAuthenticationMode() != HTTPAuthenticationMode.NONE) { 054 client.setAuthenticationMode(getAuthenticationMode()); 055 switch (getAuthenticationMode()) { 056 case BASIC : 057 client.setUsername(getUsername()); 058 client.setPassword(getPassword()); 059 break; 060 case TOKEN : 061 client.setToken(getToken()); 062 break; 063 case APIKEY : 064 client.setApiKey(getToken()); 065 break; 066 } 067 } 068 } else { 069 ServerDetailsPOJO settings = ManagedWebAccessUtils.getServer(getServerTypes(), url, getServerAuthDetails()); 070 if (settings != null) { 071 switch (settings.getAuthenticationType()) { 072 case "basic" : 073 client.setUsername(settings.getUsername()); 074 client.setPassword(settings.getPassword()); 075 client.setAuthenticationMode(HTTPAuthenticationMode.BASIC); 076 break; 077 case "token" : 078 client.setToken(settings.getToken()); 079 client.setAuthenticationMode(HTTPAuthenticationMode.TOKEN); 080 break; 081 case "apikey" : 082 client.setApiKey(settings.getApikey()); 083 client.setAuthenticationMode(HTTPAuthenticationMode.APIKEY); 084 break; 085 } 086 if (settings.getHeaders() != null) { 087 for (String n : settings.getHeaders().keySet()) { 088 client.addHeader(n, settings.getHeaders().get(n)); 089 } 090 } 091 } 092 } 093 if (getUsername() != null || getToken() != null) { 094 client.setAuthenticationMode(getAuthenticationMode()); 095 } 096 return client; 097 } 098 099 public HTTPResult get(String url) throws IOException { 100 return get(url, null); 101 } 102 103 public HTTPResult get(String url, String accept) throws IOException { 104 switch (ManagedWebAccess.getAccessPolicy()) { 105 case DIRECT: 106 SimpleHTTPClient client = setupClient(url); 107 return client.get(url, accept); 108 case MANAGED: 109 return ManagedWebAccess.getAccessor().get(getServerTypes(), url, accept, newHeaders()); 110 case PROHIBITED: 111 throw new IOException("Access to the internet is not allowed by local security policy"); 112 default: 113 throw new IOException("Internal Error"); 114 } 115 } 116 117 public HTTPResult post(String url, byte[] content, String contentType) throws IOException { 118 return post(url, content, contentType, null); 119 } 120 121 public HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException { 122 switch (ManagedWebAccess.getAccessPolicy()) { 123 case DIRECT: 124 SimpleHTTPClient client = setupClient(url); 125 return client.post(url, contentType, content, accept); 126 case MANAGED: 127 return ManagedWebAccess.getAccessor().post(getServerTypes(), url, content, contentType, accept, newHeaders()); 128 case PROHIBITED: 129 throw new IOException("Access to the internet is not allowed by local security policy"); 130 default: 131 throw new IOException("Internal Error"); 132 } 133 } 134 135 public HTTPResult put(String url, byte[] content, String contentType) throws IOException { 136 return put(url, content, contentType, null); 137 } 138 139 public HTTPResult put(String url, byte[] content, String contentType, String accept) throws IOException { 140 switch (ManagedWebAccess.getAccessPolicy()) { 141 case DIRECT: 142 SimpleHTTPClient client = setupClient(url); 143 return client.put(url, contentType, content, accept); 144 case MANAGED: 145 return ManagedWebAccess.getAccessor().put(getServerTypes(), url, content, contentType, accept, newHeaders()); 146 case PROHIBITED: 147 throw new IOException("Access to the internet is not allowed by local security policy"); 148 default: 149 throw new IOException("Internal Error"); 150 } 151 } 152}