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.web.servlets.search;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Collection;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServlet;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import net.sf.json.JSONArray;
031
032import pt.ua.dicoogle.plugins.PluginController;
033import pt.ua.dicoogle.sdk.DicooglePlugin;
034
035/**
036 * Retrieve active providers
037 *
038 * @author Frederico Silva <fredericosilva@ua.pt>
039 */
040public class ProvidersServlet extends HttpServlet{
041
042        @Override
043        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
044                        throws ServletException, IOException {
045        String paramType = req.getParameter("type");
046        
047        final String type = paramType != null ? paramType : "query";
048                Collection<String> activeProviders;
049        switch (type) {
050            case "index":
051                activeProviders = getIndexerNames();
052                break;
053            case "storage":
054                activeProviders = getStorageNames();
055                break;
056            case "query":
057                activeProviders = PluginController.getInstance().getQueryProvidersName(true);
058                break;
059            default:
060                resp.sendError(400);
061                return;
062        }
063                
064        JSONArray json = new JSONArray();
065        json.addAll(activeProviders);
066                
067        resp.setContentType("application/json");
068                resp.getWriter().print(json.toString());
069                        
070        }
071        
072    private static Collection<String> getIndexerNames() {
073        return getPluginNames(PluginController.getInstance().getIndexingPlugins(true));
074    }
075
076    private Collection<String> getStorageNames() {
077        return getPluginNames(PluginController.getInstance().getStoragePlugins(true));
078    }
079    
080    private static Collection<String> getPluginNames(Collection<? extends DicooglePlugin> plugins) {
081        Collection<String> c = new ArrayList<>(plugins.size());
082        for (DicooglePlugin p : plugins) {
083            c.add(p.getName());
084        }
085        return c;
086    }
087}