From ae2b66bd08e305d4e9418cd4ca988479aaf60115 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 3 Feb 2012 20:41:38 +0100 Subject: [PATCH] add resources to template --- .../main/java/sonia/scm/ScmServletModule.java | 4 +- .../scm/plugin/AbstractResourceServlet.java | 197 ------------ .../scm/plugin/ScriptResourceServlet.java | 285 ------------------ .../sonia/scm/template/TemplateServlet.java | 11 +- scm-webapp/src/main/webapp/index.html | 7 +- 5 files changed, 18 insertions(+), 486 deletions(-) delete mode 100644 scm-webapp/src/main/java/sonia/scm/plugin/AbstractResourceServlet.java delete mode 100644 scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java diff --git a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java index 8ca980f89c..739c88e0ff 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java @@ -61,7 +61,6 @@ import sonia.scm.plugin.DefaultPluginManager; import sonia.scm.plugin.Plugin; import sonia.scm.plugin.PluginLoader; import sonia.scm.plugin.PluginManager; -import sonia.scm.plugin.ScriptResourceServlet; import sonia.scm.repository.ChangesetViewerUtil; import sonia.scm.repository.DefaultRepositoryProvider; import sonia.scm.repository.Repository; @@ -72,6 +71,7 @@ import sonia.scm.repository.xml.XmlRepositoryManager; import sonia.scm.resources.DefaultResourceManager; import sonia.scm.resources.DevelopmentResourceManager; import sonia.scm.resources.ResourceManager; +import sonia.scm.resources.ScriptResourceServlet; import sonia.scm.security.CipherHandler; import sonia.scm.security.CipherUtil; import sonia.scm.security.EncryptionHandler; @@ -147,7 +147,7 @@ public class ScmServletModule extends ServletModule public static final String PATTERN_PAGE = "*.html"; /** Field description */ - public static final String PATTERN_PLUGIN_SCRIPT = "/plugins/sonia.plugin.js"; + public static final String PATTERN_PLUGIN_SCRIPT = "/plugins/resources/js/*"; /** Field description */ public static final String PATTERN_RESTAPI = "/api/rest/*"; diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/AbstractResourceServlet.java b/scm-webapp/src/main/java/sonia/scm/plugin/AbstractResourceServlet.java deleted file mode 100644 index 54cedd28af..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/plugin/AbstractResourceServlet.java +++ /dev/null @@ -1,197 +0,0 @@ -/** - * Copyright (c) 2010, Sebastian Sdorra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * http://bitbucket.org/sdorra/scm-manager - * - */ - -package sonia.scm.plugin; - -//~--- non-JDK imports -------------------------------------------------------- - -import sonia.scm.util.IOUtil; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * - * @author Sebastian Sdorra - */ -public abstract class AbstractResourceServlet extends HttpServlet -{ - - /** Field description */ - public static final String DEFAULT_CHARSET = "UTF-8"; - - /** Field description */ - private static final long serialVersionUID = -7703282364120349053L; - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param stream - * - * @throws IOException - * @throws ServletException - */ - protected abstract void appendResources(OutputStream stream) - throws ServletException, IOException; - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - protected abstract String getContentType(); - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @throws ServletException - */ - @Override - public void init() throws ServletException - { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - - try - { - appendResources(output); - } - catch (IOException ex) - { - throw new ServletException(ex); - } - finally - { - IOUtil.close(output); - } - - this.content = output.toByteArray(); - } - - /** - * Method description - * - * - * @param request - * @param response - * - * @throws IOException - * @throws ServletException - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException - { - processRequest(response); - } - - /** - * Method description - * - * - * @param request - * @param response - * - * @throws IOException - * @throws ServletException - */ - @Override - protected void doPost(HttpServletRequest request, - HttpServletResponse response) - throws ServletException, IOException - { - processRequest(response); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - protected String getCharacterEncoding() - { - return DEFAULT_CHARSET; - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param response - * - * @throws IOException - * @throws ServletException - */ - private void processRequest(HttpServletResponse response) - throws ServletException, IOException - { - response.setCharacterEncoding(getCharacterEncoding()); - response.setContentType(getContentType()); - response.setContentLength(content.length); - - OutputStream output = response.getOutputStream(); - - try - { - output.write(content); - } - finally - { - IOUtil.close(output); - } - } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private byte[] content; -} diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java b/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java deleted file mode 100644 index 7f8d31fc1e..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java +++ /dev/null @@ -1,285 +0,0 @@ -/** - * Copyright (c) 2010, Sebastian Sdorra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * http://bitbucket.org/sdorra/scm-manager - * - */ - - - -package sonia.scm.plugin; - -//~--- non-JDK imports -------------------------------------------------------- - -import com.google.inject.Inject; -import com.google.inject.Singleton; - -import sonia.scm.boot.BootstrapUtil; -import sonia.scm.resources.ResourceHandler; -import sonia.scm.resources.ResourceHandlerComparator; -import sonia.scm.resources.ResourceType; -import sonia.scm.util.IOUtil; -import sonia.scm.util.Util; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import javax.servlet.ServletException; - -/** - * - * @author Sebastian Sdorra - */ -@Singleton -public class ScriptResourceServlet extends AbstractResourceServlet -{ - - /** Field description */ - public static final String CONTENT_TYPE = "text/javascript"; - - /** Field description */ - private static final long serialVersionUID = -5769146163848821050L; - - //~--- constructors --------------------------------------------------------- - - /** - * Constructs ... - * - * - * @param pluginLoader - * @param resourceHandlers - */ - @Inject - public ScriptResourceServlet(PluginLoader pluginLoader, - Set resourceHandlers) - { - this.pluginLoader = pluginLoader; - this.resourceHandlers = resourceHandlers; - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param stream - * - * @throws IOException - * @throws ServletException - */ - @Override - protected void appendResources(OutputStream stream) - throws ServletException, IOException - { - stream.write( - "function sayPluginHello(){ alert('Plugin Hello !'); }".concat( - System.getProperty("line.separator")).getBytes()); - - Collection scriptResources = getScriptResources(); - - if (Util.isNotEmpty(scriptResources)) - { - for (String resource : scriptResources) - { - appendResource(stream, resource); - } - } - - if (Util.isNotEmpty(resourceHandlers)) - { - List handlerList = - new ArrayList(resourceHandlers); - - Collections.sort(handlerList, new ResourceHandlerComparator()); - - for (ResourceHandler resourceHandler : resourceHandlers) - { - if (resourceHandler.getType() == ResourceType.SCRIPT) - { - appendResource(resourceHandler.getResource(), stream); - } - } - } - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - @Override - protected String getContentType() - { - return CONTENT_TYPE; - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param stream - * @param script - * - * @throws IOException - * @throws ServletException - */ - private void appendResource(OutputStream stream, String script) - throws ServletException, IOException - { - InputStream input = getResourceAsStream(script); - - appendResource(input, stream); - } - - /** - * Method description - * - * - * @param input - * @param stream - * - * @throws IOException - */ - private void appendResource(InputStream input, OutputStream stream) - throws IOException - { - if (input != null) - { - try - { - IOUtil.copy(input, stream); - } - finally - { - IOUtil.close(input); - } - } - } - - /** - * Method description - * - * - * @param resources - * @param plugin - */ - private void processPlugin(Set resources, Plugin plugin) - { - PluginResources pluginResources = plugin.getResources(); - - if (pluginResources != null) - { - Set scriptResources = pluginResources.getScriptResources(); - - if (scriptResources != null) - { - resources.addAll(scriptResources); - } - } - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param resource - * - * @return - */ - private InputStream getResourceAsStream(String resource) - { - InputStream input = null; - ClassLoader classLoader = BootstrapUtil.getClassLoader(getServletContext()); - - if (classLoader != null) - { - String classLoaderResource = resource; - - if (classLoaderResource.startsWith("/")) - { - classLoaderResource = classLoaderResource.substring(1); - } - - input = classLoader.getResourceAsStream(classLoaderResource); - } - - if (input == null) - { - input = ScriptResourceServlet.class.getResourceAsStream(resource); - } - - return input; - } - - /** - * Method description - * - * - * @return - */ - private Collection getScriptResources() - { - Set resources = new TreeSet(); - Collection plugins = pluginLoader.getInstalledPlugins(); - - if (plugins != null) - { - for (Plugin plugin : plugins) - { - processPlugin(resources, plugin); - } - } - - return resources; - } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private PluginLoader pluginLoader; - - /** Field description */ - private Set resourceHandlers; -} diff --git a/scm-webapp/src/main/java/sonia/scm/template/TemplateServlet.java b/scm-webapp/src/main/java/sonia/scm/template/TemplateServlet.java index 2785b5a6e2..2062bbdee8 100644 --- a/scm-webapp/src/main/java/sonia/scm/template/TemplateServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/template/TemplateServlet.java @@ -39,6 +39,8 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import sonia.scm.SCMContextProvider; +import sonia.scm.resources.ResourceManager; +import sonia.scm.resources.ResourceType; import sonia.scm.util.IOUtil; //~--- JDK imports ------------------------------------------------------------ @@ -81,12 +83,15 @@ public class TemplateServlet extends HttpServlet * * @param context * @param templateHandler + * @param resourceManager */ @Inject public TemplateServlet(SCMContextProvider context, - TemplateHandler templateHandler) + TemplateHandler templateHandler, + ResourceManager resourceManager) { this.templateHandler = templateHandler; + this.resourceManager = resourceManager; this.version = context.getVersion(); } @@ -111,6 +116,7 @@ public class TemplateServlet extends HttpServlet params.put("contextPath", contextPath); params.put("version", version); + params.put("scripts", resourceManager.getResources(ResourceType.SCRIPT)); Locale l = request.getLocale(); @@ -174,6 +180,9 @@ public class TemplateServlet extends HttpServlet //~--- fields --------------------------------------------------------------- + /** Field description */ + private ResourceManager resourceManager; + /** Field description */ private TemplateHandler templateHandler; diff --git a/scm-webapp/src/main/webapp/index.html b/scm-webapp/src/main/webapp/index.html index 5adf7a8375..4ec4388743 100644 --- a/scm-webapp/src/main/webapp/index.html +++ b/scm-webapp/src/main/webapp/index.html @@ -152,7 +152,12 @@ - + + + <#list scripts as script> + + + <#if country != 'en'>