Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java

335 lines
10 KiB
Java
Raw Normal View History

2010-10-31 19:22:53 +01:00
/**
* 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
*
2010-09-16 21:50:30 +02:00
*/
2010-11-06 16:39:10 +01:00
2010-09-16 21:50:30 +02:00
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;
import sonia.scm.cache.CacheManager;
import sonia.scm.cache.EhCacheManager;
2010-11-19 18:50:42 +01:00
import sonia.scm.config.ScmConfiguration;
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.RepositoryHandler;
2010-09-16 21:50:30 +02:00
import sonia.scm.repository.RepositoryManager;
2010-11-26 17:16:26 +01:00
import sonia.scm.repository.xml.XmlRepositoryManager;
2010-10-31 11:47:16 +01:00
import sonia.scm.security.EncryptionHandler;
import sonia.scm.security.MessageDigestEncryptionHandler;
2010-11-06 16:39:10 +01:00
import sonia.scm.user.UserManager;
2010-11-26 17:16:26 +01:00
import sonia.scm.user.xml.XmlUserManager;
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-10-31 13:20:53 +01:00
import sonia.scm.web.plugin.SecurityConfig;
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-11-19 18:50:42 +01:00
import java.io.File;
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
2010-11-19 18:50:42 +01:00
import javax.xml.bind.JAXB;
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 pluginManager
2010-09-16 21:50:30 +02:00
* @param webPluginContext
*/
ScmServletModule(SCMPluginManager pluginManager,
ScmWebPluginContext webPluginContext)
2010-09-16 21:50:30 +02:00
{
this.pluginManager = pluginManager;
2010-09-16 21:50:30 +02:00
this.webPluginContext = webPluginContext;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
@Override
protected void configureServlets()
{
SCMContextProvider context = SCMContext.getContext();
bind(SCMContextProvider.class).toInstance(context);
2010-10-31 13:20:53 +01:00
2010-11-19 18:50:42 +01:00
ScmConfiguration config = getScmConfiguration(context);
bind(ScmConfiguration.class).toInstance(config);
2010-10-31 13:20:53 +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-31 13:07:43 +01:00
loadPlugins(pluginManager);
bind(CacheManager.class).to(EhCacheManager.class);
2010-11-26 17:16:26 +01:00
// bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
// BasicRepositoryManager.class);
bind(RepositoryManager.class).to(XmlRepositoryManager.class);
2010-11-26 15:37:35 +01:00
bind(UserManager.class).to(XmlUserManager.class);
2010-09-16 21:50:30 +02:00
bind(ScmWebPluginContext.class).toInstance(webPluginContext);
2010-11-19 18:50:42 +01:00
// filter(PATTERN_RESTAPI).through(LoggingFilter.class);
2010-09-16 21:50:30 +02:00
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))
{
2010-11-06 16:39:10 +01:00
// repository handlers
2010-10-31 13:07:43 +01:00
Multibinder<RepositoryHandler> repositoryHandlerBinder =
Multibinder.newSetBinder(binder(), RepositoryHandler.class);
2010-11-06 16:39:10 +01:00
Set<Class<? extends RepositoryHandler>> repositoryHandlerSet =
2010-10-31 13:07:43 +01:00
new LinkedHashSet<Class<? extends RepositoryHandler>>();
2010-11-06 16:39:10 +01:00
// security stuff
2010-10-31 13:20:53 +01:00
Class<? extends EncryptionHandler> encryptionHandler =
MessageDigestEncryptionHandler.class;
Class<? extends Authenticator> authenticator = XmlAuthenticator.class;
2010-10-31 13:07:43 +01:00
for (SCMPlugin plugin : pluginSet)
{
2010-11-06 16:39:10 +01:00
Collection<Class<? extends RepositoryHandler>> pluginRepositoryHandlers =
plugin.getRepositoryHandlers();
if (Util.isNotEmpty(pluginRepositoryHandlers))
{
repositoryHandlerSet.addAll(pluginRepositoryHandlers);
}
2010-10-31 13:20:53 +01:00
SecurityConfig securityConfig = plugin.getSecurityConfig();
if (securityConfig != null)
{
Class<? extends EncryptionHandler> enc =
securityConfig.getEncryptionHandler();
if (enc != null)
{
encryptionHandler = enc;
}
Class<? extends Authenticator> auth =
securityConfig.getAuthenticator();
if (auth != null)
{
authenticator = auth;
}
}
2010-10-31 13:07:43 +01:00
}
2010-10-31 13:20:53 +01:00
bind(EncryptionHandler.class).to(encryptionHandler);
bind(Authenticator.class).to(authenticator);
2010-11-06 16:39:10 +01:00
bindRepositoryHandlers(repositoryHandlerBinder, repositoryHandlerSet);
2010-10-31 13:07:43 +01:00
}
}
2010-11-19 18:50:42 +01:00
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param context
*
* @return
*/
private ScmConfiguration getScmConfiguration(SCMContextProvider context)
{
ScmConfiguration config = null;
File file = new File(context.getBaseDirectory(), ScmConfiguration.PATH);
if (file.exists())
{
try
{
2010-11-19 19:17:54 +01:00
config = JAXB.unmarshal(file, ScmConfiguration.class);
2010-11-19 18:50:42 +01:00
}
catch (Exception ex)
{
logger.error(ex.getMessage(), ex);
}
}
if (config == null)
{
config = new ScmConfiguration();
}
return config;
}
2010-09-16 21:50:30 +02:00
//~--- fields ---------------------------------------------------------------
/** Field description */
private SCMPluginManager pluginManager;
2010-09-16 21:50:30 +02:00
/** Field description */
private ScmWebPluginContext webPluginContext;
}