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 */
019
020package pt.ua.dicoogle.server.web.servlets.webui;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.List;
026import javax.servlet.ServletException;
027import javax.servlet.http.HttpServlet;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030import net.sf.json.JSONArray;
031import net.sf.json.JSONObject;
032import org.apache.commons.lang3.StringUtils;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035import pt.ua.dicoogle.plugins.PluginController;
036import pt.ua.dicoogle.plugins.webui.WebUIPlugin;
037import pt.ua.dicoogle.server.users.Role;
038import pt.ua.dicoogle.server.users.RolesStruct;
039import pt.ua.dicoogle.server.users.User;
040import pt.ua.dicoogle.server.web.auth.Authentication;
041
042/**
043 * Retrieval of web UI plugins and respective packages/modules.
044 * 
045 * <b>This API is unstable. It is currently only compatible with dicoogle-webcore >= 0.12.x && <= 0.14.x</b>
046 *
047 * @author Eduardo Pinho
048 */
049public class WebUIServlet extends HttpServlet {
050    private static final Logger logger = LoggerFactory.getLogger(WebUIServlet.class);
051    
052    @Override
053    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
054        String name = req.getParameter("name");
055        String[] slotIdArr = req.getParameterValues("slot-id");
056        String module = req.getParameter("module");
057        String process = req.getParameter("process");
058
059        resp.setContentType("application/json");
060        if (name != null) {
061            resp.getWriter().append(this.getPlugin(resp, name));
062        } else if (module != null) {
063            /**
064             * deprecated: Use {@link WebUIModuleServlet} instead
065             */
066            resp.setContentType("application/javascript");
067            boolean doProcess = process == null || Boolean.parseBoolean(process);
068            resp.sendRedirect("/webui/module/" + module + (!doProcess ? "?process=false" : ""));
069        } else {
070            resp.getWriter().append(this.getPluginsBySlot(req, slotIdArr));
071        }
072    }
073
074    /** Retrieve plugins. */
075    private String getPluginsBySlot(HttpServletRequest req, String... slotIds) throws IOException {
076        String token = req.getHeader("Authorization");
077        User user = Authentication.getInstance().getUsername(token);
078
079        Collection<WebUIPlugin> plugins = PluginController.getInstance().getWebUIPlugins(slotIds);
080        List<String> pkgList = new ArrayList<>(plugins.size());
081        for (WebUIPlugin plugin : plugins) {
082
083            String pkg = PluginController.getInstance().getWebUIPackageJSON(plugin.getName());
084            boolean hasUserAllowPlugin= false;
085            for (String r:plugin.getRoles())
086            {
087                Role rr = RolesStruct.getInstance().getRole(r);
088
089                hasUserAllowPlugin = RolesStruct.getInstance().hasRole(user, rr);
090                if (hasUserAllowPlugin)
091                    break;
092            }
093
094
095            if (pkg != null&&(hasUserAllowPlugin||(user!=null&&user.isAdmin()))) {
096                pkgList.add(pkg);
097            }
098        }
099        JSONObject o = new JSONObject();
100        JSONArray pkgArr = new JSONArray();
101        for (String n : pkgList) {
102            pkgArr.add(n);
103        }
104        o.element("plugins", pkgArr);
105        return o.toString();
106    }
107
108    private String getPlugin(HttpServletResponse resp, String name) throws IOException {
109        resp.setContentType("application/json");
110        String json = PluginController.getInstance().getWebUIPackageJSON(name);
111        return json;
112    }
113
114    /** Convert from dash-lowercase to camelCase
115     * @param s a string in dash-lowercase
116     * @return a string in camelCase
117     */
118    public static String camelize(String s) {
119        String [] words = s.split("-");
120        if (words.length == 0) return "";
121        StringBuilder t = new StringBuilder();
122        t.append(words[0]);
123        for (int i = 1 ; i < words.length ; i++) {
124            if (!words[i].isEmpty()) {
125                t.append(Character.toUpperCase(words[i].charAt(0)));
126                t.append(words[i].substring(1));
127            }
128        }
129        return t.toString();
130    }
131}