2010-09-16 21:50:30 +02:00
|
|
|
/*
|
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
|
* and open the template in the editor.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2010-10-12 09:16:40 +02:00
|
|
|
import com.google.inject.multibindings.Multibinder;
|
2010-09-16 21:50:30 +02:00
|
|
|
import com.google.inject.servlet.ServletModule;
|
|
|
|
|
|
2010-10-16 11:03:54 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2010-09-16 21:50:30 +02:00
|
|
|
import sonia.scm.api.rest.UriExtensionsConfig;
|
2010-10-14 07:58:51 +02:00
|
|
|
import sonia.scm.cache.CacheManager;
|
|
|
|
|
import sonia.scm.cache.CacheRepositoryManagerDecorator;
|
|
|
|
|
import sonia.scm.cache.EhCacheManager;
|
2010-10-15 17:58:16 +02:00
|
|
|
import sonia.scm.filter.SecurityFilter;
|
2010-09-16 21:50:30 +02:00
|
|
|
import sonia.scm.plugin.ScriptResourceServlet;
|
2010-10-12 09:16:40 +02:00
|
|
|
import sonia.scm.repository.BasicRepositoryManager;
|
|
|
|
|
import sonia.scm.repository.RepositoryHandler;
|
2010-09-16 21:50:30 +02:00
|
|
|
import sonia.scm.repository.RepositoryManager;
|
2010-10-31 11:47:16 +01:00
|
|
|
import sonia.scm.security.EncryptionHandler;
|
|
|
|
|
import sonia.scm.security.MessageDigestEncryptionHandler;
|
2010-10-15 17:58:16 +02:00
|
|
|
import sonia.scm.util.DebugServlet;
|
2010-10-31 13:07:43 +01:00
|
|
|
import sonia.scm.util.Util;
|
|
|
|
|
import sonia.scm.web.plugin.SCMPlugin;
|
|
|
|
|
import sonia.scm.web.plugin.SCMPluginManager;
|
|
|
|
|
import sonia.scm.web.plugin.ScmWebPluginContext;
|
2010-09-26 15:53:26 +02:00
|
|
|
import sonia.scm.web.security.Authenticator;
|
2010-10-15 17:58:16 +02:00
|
|
|
import sonia.scm.web.security.BasicSecurityContext;
|
|
|
|
|
import sonia.scm.web.security.SecurityContext;
|
2010-10-31 11:47:16 +01:00
|
|
|
import sonia.scm.web.security.XmlAuthenticator;
|
2010-09-16 21:50:30 +02:00
|
|
|
|
|
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import com.sun.jersey.api.core.PackagesResourceConfig;
|
|
|
|
|
import com.sun.jersey.api.core.ResourceConfig;
|
|
|
|
|
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
|
|
|
|
|
import com.sun.jersey.spi.container.servlet.ServletContainer;
|
|
|
|
|
|
2010-10-12 09:16:40 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2010-10-31 13:07:43 +01:00
|
|
|
import java.util.Collection;
|
2010-09-16 21:50:30 +02:00
|
|
|
import java.util.HashMap;
|
2010-10-31 13:07:43 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2010-09-16 21:50:30 +02:00
|
|
|
import java.util.Map;
|
2010-10-31 13:07:43 +01:00
|
|
|
import java.util.Set;
|
2010-09-16 21:50:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public class ScmServletModule extends ServletModule
|
|
|
|
|
{
|
|
|
|
|
|
2010-10-15 17:58:16 +02:00
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_DEBUG = "/debug.html";
|
|
|
|
|
|
2010-09-16 21:50:30 +02:00
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_PAGE = "*.html";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_PLUGIN_SCRIPT = "/plugins/sonia.plugin.js";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_RESTAPI = "/api/rest/*";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_SCRIPT = "*.js";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String PATTERN_STYLESHEET = "*.css";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String REST_PACKAGE = "sonia.scm.api.rest";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String[] PATTERN_STATIC_RESOURCES = new String[] {
|
|
|
|
|
PATTERN_SCRIPT,
|
|
|
|
|
PATTERN_STYLESHEET, "*.jpg", "*.gif", "*.png" };
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String[] PATTERN_COMPRESSABLE = new String[] {
|
|
|
|
|
PATTERN_SCRIPT,
|
|
|
|
|
PATTERN_STYLESHEET, "*.json", "*.xml", "*.txt" };
|
|
|
|
|
|
2010-10-16 11:03:54 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static Logger logger =
|
|
|
|
|
LoggerFactory.getLogger(ScmServletModule.class);
|
|
|
|
|
|
2010-09-16 21:50:30 +02:00
|
|
|
//~--- constructors ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs ...
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param webPluginContext
|
|
|
|
|
*/
|
|
|
|
|
ScmServletModule(ScmWebPluginContext webPluginContext)
|
|
|
|
|
{
|
|
|
|
|
this.webPluginContext = webPluginContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void configureServlets()
|
|
|
|
|
{
|
|
|
|
|
SCMContextProvider context = SCMContext.getContext();
|
|
|
|
|
|
|
|
|
|
bind(SCMContextProvider.class).toInstance(context);
|
2010-10-31 11:47:16 +01:00
|
|
|
bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
|
|
|
|
|
bind(Authenticator.class).to(XmlAuthenticator.class);
|
2010-10-15 17:58:16 +02:00
|
|
|
bind(SecurityContext.class).to(BasicSecurityContext.class);
|
2010-10-12 09:16:40 +02:00
|
|
|
|
|
|
|
|
SCMPluginManager pluginManager = new SCMPluginManager();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pluginManager.load();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
2010-10-16 11:03:54 +02:00
|
|
|
logger.error(ex.getMessage(), ex);
|
2010-10-12 09:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-31 13:07:43 +01:00
|
|
|
loadPlugins(pluginManager);
|
2010-10-14 07:58:51 +02:00
|
|
|
bind(CacheManager.class).to(EhCacheManager.class);
|
|
|
|
|
bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
|
|
|
|
|
BasicRepositoryManager.class);
|
|
|
|
|
bind(RepositoryManager.class).to(CacheRepositoryManagerDecorator.class);
|
2010-09-16 21:50:30 +02:00
|
|
|
bind(ScmWebPluginContext.class).toInstance(webPluginContext);
|
|
|
|
|
|
2010-09-29 09:10:41 +02:00
|
|
|
/*
|
|
|
|
|
* filter(PATTERN_PAGE,
|
|
|
|
|
* PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class);
|
|
|
|
|
* filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class);
|
|
|
|
|
* filter(PATTERN_RESTAPI).through(SecurityFilter.class);
|
|
|
|
|
*/
|
2010-10-15 17:58:16 +02:00
|
|
|
filter(PATTERN_RESTAPI, PATTERN_DEBUG).through(SecurityFilter.class);
|
|
|
|
|
|
|
|
|
|
// debug servlet
|
|
|
|
|
serve(PATTERN_DEBUG).with(DebugServlet.class);
|
2010-09-16 21:50:30 +02:00
|
|
|
|
|
|
|
|
// plugin resources
|
|
|
|
|
serve(PATTERN_PLUGIN_SCRIPT).with(ScriptResourceServlet.class);
|
|
|
|
|
|
|
|
|
|
// jersey
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* params.put("com.sun.jersey.spi.container.ContainerRequestFilters",
|
|
|
|
|
* "com.sun.jersey.api.container.filter.LoggingFilter");
|
|
|
|
|
* params.put("com.sun.jersey.spi.container.ContainerResponseFilters",
|
|
|
|
|
* "com.sun.jersey.api.container.filter.LoggingFilter");
|
|
|
|
|
* params.put("com.sun.jersey.config.feature.Trace", "true");
|
|
|
|
|
* params.put("com.sun.jersey.config.feature.TracePerRequest", "true");
|
|
|
|
|
*/
|
|
|
|
|
params.put(ResourceConfig.FEATURE_REDIRECT, Boolean.TRUE.toString());
|
|
|
|
|
params.put(ServletContainer.RESOURCE_CONFIG_CLASS,
|
|
|
|
|
UriExtensionsConfig.class.getName());
|
|
|
|
|
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, REST_PACKAGE);
|
|
|
|
|
serve(PATTERN_RESTAPI).with(GuiceContainer.class, params);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-31 13:07:43 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param repositoryHandlerBinder
|
|
|
|
|
* @param handlerSet
|
|
|
|
|
*/
|
|
|
|
|
private void bindRepositoryHandlers(
|
|
|
|
|
Multibinder<RepositoryHandler> repositoryHandlerBinder,
|
|
|
|
|
Set<Class<? extends RepositoryHandler>> handlerSet)
|
|
|
|
|
{
|
|
|
|
|
for (Class<? extends RepositoryHandler> handlerClass : handlerSet)
|
|
|
|
|
{
|
|
|
|
|
if (logger.isInfoEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.info("load RepositoryHandler {}", handlerClass.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bind(handlerClass);
|
|
|
|
|
repositoryHandlerBinder.addBinding().to(handlerClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param pluginManager
|
|
|
|
|
*/
|
|
|
|
|
private void loadPlugins(SCMPluginManager pluginManager)
|
|
|
|
|
{
|
|
|
|
|
Set<SCMPlugin> pluginSet = pluginManager.getPlugins();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(pluginSet))
|
|
|
|
|
{
|
|
|
|
|
Multibinder<RepositoryHandler> repositoryHandlerBinder =
|
|
|
|
|
Multibinder.newSetBinder(binder(), RepositoryHandler.class);
|
|
|
|
|
Set<Class<? extends RepositoryHandler>> handlerSet =
|
|
|
|
|
new LinkedHashSet<Class<? extends RepositoryHandler>>();
|
|
|
|
|
|
|
|
|
|
for (SCMPlugin plugin : pluginSet)
|
|
|
|
|
{
|
|
|
|
|
Collection<Class<? extends RepositoryHandler>> handlers =
|
|
|
|
|
plugin.getHandlers();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(handlers))
|
|
|
|
|
{
|
|
|
|
|
handlerSet.addAll(handlers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bindRepositoryHandlers(repositoryHandlerBinder, handlerSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 21:50:30 +02:00
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private ScmWebPluginContext webPluginContext;
|
|
|
|
|
}
|