Search code examples
javaweb-servicesclient-serverjava-web-start

How to browse the file system of a server machine when connecting with a client (java)


I'm in the process of making a proof of concept to dissociate the business code from the gui for the ps3 media server (http://www.ps3mediaserver.org/). For this I've got a project hosted at source forge (http://sourceforge.net/projects/pms-remote/). The client should be a simple front end to configure the server from any location within a network having the rights to connect to the server.

On the server side, all service have been exposed using javax.jws and the client proxy has been generated using wsimport.

One of the features of the current features (actually, the only blocking one), is to define the folders that will be shared by the server. As the client and server are now running as a single application on the same machine, it's trivial to browse its file system.

Problem: I'd like to expose the file system of the server machine through web services. This will allow any client (the one I'm currently working on is the same as the original using java swing) to show available folders and to select the ones that will be shown by the media server. In the end the only thing I'm interested in is an absolute folder path (string).

I thought I'd find a library giving me this functionality but couldn't find any. Browsing the files using a UNC path and accessing a distant machine doesn't seem feasible, as it wouldn't be transparent for the user.

For now I don't want to worry about security issues, I'll figure these out once the rest seems feasible.

I'd be grateful for any input. Thanks, Philippe


Solution

  • I've ended up creating a pretty simple web service letting either list all root folders or all child folders for a given path. It's now up to the client to have a (GUI) browser to access this service.

    package net.pms.plugin.webservice.filesystem;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    import net.pms.plugin.webservice.ServiceBase;
    
    @WebService(serviceName = "FileSystem", targetNamespace = "http://ps3mediaserver.org/filesystem")
    public class FileSystemWebService extends ServiceBase {
    
        @WebMethod()
        public List<String> getRoots() {
            List<String> roots = new ArrayList<String>();
            for(File child : File.listRoots()) {
                roots.add(child.getAbsolutePath());
            }
            return roots;
        }
    
        @WebMethod()
        public List<String> getChildFolders(@WebParam(name="folderPath") String folderPath) throws FileNotFoundException {
            List<String> children = new ArrayList<String>();
            File d = new File(folderPath);
            if(d.isDirectory()) {
                for(File child : d.listFiles()) {
                    if(child.isDirectory() && !child.isHidden()) {
                        children.add(child.getAbsolutePath());
                    }
                }
            } else {
                throw new FileNotFoundException();
            }
            return children;
        }
    }
    

    For people wanting to use this, here's the ServiceBase class as well

    package net.pms.plugin.webservice;
    
    import javax.xml.ws.Endpoint;
    
    import org.apache.log4j.Logger;
    
    public abstract class ServiceBase {
        private static final Logger log = Logger.getLogger(ServiceBase.class);
        protected boolean isInitialized;
    
        /**
         * the published endpoint
         */
        private Endpoint endpoint = null;
    
        /**
         * 
         * Start to listen for remote requests
         * 
         * @param host ip or host name
         * @param port port to use
         * @param path name of the web service
         */
        public void bind(String host, int port, String path) {
            String endpointURL = "http://" + host + ":" + port + "/" + path;
            try {
                endpoint = Endpoint.publish(endpointURL, this);
                isInitialized = true;
                log.info("Sucessfully bound enpoint: " + endpointURL);
            } catch (Exception e) {
                log.error("Failed to bind enpoint: " + endpointURL, e);
            }
        }
    
        /**
         * Stop the webservice
         */
        public void shutdown() {
            log.info("Shut down " + getClass().getName());
            if (endpoint != null)
                endpoint.stop();
            endpoint = null;
        }
    
    }