From fc387cee2167e0e88765d66213a5ad438d796a46 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 22 Jan 2011 13:20:54 +0100 Subject: [PATCH] added public bzr url --- .../scm/repository/BzrRepositoryHandler.java | 51 +++++-- .../java/sonia/scm/web/BzrPublicFilter.java | 131 ++++++++++++++++++ .../java/sonia/scm/web/BzrServletModule.java | 9 +- .../java/sonia/scm/util/SecurityUtil.java | 28 ++++ 4 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrPublicFilter.java diff --git a/plugins/scm-bzr-plugin/src/main/java/sonia/scm/repository/BzrRepositoryHandler.java b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/repository/BzrRepositoryHandler.java index efdba8e56f..2a3bca6ce3 100644 --- a/plugins/scm-bzr-plugin/src/main/java/sonia/scm/repository/BzrRepositoryHandler.java +++ b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/repository/BzrRepositoryHandler.java @@ -36,15 +36,15 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- import com.google.inject.Inject; +import com.google.inject.Provider; import com.google.inject.Singleton; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import sonia.scm.Type; import sonia.scm.io.ExtendedCommand; import sonia.scm.plugin.ext.Extension; import sonia.scm.store.StoreFactory; +import sonia.scm.util.SecurityUtil; +import sonia.scm.web.security.WebSecurityContext; //~--- JDK imports ------------------------------------------------------------ @@ -60,6 +60,9 @@ public class BzrRepositoryHandler extends AbstractSimpleRepositoryHandler { + /** Field description */ + public static final String PUBLIC_RESOURCEPATH_BASE = "/public/bzr/"; + /** Field description */ public static final String TYPE_DISPLAYNAME = "Bazaar"; @@ -69,10 +72,6 @@ public class BzrRepositoryHandler /** Field description */ public static final Type TYPE = new Type(TYPE_NAME, TYPE_DISPLAYNAME); - /** the logger for BzrRepositoryHandler */ - private static final Logger logger = - LoggerFactory.getLogger(BzrRepositoryHandler.class); - //~--- constructors --------------------------------------------------------- /** @@ -80,11 +79,42 @@ public class BzrRepositoryHandler * * * @param storeFactory + * @param securityContextProvider */ @Inject - public BzrRepositoryHandler(StoreFactory storeFactory) + public BzrRepositoryHandler( + StoreFactory storeFactory, + Provider securityContextProvider) { super(storeFactory); + this.securityContextProvider = securityContextProvider; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param repository + * + * @return + */ + @Override + public String createResourcePath(Repository repository) + { + String resourcePath = null; + + if (SecurityUtil.isAnonymous(securityContextProvider)) + { + resourcePath = PUBLIC_RESOURCEPATH_BASE.concat(repository.getName()); + } + else + { + resourcePath = super.createResourcePath(repository); + } + + return resourcePath; } //~--- get methods ---------------------------------------------------------- @@ -145,4 +175,9 @@ public class BzrRepositoryHandler { return BzrConfig.class; } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Provider securityContextProvider; } diff --git a/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrPublicFilter.java b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrPublicFilter.java new file mode 100644 index 0000000000..0abc13c21b --- /dev/null +++ b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrPublicFilter.java @@ -0,0 +1,131 @@ +/** + * 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.web; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import sonia.scm.repository.BzrRepositoryHandler; +import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryManager; +import sonia.scm.util.HttpUtil; +import sonia.scm.web.filter.HttpFilter; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Sebastian Sdorra + */ +@Singleton +public class BzrPublicFilter extends HttpFilter +{ + + /** Field description */ + private static final Pattern REGEX_REPOSITORYNAME = + Pattern.compile("/public/bzr/([^/]+)"); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param repositoryManager + */ + @Inject + public BzrPublicFilter(RepositoryManager repositoryManager) + { + this.repositoryManager = repositoryManager; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param request + * @param response + * @param chain + * + * @throws IOException + * @throws ServletException + */ + @Override + protected void doFilter(HttpServletRequest request, + HttpServletResponse response, FilterChain chain) + throws IOException, ServletException + { + String requestURI = HttpUtil.getStrippedURI(request); + Matcher m = REGEX_REPOSITORYNAME.matcher(requestURI); + + if (m.find()) + { + String name = m.group(1); + Repository repository = + repositoryManager.get(BzrRepositoryHandler.TYPE_NAME, name); + + if ((repository != null) && repository.isPublicReadable()) + { + chain.doFilter(request, response); + } + else + { + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + } + } + else + { + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + } + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private RepositoryManager repositoryManager; +} diff --git a/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrServletModule.java b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrServletModule.java index 086a81cafc..e9491f19b8 100644 --- a/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrServletModule.java +++ b/plugins/scm-bzr-plugin/src/main/java/sonia/scm/web/BzrServletModule.java @@ -51,6 +51,9 @@ public class BzrServletModule extends ServletModule /** Field description */ public static final String MAPPING_BZR = "/bzr/*"; + /** Field description */ + public static final String MAPPING_PUBLICBZR = "/public/bzr/*"; + //~--- methods -------------------------------------------------------------- /** @@ -60,8 +63,10 @@ public class BzrServletModule extends ServletModule @Override protected void configureServlets() { - filter(MAPPING_BZR).through(BasicAuthenticationFilter.class); + filter(MAPPING_BZR, + MAPPING_PUBLICBZR).through(BasicAuthenticationFilter.class); filter(MAPPING_BZR).through(BzrPermissionFilter.class); - serve(MAPPING_BZR).with(BzrCGIServlet.class); + filter(MAPPING_PUBLICBZR).through(BzrPublicFilter.class); + serve(MAPPING_BZR, MAPPING_PUBLICBZR).with(BzrCGIServlet.class); } } diff --git a/scm-core/src/main/java/sonia/scm/util/SecurityUtil.java b/scm-core/src/main/java/sonia/scm/util/SecurityUtil.java index 30c03bc951..c753f57fd4 100644 --- a/scm-core/src/main/java/sonia/scm/util/SecurityUtil.java +++ b/scm-core/src/main/java/sonia/scm/util/SecurityUtil.java @@ -37,6 +37,7 @@ package sonia.scm.util; import com.google.inject.Provider; +import sonia.scm.SCMContext; import sonia.scm.security.ScmSecurityException; import sonia.scm.security.SecurityContext; import sonia.scm.user.User; @@ -111,4 +112,31 @@ public class SecurityUtil return user; } + + /** + * Method description + * + * + * @param contextProvider + * + * @return + */ + public static boolean isAnonymous( + Provider contextProvider) + { + return isAnonymous(contextProvider.get()); + } + + /** + * Method description + * + * + * @param context + * + * @return + */ + public static boolean isAnonymous(SecurityContext context) + { + return SCMContext.USER_ANONYMOUS.equals(context.getUser().getName()); + } }