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 org.apache.commons.codec.binary.Base64; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025import pt.ua.dicoogle.plugins.PluginController; 026import pt.ua.dicoogle.plugins.webui.WebUIPlugin; 027 028import javax.servlet.ServletException; 029import javax.servlet.http.HttpServlet; 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032import java.io.IOException; 033import java.io.Writer; 034import java.nio.ByteBuffer; 035 036/** 037 * Retrieval of web UI plugins and respective packages/modules. 038 * 039 * <b>This API is unstable. It is currently only compatible with dicoogle-webcore 0.14.x</b> 040 * 041 * @author Eduardo Pinho 042 */ 043public class WebUIModuleServlet extends HttpServlet { 044 private static final Logger logger = LoggerFactory.getLogger(WebUIModuleServlet.class); 045 046 @Override 047 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 048 String name = req.getRequestURI().substring("/webui/module/".length()); 049 if (name.isEmpty()) { 050 resp.sendError(400); 051 return; 052 } 053 logger.debug("Service request: retrieve webplugin module {}", name); 054 055 String process = req.getParameter("process"); 056 057 resp.setContentType("application/javascript"); 058 boolean doProcess = process == null || Boolean.parseBoolean(process); 059 060 WebUIPlugin plugin = PluginController.getInstance().getWebUIPlugin(name); 061 if (plugin == null) { 062 resp.sendError(404); 063 return; 064 } 065 String js = PluginController.getInstance().getWebUIModuleJS(name); 066 067 this.writeModule(resp.getWriter(), name, js, doProcess); 068 } 069 070 /** Convert from dash-lowercase to camelCase 071 * @param s a string in dash-lowercase 072 * @return a string in camelCase 073 */ 074 public static String camelize(String s) { 075 String [] words = s.split("-"); 076 if (words.length == 0) return ""; 077 String t = words[0]; 078 for (int i = 1 ; i < words.length ; i++) { 079 if (!words[i].isEmpty()) { 080 t += Character.toUpperCase(words[i].charAt(0)) + words[i].substring(1); 081 } 082 } 083 return t; 084 } 085 086 public static boolean isPrerelease(String version) { 087 return version.indexOf('-') != -1; 088 } 089 090 public static void writeModule(Writer writer, String name, String module, boolean process) throws IOException { 091 if (process) { 092 writer.append("(function(r,f){\n"); 093 writer.append("var w=r.DicoogleWebcore||require(\"dicoogle-webcore\");"); 094 writer.append("if (w.__esModule)w=w.default;"); 095 writer.append("var c=(r.Dicoogle||require(\"dicoogle-client\"))();"); 096 writer.append("var m={exports:{}};"); 097 writer.append("f(c,m,m.exports);"); 098 writer.append("var o=m.exports.__esModule?m.exports.default:m.exports;"); 099 writer.append("w.constructors[\""); 100 writer.append(name); 101 writer.append("\"]=o;"); 102 writer.append("w.onRegister(new o(),\""); 103 writer.append(name); 104 writer.append("\");"); 105 writer.append("})(this,function(Dicoogle,module,exports){\n"); 106 } 107 writer.append(module); 108 if (process) { 109 writer.append("});\n"); 110 } 111 } 112}