Commits

Sebastian Sdorra committed d02e9fd

generate repository urls

Comments (0)

Files changed (11)

plugins/scm-git-plugin/src/main/resources/sonia/scm/git.config.js

     name: 'repositoryDirectory',
     fieldLabel: 'Repository directory',
     allowBlank : false
-  },{
-    xtype: 'textfield',
-    name: 'baseUrl',
-    fieldLabel: 'Base URL',
-    allowBlank : false
   }],
 
   onSubmit: function(values){

plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java

 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
-import java.util.logging.Level;
 
 /**
  *
    *
    * @param repository
    *
+   * @return
+   */
+  @Override
+  public String createResourcePath(Repository repository)
+  {
+    return "/hg/".concat(repository.getName());
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param repository
+   *
    * @throws IOException
    * @throws RepositoryException
    */
         repository = new Repository();
         repository.setType(TYPE_NAME);
         repository.setName(name);
-        repository.setUrl(buildUrl(repository));
         loadRepository(repository, hgDirectory);
       }
     }

plugins/scm-hg-plugin/src/main/resources/sonia/scm/hg.config.js

     name: 'repositoryDirectory',
     fieldLabel: 'Repository directory',
     allowBlank : false
-  },{
-    xtype: 'textfield',
-    name: 'baseUrl',
-    fieldLabel: 'Base URL',
-    allowBlank : false
   }],
 
   onSubmit: function(values){

plugins/scm-svn-plugin/src/main/resources/sonia/scm/svn.config.js

     name: 'repositoryDirectory',
     fieldLabel: 'Repository directory',
     allowBlank : false
-  },{
-    xtype: 'textfield',
-    name: 'baseUrl',
-    fieldLabel: 'Base URL',
-    allowBlank : false
-  },{
-    xtype: 'textfield',
-    name: 'svnAccessFile',
-    fieldLabel: 'Svn Accessfile',
-    allowBlank : true
   }],
 
   onSubmit: function(values){

scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java

 import sonia.scm.ConfigChangedListener;
 import sonia.scm.SCMContextProvider;
 import sonia.scm.util.IOUtil;
-import sonia.scm.util.Util;
 
 //~--- JDK imports ------------------------------------------------------------
 
  *
  * @param <C>
  */
-public abstract class AbstractRepositoryHandler<C extends BasicRepositoryConfig>
+public abstract class AbstractRepositoryHandler<C extends SimpleRepositoryConfig>
         implements RepositoryHandler
 {
 
    *
    *
    * @param repository
-   *
-   * @return
-   */
-  protected String buildUrl(Repository repository)
-  {
-    String url = config.getBaseUrl();
-
-    if (Util.isNotEmpty(url))
-    {
-      if (!url.endsWith("/"))
-      {
-        url = url.concat("/");
-      }
-
-      url = url.concat(repository.getName());
-    }
-
-    return url;
-  }
-
-  /**
-   * Method description
-   *
-   *
-   * @param repository
    */
   protected void initNewRepository(Repository repository)
   {
     repository.setId(UUID.randomUUID().toString());
-    repository.setUrl(buildUrl(repository));
     repository.setCreationDate(System.currentTimeMillis());
   }
 

scm-core/src/main/java/sonia/scm/repository/AbstractSimpleRepositoryHandler.java

 import sonia.scm.io.CommandResult;
 import sonia.scm.io.ExtendedCommand;
 import sonia.scm.util.IOUtil;
-import sonia.scm.util.Util;
 
 //~--- JDK imports ------------------------------------------------------------
 
    * Method description
    *
    *
+   *
+   * @param repository
+   * @return
+   */
+  @Override
+  public String createResourcePath(Repository repository)
+  {
+    StringBuilder path = new StringBuilder("/");
+
+    path.append(getType().getName()).append("/").append(repository.getName());
+
+    return path.toString();
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param repository
    *
    * @throws IOException
 
       repository = null;
     }
-    else
-    {
-      String url = buildUrl(repository);
-
-      if (Util.isNotEmpty(url))
-      {
-        repository.setUrl(url);
-      }
-    }
 
     return repository;
   }

scm-core/src/main/java/sonia/scm/repository/BasicRepositoryConfig.java

-/**
- * 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.repository;
-
-/**
- *
- * @author Sebastian Sdorra
- */
-public class BasicRepositoryConfig
-{
-
-  /**
-   * Method description
-   *
-   *
-   * @return
-   */
-  public String getBaseUrl()
-  {
-    return baseUrl;
-  }
-
-  //~--- set methods ----------------------------------------------------------
-
-  /**
-   * Method description
-   *
-   *
-   * @param baseUrl
-   */
-  public void setBaseUrl(String baseUrl)
-  {
-    this.baseUrl = baseUrl;
-  }
-
-  //~--- fields ---------------------------------------------------------------
-
-  /** Field description */
-  private String baseUrl;
-}

scm-core/src/main/java/sonia/scm/repository/RepositoryHandler.java

  */
 public interface RepositoryHandler
         extends Handler<Repository, RepositoryException>,
-                ListenerSupport<ConfigChangedListener> {}
+                ListenerSupport<ConfigChangedListener>
+{
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  public String createResourcePath( Repository repository );
+}

scm-core/src/main/java/sonia/scm/repository/SimpleRepositoryConfig.java

  *
  * @author Sebastian Sdorra
  */
-public class SimpleRepositoryConfig extends BasicRepositoryConfig
+public class SimpleRepositoryConfig
 {
 
   /**

scm-webapp/src/main/java/sonia/scm/ScmServletModule.java

     {
       try
       {
-        JAXB.unmarshal(file, ScmConfiguration.class);
+        config = JAXB.unmarshal(file, ScmConfiguration.class);
       }
       catch (Exception ex)
       {

scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java

  *
  */
 
+
+
 package sonia.scm.api.rest.resources;
 
 //~--- non-JDK imports --------------------------------------------------------
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
+import sonia.scm.config.ScmConfiguration;
 import sonia.scm.repository.Repository;
 import sonia.scm.repository.RepositoryException;
+import sonia.scm.repository.RepositoryHandler;
 import sonia.scm.repository.RepositoryManager;
 
 //~--- JDK imports ------------------------------------------------------------
 
 import java.util.Collection;
 
+import javax.servlet.http.HttpServletRequest;
+
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 
 /**
   /** Field description */
   public static final String PATH_PART = "repositories";
 
+  //~--- constructors ---------------------------------------------------------
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param configuration
+   * @param repositoryManager
+   */
+  @Inject
+  public RepositoryResource(ScmConfiguration configuration,
+                            RepositoryManager repositoryManager)
+  {
+    this.configuration = configuration;
+    this.repositoryManager = repositoryManager;
+  }
+
   //~--- methods --------------------------------------------------------------
 
   /**
   protected Repository[] getAllItems()
   {
     Collection<Repository> repositoryCollection = repositoryManager.getAll();
+    Repository[] repositories =
+      repositoryCollection.toArray(new Repository[repositoryCollection.size()]);
 
-    return repositoryCollection.toArray(
-        new Repository[repositoryCollection.size()]);
+    for (Repository repository : repositories)
+    {
+      appendUrl(repository);
+    }
+
+    return repositories;
   }
 
   /**
   @Override
   protected Repository getItem(String id)
   {
-    return repositoryManager.get(id);
+    Repository repository = repositoryManager.get(id);
+
+    appendUrl(repository);
+
+    return repository;
   }
 
   /**
     return PATH_PART;
   }
 
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param repository
+   */
+  private void appendUrl(Repository repository)
+  {
+    StringBuilder url = new StringBuilder(request.getScheme());
+
+    url.append("://").append(configuration.getServername());
+    url.append(":").append(request.getLocalPort());
+
+    String ctxPath = request.getContextPath();
+
+    url.append(ctxPath);
+
+    if (ctxPath.endsWith("/"))
+    {
+      ctxPath.substring(0, ctxPath.length() - 1);
+    }
+
+    RepositoryHandler handler =
+      repositoryManager.getHandler(repository.getType());
+
+    url.append(handler.createResourcePath(repository));
+    repository.setUrl(url.toString());
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
-  @Inject
+  private ScmConfiguration configuration;
+
+  /** Field description */
   private RepositoryManager repositoryManager;
+
+  /** TODO path request direct to method */
+  @Context
+  private HttpServletRequest request;
 }