001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.server;
020
021import java.util.ArrayList;
022import java.util.List;
023import org.slf4j.LoggerFactory;
024import org.restlet.Application;
025import org.restlet.Restlet;
026import org.restlet.resource.ServerResource;
027import org.restlet.routing.Router;
028
029/** A Restlet Application for aggregating web services from plugins
030 *
031 * @author psytek
032 * @author Luís A. Bastião Silva <bastiao@ua.pt>
033 * @author Eduardo Pinho <eduardopinho@ua.pt>
034 */
035public class PluginRestletApplication extends Application {
036
037    private static final List<ServerResource> pluginServices = new ArrayList<>();
038    
039    private Router internalRouter;
040    
041    public PluginRestletApplication() {
042        super();
043    }
044    
045    /**
046     * Creates a root Restlet that will receive all incoming calls.
047     * @return a Restlet for the root 
048     */
049    @Override
050    public synchronized Restlet createInboundRoot() {
051        // Create a router Restlet that routes each call to a
052        // new instance of our resources
053        this.internalRouter = new Router(getContext());
054        // Defines routing to resources
055        this.internalRouter.setDefaultMatchingQuery(false);
056        
057        //lets add plugin registred services
058        //this is still a little brittle... :(
059        for(ServerResource resource : pluginServices) {
060            LoggerFactory.getLogger(PluginRestletApplication.class).debug("Inbound: {}", resource);
061            internalRouter.attach("/" + resource.toString(), resource.getClass());
062        }
063        
064        LoggerFactory.getLogger(PluginRestletApplication.class).debug("Installed plugin restlets: {}",
065                pluginServices);
066        return internalRouter;
067    }
068    
069    protected void loadPlugins() {
070    }
071    
072    public static void attachRestPlugin(ServerResource resource){
073        pluginServices.add(resource);
074    }
075    
076}