package org.javafxdata.datasources.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Implementation of DataSourceReader for obtaining data from a network source
 * (e.g. a static XML file, a dynamically served REST interface,...). Instances
 * of this class can be created and passed in the constructor of a protocol
 * implementations in the org.javafxdata.datasources.io package. <br/> Note that
 * using this class does not create a binding between the physical network
 * source and the view that renders the data.
 *
 * @author johan
 */
public class NetworkSource implements DataSourceReader {

    private InputStream is;
    private final String address;
    private String protocol;
    private String accept;

    public NetworkSource(String address) {
        this.address = address;

    }
    
    public void setAccept(String accept) {
        this.accept = accept;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        if (accept != null) {
            connection.addRequestProperty("Accept", accept);
        }
        is = connection.getInputStream();
        return is;
    }
    
}