001package org.hl7.fhir.utilities.http;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileNotFoundException;
037import java.io.FileOutputStream;
038import java.io.IOException;
039import java.io.InputStream;
040import java.nio.file.Path;
041import java.util.ArrayList;
042import java.util.List;
043import java.util.Map;
044
045import org.hl7.fhir.utilities.Utilities;
046import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
047
048/**
049 * see security.md - manages access to the local file system by the FHIR HAPI Core library
050 * 
051 * By using accessPolicy, allowedDomains and accessor, a host java application can control 
052 * whether this library has direct access to the web (and which domains it is allowed to access),
053 * or whether the host application provides controlled access, or whether no access is allowed at all
054 * (in which case other information providers need to be provided)
055 *  
056 * @author Grahame
057 *
058 */
059public class ManagedWebAccess {
060  
061  public interface IWebAccessor {
062    HTTPResult get(String url, String accept, Map<String, String> headers) throws IOException;
063    HTTPResult post(String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
064    HTTPResult put(String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException;
065  }
066
067  public enum WebAccessPolicy {
068    DIRECT, // open access to the web, though access can be restricted only to domains in AllowedDomains
069    MANAGED, // no access except by the IWebAccessor
070    PROHIBITED, // no access at all to the web
071  }
072
073  private static WebAccessPolicy accessPolicy = WebAccessPolicy.DIRECT; // for legacy reasons
074  private static List<String> allowedDomains = new ArrayList<>();
075  private static IWebAccessor accessor;
076  private static String userAgent;
077  private static List<ServerDetailsPOJO> serverAuthDetails;
078  
079  
080  public static WebAccessPolicy getAccessPolicy() {
081    return accessPolicy;
082  }
083
084  public static void setAccessPolicy(WebAccessPolicy accessPolicy) {
085    ManagedWebAccess.accessPolicy = accessPolicy;
086  }
087
088  static boolean inAllowedPaths(String pathname) {
089    if (allowedDomains.isEmpty()) {
090      return true;
091    }
092    for (String s : allowedDomains) {
093      if (pathname.startsWith(s)) {
094        return true;
095      }
096    }
097    return false;
098  }
099
100  public static String getUserAgent() {
101    return userAgent;
102  }
103
104  public static void setUserAgent(String userAgent) {
105    ManagedWebAccess.userAgent = userAgent;
106  }
107
108  public static IWebAccessor getAccessor() {
109    return accessor;
110  }
111
112  public static ManagedWebAccessBuilder builder() {
113    return new ManagedWebAccessBuilder(userAgent, serverAuthDetails);
114  }
115
116  public static HTTPResult get(String url) throws IOException {
117    return builder().get(url);
118  }
119
120  public static HTTPResult get(String url, String accept) throws IOException {
121    return builder().withAccept(accept).get(url);
122  }
123
124  public static HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException {
125    return builder().withAccept(accept).post(url, content, contentType);
126  }
127  
128
129  public static HTTPResult put(String url, byte[] content, String contentType, String accept) throws IOException {
130    return builder().withAccept(accept).put(url, content, contentType);
131  }
132
133}