diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java index 6ca33740e4..eff6ad029c 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java @@ -46,6 +46,12 @@ public class ScmUrlProvider /** Field description */ public static final String URLPART_AUTHENTICATION = "authentication/login"; + /** Field description */ + public static final String URLPART_REPOSITORIES = "repositories"; + + /** Field description */ + public static final String URLPART_REPOSITORY = "repositories/"; + //~--- constructors --------------------------------------------------------- /** @@ -101,6 +107,30 @@ public class ScmUrlProvider return extension; } + /** + * Method description + * + * + * @return + */ + public String getRepositoriesUrl() + { + return getResourceUrl(URLPART_REPOSITORIES); + } + + /** + * Method description + * + * + * @param id + * + * @return + */ + public String getRepositoryUrl(String id) + { + return getResourceUrl(URLPART_REPOSITORY.concat(id)); + } + /** * Method description * diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java new file mode 100644 index 0000000000..8d45ea7ec5 --- /dev/null +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java @@ -0,0 +1,60 @@ +/** + * 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.client; + +//~--- JDK imports ------------------------------------------------------------ + +import com.sun.jersey.api.client.ClientResponse; + +/** + * + * @author Sebastian Sdorra + */ +public class ClientUtil +{ + + /** + * Method description + * + * + * @param response + */ + public static void close(ClientResponse response) + { + if (response == null) + { + response.close(); + } + } +} diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java index ff508ecaf8..f2335f6e56 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java @@ -80,11 +80,22 @@ public class JerseyClientSession implements ScmClientSession @Override public void close() throws IOException { - throw new UnsupportedOperationException("Not supported yet."); + client.destroy(); } //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @return + */ + public Client getClient() + { + return client; + } + /** * Method description * @@ -106,7 +117,7 @@ public class JerseyClientSession implements ScmClientSession @Override public ClientHandler getRepositoryHandler() { - throw new UnsupportedOperationException("Not supported yet."); + return new JerseyRepositoryClientHandler(this); } /** @@ -118,7 +129,18 @@ public class JerseyClientSession implements ScmClientSession @Override public ScmState getState() { - throw new UnsupportedOperationException("Not supported yet."); + return state; + } + + /** + * Method description + * + * + * @return + */ + public ScmUrlProvider getUrlProvider() + { + return urlProvider; } /** diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java new file mode 100644 index 0000000000..78a876dbb2 --- /dev/null +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java @@ -0,0 +1,287 @@ +/** + * 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.client; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.repository.Repository; +import sonia.scm.util.AssertUtil; + +//~--- JDK imports ------------------------------------------------------------ + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.GenericType; +import com.sun.jersey.api.client.WebResource; + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class JerseyRepositoryClientHandler implements ClientHandler +{ + + /** + * Constructs ... + * + * + * @param session + */ + public JerseyRepositoryClientHandler(JerseyClientSession session) + { + this.client = session.getClient(); + this.urlProvider = session.getUrlProvider(); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param repository + */ + @Override + public void create(Repository repository) + { + AssertUtil.assertIsNotNull(repository); + + WebResource resource = client.resource(urlProvider.getRepositoriesUrl()); + ClientResponse response = null; + + try + { + response = resource.post(ClientResponse.class, repository); + + if (response.getStatus() == 201) + { + String url = response.getHeaders().get("Location").get(0); + + AssertUtil.assertIsNotEmpty(url); + + Repository newRepository = getRepository(url); + + AssertUtil.assertIsNotNull(newRepository); + newRepository.copyProperties(repository); + + // copyProperties does not copy the repository id + repository.setId(newRepository.getId()); + } + else + { + + // todo errorhandling + } + } + finally + { + ClientUtil.close(response); + } + } + + /** + * Method description + * + * + * @param id + */ + @Override + public void delete(String id) + { + AssertUtil.assertIsNotEmpty(id); + + WebResource resource = client.resource(urlProvider.getRepositoryUrl(id)); + ClientResponse response = null; + + try + { + response = resource.delete(ClientResponse.class); + + if (response.getStatus() != 204) + { + + // todo errorhandling + } + } + finally + { + ClientUtil.close(response); + } + } + + /** + * Method description + * + * + * @param repository + */ + @Override + public void delete(Repository repository) + { + AssertUtil.assertIsNotNull(repository); + delete(repository.getId()); + } + + /** + * Method description + * + * + * @param repository + */ + @Override + public void modify(Repository repository) + { + AssertUtil.assertIsNotNull(repository); + + String id = repository.getId(); + + AssertUtil.assertIsNotEmpty(id); + + WebResource resource = client.resource(urlProvider.getRepositoryUrl(id)); + ClientResponse response = null; + + try + { + response = resource.post(ClientResponse.class, repository); + + if (response.getStatus() != 204) + { + + // todo errorhandling + } + } + finally + { + ClientUtil.close(response); + } + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param id + * + * @return + */ + @Override + public Repository get(String id) + { + return getRepository(urlProvider.getRepositoryUrl(id)); + } + + /** + * Method description + * + * + * @return + */ + @Override + public List getAll() + { + List repositories = null; + WebResource resource = client.resource(urlProvider.getRepositoriesUrl()); + ClientResponse response = null; + + try + { + response = resource.get(ClientResponse.class); + + if (response.getStatus() == 200) + { + repositories = response.getEntity(new GenericType>() {} + ); + } + else + { + + // todo errorhandling + } + } + finally + { + ClientUtil.close(response); + } + + return repositories; + } + + /** + * Method description + * + * + * @param url + * + * @return + */ + private Repository getRepository(String url) + { + Repository repository = null; + WebResource resource = client.resource(url); + ClientResponse response = null; + + try + { + response = resource.get(ClientResponse.class); + + if (response.getStatus() == 200) + { + repository = response.getEntity(Repository.class); + } + else + { + + // todo errorhandling + } + } + finally + { + ClientUtil.close(response); + } + + return repository; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Client client; + + /** Field description */ + private ScmUrlProvider urlProvider; +} diff --git a/scm-core/src/main/java/sonia/scm/repository/Repository.java b/scm-core/src/main/java/sonia/scm/repository/Repository.java index c5e2627d0b..4282de556c 100644 --- a/scm-core/src/main/java/sonia/scm/repository/Repository.java +++ b/scm-core/src/main/java/sonia/scm/repository/Repository.java @@ -150,6 +150,7 @@ public class Repository implements ModelObject repository.setName(name); repository.setContact(contact); repository.setCreationDate(creationDate); + repository.setLastModified(lastModified); repository.setDescription(description); repository.setPermissions(permissions); repository.setUrl(url);