mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
added module to register servlets and filters by new WebElement annotation
This commit is contained in:
@@ -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 {};
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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<WebElementDescriptor> getWebElements();
|
||||
}
|
||||
|
||||
@@ -127,6 +127,17 @@ public class ScmModule
|
||||
return nonNull(subscribers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Iterable<WebElementDescriptor> getWebElements()
|
||||
{
|
||||
return nonNull(webElements);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -225,4 +236,8 @@ public class ScmModule
|
||||
/** Field description */
|
||||
@XmlElement(name = "subscriber")
|
||||
private Set<SubscriberElement> subscribers;
|
||||
|
||||
/** Field description */
|
||||
@XmlElement(name = "web-element")
|
||||
private Set<WebElementDescriptor> webElements;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -10,4 +10,17 @@
|
||||
<artifactId>scm-legacy-plugin</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<packaging>smp</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- servlet api -->
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${servlet.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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())
|
||||
|
||||
@@ -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 <T>
|
||||
*/
|
||||
public final class TypedWebElementDescriptor<T>
|
||||
{
|
||||
private final Class<T> clazz;
|
||||
private final WebElementDescriptor descriptor;
|
||||
|
||||
public TypedWebElementDescriptor(Class<T> clazz,
|
||||
WebElementDescriptor descriptor)
|
||||
{
|
||||
this.clazz = clazz;
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
|
||||
public Class<T> getClazz()
|
||||
{
|
||||
return clazz;
|
||||
}
|
||||
|
||||
public WebElementDescriptor getDescriptor()
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<WebElementDescriptor> elements)
|
||||
{
|
||||
List<TypedWebElementDescriptor<? extends Filter>> fl = Lists.newArrayList();
|
||||
List<TypedWebElementDescriptor<? extends HttpServlet>> sl =
|
||||
Lists.newArrayList();
|
||||
|
||||
for (WebElementDescriptor element : elements)
|
||||
{
|
||||
if (Filter.class.isAssignableFrom(element.getClazz()))
|
||||
{
|
||||
fl.add(
|
||||
new TypedWebElementDescriptor<>(
|
||||
(Class<? extends Filter>) element.getClazz(), element));
|
||||
}
|
||||
else if (Servlet.class.isAssignableFrom(element.getClazz()))
|
||||
{
|
||||
sl.add(
|
||||
new TypedWebElementDescriptor<>(
|
||||
(Class<? extends HttpServlet>) 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<TypedWebElementDescriptor<? extends Filter>> getFilters()
|
||||
{
|
||||
return filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Iterable<TypedWebElementDescriptor<? extends HttpServlet>> 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<TypedWebElementDescriptor<?>>
|
||||
{
|
||||
|
||||
/**
|
||||
* 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<TypedWebElementDescriptor<? extends Filter>> filters;
|
||||
|
||||
/** Field description */
|
||||
private final Iterable<TypedWebElementDescriptor<? extends HttpServlet>> servlets;
|
||||
}
|
||||
164
scm-webapp/src/main/java/sonia/scm/filter/WebElementModule.java
Normal file
164
scm-webapp/src/main/java/sonia/scm/filter/WebElementModule.java
Normal file
@@ -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<? extends Filter> f : collector.getFilters())
|
||||
{
|
||||
bindFilter(f);
|
||||
}
|
||||
|
||||
for (TypedWebElementDescriptor<? extends HttpServlet> s :
|
||||
collector.getServlets())
|
||||
{
|
||||
bindServlet(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param filter
|
||||
*/
|
||||
private void bindFilter(TypedWebElementDescriptor<? extends Filter> filter)
|
||||
{
|
||||
Class<? extends Filter> 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<? extends HttpServlet> servlet)
|
||||
{
|
||||
Class<? extends HttpServlet> 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;
|
||||
}
|
||||
@@ -113,6 +113,20 @@ public class DefaultExtensionProcessor implements ExtensionProcessor
|
||||
logger.info("bound extensions in {}", sw.stop());
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Iterable<WebElementDescriptor> getWebElements()
|
||||
{
|
||||
return collector.getWebElements();
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -205,6 +205,17 @@ public final class ExtensionCollector
|
||||
return restResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<WebElementDescriptor> 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<Class> looseExtensions = Sets.newHashSet();
|
||||
private final Set<WebElementDescriptor> webElements = Sets.newHashSet();
|
||||
|
||||
/** Field description */
|
||||
private final Set<Class> restResources = Sets.newHashSet();
|
||||
|
||||
/** Field description */
|
||||
private final Set<Class> restProviders = Sets.newHashSet();
|
||||
|
||||
/** Field description */
|
||||
private final Set<Class> restResources = Sets.newHashSet();
|
||||
private final Set<Class> looseExtensions = Sets.newHashSet();
|
||||
|
||||
/** Field description */
|
||||
private final Multimap<ExtensionPointElement, Class> extensions =
|
||||
|
||||
Reference in New Issue
Block a user