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
013public class ManagedWebAccessBuilder {
014
015  private String userAgent;
016  private HTTPAuthenticationMode authenticationMode;
017  private String username;
018  private String password;
019  private String token;
020  private String accept;
021  private List<ServerDetailsPOJO> serverAuthDetails;
022  private Map<String, String> headers = new HashMap<String, String>();
023  
024  public ManagedWebAccessBuilder(String userAgent, List<ServerDetailsPOJO> serverAuthDetails) {
025    this.userAgent = userAgent;
026    this.serverAuthDetails = serverAuthDetails;
027  }
028
029  public ManagedWebAccessBuilder withAccept(String accept) {
030    this.accept = accept;
031    return this;
032  }
033
034  public ManagedWebAccessBuilder withHeader(String name, String value) {
035    headers.put(name, value);
036    return this;
037  }
038
039  public ManagedWebAccessBuilder withBasicAuth(String username, String password) {
040    this.authenticationMode = HTTPAuthenticationMode.BASIC;
041    this.username = username;
042    this.password = password;
043    return this;
044  }
045  
046  public ManagedWebAccessBuilder withToken(String token) {
047    this.authenticationMode = HTTPAuthenticationMode.TOKEN;
048    this.token = token;
049    return this;
050  }
051  
052  private Map<String, String> headers() {
053    Map<String, String> headers = new HashMap<String, String>();
054    headers.putAll(this.headers);
055    if (authenticationMode == HTTPAuthenticationMode.TOKEN) {
056      headers.put("Authorization", "Bearer " + token);
057    } else if (authenticationMode == HTTPAuthenticationMode.BASIC) {
058      String auth = username+":"+password;
059      byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
060      headers.put("Authorization", "Basic " + new String(encodedAuth));
061    }
062
063    if (userAgent != null) {
064      headers.put("User-Agent", userAgent);
065    }
066
067    return headers;
068  }
069
070  private SimpleHTTPClient setupClient(String url) throws IOException {
071    if (!ManagedWebAccess.inAllowedPaths(url)) {
072      throw new IOException("The pathname '"+url+"' cannot be accessed by policy");
073    }
074    SimpleHTTPClient client = new SimpleHTTPClient();
075    if (userAgent != null) {
076      client.addHeader("User-Agent", userAgent);
077    }
078    if (authenticationMode != null && authenticationMode != HTTPAuthenticationMode.NONE) {
079      client.setAuthenticationMode(authenticationMode);
080      switch (authenticationMode) {
081      case BASIC :
082        client.setUsername(username); 
083        client.setPassword(password);
084        break;
085      case TOKEN :
086        client.setToken(token);
087        break;
088      case APIKEY :
089        client.setApiKey(token);
090        break;
091      }
092    } else {
093      ServerDetailsPOJO settings = getServer(url);
094      if (settings != null) {
095        switch (settings.getAuthenticationType()) {
096        case "basic" :
097          client.setUsername(settings.getUsername()); 
098          client.setPassword(settings.getPassword());
099          client.setAuthenticationMode(HTTPAuthenticationMode.BASIC);
100          break;
101        case "token" :
102          client.setToken(settings.getToken());
103          client.setAuthenticationMode(HTTPAuthenticationMode.TOKEN);
104          break;
105        case "apikey" :
106          client.setApiKey(settings.getApikey());
107          client.setAuthenticationMode(HTTPAuthenticationMode.APIKEY);
108          break;
109        }
110      }
111    }
112    if (username != null || token != null) {
113      
114      client.setAuthenticationMode(authenticationMode);
115    }
116    return client;
117  }
118
119
120  private ServerDetailsPOJO getServer(String url) {
121    if (serverAuthDetails != null) {
122      for (ServerDetailsPOJO t : serverAuthDetails) {
123        if (url.startsWith(t.getUrl())) {
124          return t;
125        }
126      }
127    }
128    return null;
129  }
130
131  public HTTPResult get(String url) throws IOException {
132    switch (ManagedWebAccess.getAccessPolicy()) {
133    case DIRECT:
134      SimpleHTTPClient client = setupClient(url);
135      return client.get(url, accept);
136    case MANAGED:
137      return ManagedWebAccess.getAccessor().get(url, accept, headers());
138    case PROHIBITED:
139      throw new IOException("Access to the internet is not allowed by local security policy");
140    default:
141      throw new IOException("Internal Error");
142    }
143  }
144
145
146  public HTTPResult post(String url, byte[] content, String contentType) throws IOException {
147    switch (ManagedWebAccess.getAccessPolicy()) {
148    case DIRECT:
149      SimpleHTTPClient client = setupClient(url);
150      return client.post(url, contentType, content, accept);
151    case MANAGED:
152      return ManagedWebAccess.getAccessor().post(url, content, contentType, accept, headers());
153    case PROHIBITED:
154      throw new IOException("Access to the internet is not allowed by local security policy");
155    default:
156      throw new IOException("Internal Error");
157    }
158  }
159
160  public HTTPResult put(String url, byte[] content, String contentType) throws IOException {
161    switch (ManagedWebAccess.getAccessPolicy()) {
162    case DIRECT:
163      SimpleHTTPClient client = setupClient(url);
164      return client.put(url, contentType, content, accept);
165    case MANAGED:
166      return ManagedWebAccess.getAccessor().put(url, content, contentType, accept, headers());
167    case PROHIBITED:
168      throw new IOException("Access to the internet is not allowed by local security policy");
169    default:
170      throw new IOException("Internal Error");
171    }
172  }
173
174}