diff --git a/scm-annotations/src/main/java/sonia/scm/filter/WebElement.java b/scm-annotations/src/main/java/sonia/scm/filter/WebElement.java new file mode 100644 index 0000000000..1360111872 --- /dev/null +++ b/scm-annotations/src/main/java/sonia/scm/filter/WebElement.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2014, 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.filter; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.plugin.PluginAnnotation; + +//~--- JDK imports ------------------------------------------------------------ + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to register servlets and filters. The annotation is automatically + * picked up by the plugin registration processor of SCM-Manager. + * + * @author Sebastian Sdorra + * @since 2.0.0 + */ +@Documented +@Target(ElementType.TYPE) +@PluginAnnotation("web-element") +@Retention(RetentionPolicy.RUNTIME) +public @interface WebElement +{ + /** + * Returns filter or servlet path pattern. The path can be specified as glob + * or as regex. + * + * @return mapping path + */ + public String value(); + + /** + * Returns additional filter or servlet path patterns. The path can be + * specified as glob or as regex. + * + * @return mapping paths + */ + public String[] morePatterns() default {}; + + /** + * Returns {@code true} if the path patterns are specified as regex patterns. + * Default is {@code false}. + * + * @return {@code true} if the path patterns are specified as regex patterns + */ + public boolean regex() default false; + + /** + * Returns an array of init params. + * + * @return array of init params + */ + public WebInitParam[] initParams() default {}; +} diff --git a/scm-annotations/src/main/java/sonia/scm/filter/WebInitParam.java b/scm-annotations/src/main/java/sonia/scm/filter/WebInitParam.java new file mode 100644 index 0000000000..910af9b8a7 --- /dev/null +++ b/scm-annotations/src/main/java/sonia/scm/filter/WebInitParam.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2014, 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.filter; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Init param for servlet of filter registration. This annotation can only be + * used with {@link WebElement}. + * + * @author Sebastian Sdorra + */ +@Documented +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface WebInitParam +{ + /** + * Name of the init parameter. + * + * @return name of init parameter + */ + public String name(); + + /** + * Value of the init parameter. + * + * @return value of init parameter + */ + public String value(); +} diff --git a/scm-core/src/main/java/sonia/scm/plugin/ExtensionProcessor.java b/scm-core/src/main/java/sonia/scm/plugin/ExtensionProcessor.java index b059238eba..39f316262b 100644 --- a/scm-core/src/main/java/sonia/scm/plugin/ExtensionProcessor.java +++ b/scm-core/src/main/java/sonia/scm/plugin/ExtensionProcessor.java @@ -74,4 +74,14 @@ public interface ExtensionProcessor * @param binder injection binder */ public void processAutoBindExtensions(Binder binder); + + //~--- get methods ---------------------------------------------------------- + + /** + * Returns all collected web elements (servlets and filters). + * + * + * @return collected web elements + */ + public Iterable getWebElements(); } diff --git a/scm-core/src/main/java/sonia/scm/plugin/ScmModule.java b/scm-core/src/main/java/sonia/scm/plugin/ScmModule.java index 2bb6cbcf36..683b2a7e0c 100644 --- a/scm-core/src/main/java/sonia/scm/plugin/ScmModule.java +++ b/scm-core/src/main/java/sonia/scm/plugin/ScmModule.java @@ -127,6 +127,17 @@ public class ScmModule return nonNull(subscribers); } + /** + * Method description + * + * + * @return + */ + public Iterable getWebElements() + { + return nonNull(webElements); + } + //~--- methods -------------------------------------------------------------- /** @@ -225,4 +236,8 @@ public class ScmModule /** Field description */ @XmlElement(name = "subscriber") private Set subscribers; + + /** Field description */ + @XmlElement(name = "web-element") + private Set webElements; } diff --git a/scm-core/src/main/java/sonia/scm/plugin/WebElementDescriptor.java b/scm-core/src/main/java/sonia/scm/plugin/WebElementDescriptor.java new file mode 100644 index 0000000000..9e7f438f1c --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/plugin/WebElementDescriptor.java @@ -0,0 +1,149 @@ +/** + * Copyright (c) 2014, 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; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Arrays; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sebastian Sdorra + * @since 2.0.0 + */ +@XmlRootElement(name = "web-element") +@XmlAccessorType(XmlAccessType.FIELD) +public final class WebElementDescriptor +{ + + /** + * Constructs ... + * + */ + WebElementDescriptor() {} + + /** + * Constructs ... + * + * + * @param clazz + * @param pattern + * @param morePatterns + * @param regex + */ + public WebElementDescriptor(Class clazz, String pattern, + String[] morePatterns, boolean regex) + { + this.clazz = clazz; + this.pattern = pattern; + this.morePatterns = morePatterns; + this.regex = regex; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public Class getClazz() + { + return clazz; + } + + /** + * Method description + * + * + * @return + */ + public String[] getMorePatterns() + { + String[] patterns; + + if (morePatterns != null) + { + patterns = Arrays.copyOf(morePatterns, morePatterns.length); + } + else + { + patterns = new String[0]; + } + + return patterns; + } + + /** + * Method description + * + * + * @return + */ + public String getPattern() + { + return pattern; + } + + /** + * Method description + * + * + * @return + */ + public boolean isRegex() + { + return regex; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlElement(name = "class") + private Class clazz; + + /** Field description */ + private String[] morePatterns; + + /** Field description */ + @XmlElement(name = "value") + private String pattern; + + /** Field description */ + private boolean regex = false; +} diff --git a/scm-plugins/scm-legacy-plugin/pom.xml b/scm-plugins/scm-legacy-plugin/pom.xml index eb12b72dfb..31d8422fbf 100644 --- a/scm-plugins/scm-legacy-plugin/pom.xml +++ b/scm-plugins/scm-legacy-plugin/pom.xml @@ -10,4 +10,17 @@ scm-legacy-plugin 2.0.0-SNAPSHOT smp + + + + + + + javax.servlet + javax.servlet-api + ${servlet.version} + provided + + + \ No newline at end of file diff --git a/scm-webapp/src/main/java/sonia/scm/ScmContextListener.java b/scm-webapp/src/main/java/sonia/scm/ScmContextListener.java index 2ad6bc72d6..26f5283677 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmContextListener.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmContextListener.java @@ -66,6 +66,7 @@ import java.util.Set; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; +import sonia.scm.filter.WebElementModule; /** * @@ -255,6 +256,7 @@ public class ScmContextListener extends GuiceServletContextListener moduleList.add(new ScmEventBusModule()); moduleList.add(new EagerSingletonModule()); moduleList.add(ShiroWebModule.guiceFilterModule()); + moduleList.add(new WebElementModule(pluginLoader)); moduleList.add(new ScmServletModule(servletCtx, pluginLoader, overrides)); moduleList.add( new ScmSecurityModule(servletCtx, pluginLoader.getExtensionProcessor()) diff --git a/scm-webapp/src/main/java/sonia/scm/filter/TypedWebElementDescriptor.java b/scm-webapp/src/main/java/sonia/scm/filter/TypedWebElementDescriptor.java new file mode 100644 index 0000000000..8e9e576b0c --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/filter/TypedWebElementDescriptor.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2014, 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.filter; + +import sonia.scm.plugin.WebElementDescriptor; + +/** + * + * @author Sebastian Sdorra + * @param + */ +public final class TypedWebElementDescriptor +{ + private final Class clazz; + private final WebElementDescriptor descriptor; + + public TypedWebElementDescriptor(Class clazz, + WebElementDescriptor descriptor) + { + this.clazz = clazz; + this.descriptor = descriptor; + } + + public Class getClazz() + { + return clazz; + } + + public WebElementDescriptor getDescriptor() + { + return descriptor; + } + +} diff --git a/scm-webapp/src/main/java/sonia/scm/filter/WebElementCollector.java b/scm-webapp/src/main/java/sonia/scm/filter/WebElementCollector.java new file mode 100644 index 0000000000..3d19d0d965 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/filter/WebElementCollector.java @@ -0,0 +1,191 @@ +/** + * Copyright (c) 2014, 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.filter; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import com.google.common.primitives.Ints; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.Priorities; +import sonia.scm.plugin.PluginLoader; +import sonia.scm.plugin.WebElementDescriptor; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.List; + +import javax.servlet.Filter; +import javax.servlet.Servlet; +import javax.servlet.http.HttpServlet; + +/** + * + * @author Sebastian Sdorra + */ +public final class WebElementCollector +{ + + /** + * the logger for WebElementCollector + */ + private static final Logger logger = + LoggerFactory.getLogger(WebElementCollector.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param elements + */ + @SuppressWarnings("unchecked") + private WebElementCollector(Iterable elements) + { + List> fl = Lists.newArrayList(); + List> sl = + Lists.newArrayList(); + + for (WebElementDescriptor element : elements) + { + if (Filter.class.isAssignableFrom(element.getClazz())) + { + fl.add( + new TypedWebElementDescriptor<>( + (Class) element.getClazz(), element)); + } + else if (Servlet.class.isAssignableFrom(element.getClazz())) + { + sl.add( + new TypedWebElementDescriptor<>( + (Class) element.getClazz(), element)); + } + else + { + logger.warn( + "found class {} witch is annotated with webelement annotation, but is neither an filter or servlet", + element.getClazz()); + } + } + + TypedWebElementDescriptorOrdering ordering = + new TypedWebElementDescriptorOrdering(); + + filters = ordering.immutableSortedCopy(fl); + servlets = ordering.immutableSortedCopy(sl); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param loader + * + * @return + */ + public static WebElementCollector collect(PluginLoader loader) + { + return new WebElementCollector( + loader.getExtensionProcessor().getWebElements()); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public Iterable> getFilters() + { + return filters; + } + + /** + * Method description + * + * + * @return + */ + public Iterable> getServlets() + { + return servlets; + } + + //~--- inner classes -------------------------------------------------------- + + /** + * Class description + * + * + * @version Enter version here..., 15/02/01 + * @author Enter your name here... + */ + private static class TypedWebElementDescriptorOrdering + extends Ordering> + { + + /** + * Method description + * + * + * @param left + * @param right + * + * @return + */ + @Override + public int compare(TypedWebElementDescriptor left, + TypedWebElementDescriptor right) + { + return Ints.compare(Priorities.getPriority(left.getClazz()), + Priorities.getPriority(right.getClazz())); + } + } + + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private final Iterable> filters; + + /** Field description */ + private final Iterable> servlets; +} diff --git a/scm-webapp/src/main/java/sonia/scm/filter/WebElementModule.java b/scm-webapp/src/main/java/sonia/scm/filter/WebElementModule.java new file mode 100644 index 0000000000..66e2fd1434 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/filter/WebElementModule.java @@ -0,0 +1,164 @@ +/** + * Copyright (c) 2014, 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.filter; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Scopes; +import com.google.inject.servlet.ServletModule; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.plugin.PluginLoader; +import sonia.scm.plugin.WebElementDescriptor; + +//~--- JDK imports ------------------------------------------------------------ + +import javax.servlet.Filter; +import javax.servlet.http.HttpServlet; + +/** + * + * @author Sebastian Sdorra + */ +public class WebElementModule extends ServletModule +{ + + /** + * the logger for WebElementModule + */ + private static final Logger logger = + LoggerFactory.getLogger(WebElementModule.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param pluginLoader + */ + public WebElementModule(PluginLoader pluginLoader) + { + collector = WebElementCollector.collect(pluginLoader); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + */ + @Override + protected void configureServlets() + { + for (TypedWebElementDescriptor f : collector.getFilters()) + { + bindFilter(f); + } + + for (TypedWebElementDescriptor s : + collector.getServlets()) + { + bindServlet(s); + } + } + + /** + * Method description + * + * + * @param filter + */ + private void bindFilter(TypedWebElementDescriptor filter) + { + Class clazz = filter.getClazz(); + + logger.info("bind filter {} to filter chain", clazz); + + // filters must be in singleton scope + bind(clazz).in(Scopes.SINGLETON); + + WebElementDescriptor opts = filter.getDescriptor(); + FilterKeyBindingBuilder builder; + + if (opts.isRegex()) + { + builder = filterRegex(opts.getPattern(), opts.getMorePatterns()); + } + else + { + builder = filter(opts.getPattern(), opts.getMorePatterns()); + } + + // TODO handle init parameters + builder.through(clazz); + } + + /** + * Method description + * + * + * @param servlet + */ + private void bindServlet( + TypedWebElementDescriptor servlet) + { + Class clazz = servlet.getClazz(); + + logger.info("bind servlet {} to servlet chain", clazz); + + // filters must be in singleton scope + bind(clazz).in(Scopes.SINGLETON); + + WebElementDescriptor opts = servlet.getDescriptor(); + ServletKeyBindingBuilder builder; + + if (opts.isRegex()) + { + builder = serveRegex(opts.getPattern(), opts.getMorePatterns()); + } + else + { + builder = serve(opts.getPattern(), opts.getMorePatterns()); + } + + // TODO handle init parameters + builder.with(clazz); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private final WebElementCollector collector; +} diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultExtensionProcessor.java b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultExtensionProcessor.java index 4c2ef8767d..053779b25d 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultExtensionProcessor.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultExtensionProcessor.java @@ -113,6 +113,20 @@ public class DefaultExtensionProcessor implements ExtensionProcessor logger.info("bound extensions in {}", sw.stop()); } + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public Iterable getWebElements() + { + return collector.getWebElements(); + } + //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/ExtensionCollector.java b/scm-webapp/src/main/java/sonia/scm/plugin/ExtensionCollector.java index d374bd3a8d..9ecf9a9da8 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/ExtensionCollector.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/ExtensionCollector.java @@ -205,6 +205,17 @@ public final class ExtensionCollector return restResources; } + /** + * Method description + * + * + * @return + */ + public Set getWebElements() + { + return webElements; + } + //~--- methods -------------------------------------------------------------- /** @@ -263,18 +274,22 @@ public final class ExtensionCollector restProviders.addAll(Lists.newArrayList(module.getRestProviders())); restResources.addAll(Lists.newArrayList(module.getRestResources())); + Iterables.addAll(webElements, module.getWebElements()); } //~--- fields --------------------------------------------------------------- /** Field description */ - private final Set looseExtensions = Sets.newHashSet(); + private final Set webElements = Sets.newHashSet(); + + /** Field description */ + private final Set restResources = Sets.newHashSet(); /** Field description */ private final Set restProviders = Sets.newHashSet(); /** Field description */ - private final Set restResources = Sets.newHashSet(); + private final Set looseExtensions = Sets.newHashSet(); /** Field description */ private final Multimap extensions =