From 2cbde2fd17c90ba771a6d08ea83a21deccf371bb Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 16 Sep 2011 10:09:11 +0200 Subject: [PATCH] added repository browser client api --- .../scm/client/ClientRepositoryBrowser.java | 85 ++++++++ .../sonia/scm/client/FileObjectWrapper.java | 180 +++++++++++++++++ .../scm/client/RepositoryClientHandler.java | 15 ++ .../java/sonia/scm/client/ScmUrlProvider.java | 93 ++++++++- .../client/JerseyClientRepositoryBrowser.java | 189 ++++++++++++++++++ .../client/JerseyRepositoryClientHandler.java | 18 +- 6 files changed, 578 insertions(+), 2 deletions(-) create mode 100644 scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientRepositoryBrowser.java create mode 100644 scm-clients/scm-client-api/src/main/java/sonia/scm/client/FileObjectWrapper.java create mode 100644 scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientRepositoryBrowser.java diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientRepositoryBrowser.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientRepositoryBrowser.java new file mode 100644 index 0000000000..fabf35eed8 --- /dev/null +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientRepositoryBrowser.java @@ -0,0 +1,85 @@ +/** + * 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 java.io.IOException; +import java.io.InputStream; + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + * @since 1.8 + */ +public interface ClientRepositoryBrowser +{ + + /** + * Method description + * + * + * @param revision + * @param path + * + * @return + * + * @throws IOException + */ + public InputStream getContent(String revision, String path) + throws IOException; + + /** + * Method description + * + * + * @param revision + * @param path + * + * @return + */ + public List getFiles(String revision, String path); + + /** + * Method description + * + * + * @param revision + * + * @return + */ + public List getFiles(String revision); +} diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/FileObjectWrapper.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/FileObjectWrapper.java new file mode 100644 index 0000000000..2f987d4a9e --- /dev/null +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/FileObjectWrapper.java @@ -0,0 +1,180 @@ +/** + * 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.FileObject; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; +import java.io.InputStream; + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + * @since 1.8 + */ +public class FileObjectWrapper +{ + + /** + * Constructs ... + * + * + * + * @param repositoryBrowser + * @param revision + * @param file + */ + public FileObjectWrapper(ClientRepositoryBrowser repositoryBrowser, + String revision, FileObject file) + { + this.repositoryBrowser = repositoryBrowser; + this.file = file; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public List getChildren() + { + List children = null; + + if (isDirectory()) + { + children = repositoryBrowser.getFiles(revision, getPath()); + } + + return children; + } + + /** + * Method description + * + * + * @return + * + * @throws IOException + */ + public InputStream getContent() throws IOException + { + return repositoryBrowser.getContent(revision, getPath()); + } + + /** + * Method description + * + * + * @return + */ + public String getDescription() + { + return file.getDescription(); + } + + /** + * Method description + * + * + * @return + */ + public Long getLastModified() + { + return file.getLastModified(); + } + + /** + * Method description + * + * + * @return + */ + public long getLength() + { + return file.getLength(); + } + + /** + * Method description + * + * + * @return + */ + public String getName() + { + return file.getName(); + } + + /** + * Method description + * + * + * @return + */ + public String getPath() + { + return file.getPath(); + } + + /** + * Method description + * + * + * @return + */ + public boolean isDirectory() + { + return file.isDirectory(); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private FileObject file; + + /** Field description */ + private ClientRepositoryBrowser repositoryBrowser; + + /** Field description */ + private String revision; +} diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/RepositoryClientHandler.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/RepositoryClientHandler.java index f88be8136e..bfacaf247b 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/RepositoryClientHandler.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/RepositoryClientHandler.java @@ -35,6 +35,7 @@ package sonia.scm.client; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.NotSupportedFeatuerException; import sonia.scm.Type; import sonia.scm.repository.Repository; @@ -49,6 +50,20 @@ import java.util.Collection; public interface RepositoryClientHandler extends ClientHandler { + /** + * Method description + * + * + * @param repository + * + * @return + * @since 1.8 + * + * @throws NotSupportedFeatuerException + */ + public ClientRepositoryBrowser getRepositoryBrowser(Repository repository) + throws NotSupportedFeatuerException; + /** * Method description * 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 c956735a5b..0bda7a76b6 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 @@ -38,6 +38,12 @@ package sonia.scm.client; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.util.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import java.text.MessageFormat; + /** * * @author Sebastian Sdorra @@ -73,6 +79,12 @@ public class ScmUrlProvider /** Field description */ public static final String URLPART_USERS = "users"; + /** Field description */ + public static final String URLPATTERN_BROWSE = "repositories/{0}/browse"; + + /** Field description */ + public static final String URLPATTERN_CONTENT = "repositories/{0}/content"; + /** the logger for classVar */ private static final Logger logger = LoggerFactory.getLogger(ScmUrlProvider.class); @@ -183,6 +195,65 @@ public class ScmUrlProvider return getResourceUrl(URLPART_REPOSITORIES); } + /** + * Method description + * + * + * @param repositoryId + * @param revision + * @param path + * @since 1.8 + * + * @return + */ + public String getRepositoryBrowseUrl(String repositoryId, String path, + String revision) + { + String url = MessageFormat.format(getResourceUrl(URLPATTERN_BROWSE), + repositoryId); + String s = "?"; + + if (Util.isNotEmpty(path)) + { + url = url.concat(s).concat("path=").concat(path); + s = "&"; + } + + if (Util.isNotEmpty(revision)) + { + url = url.concat(s).concat("revision=").concat(revision); + } + + return url; + } + + /** + * Method description + * + * + * @param repositoryId + * @param path + * @param revision + * @since 1.8 + * + * @return + */ + public String getRepositoryContentUrl(String repositoryId, String path, + String revision) + { + String url = MessageFormat.format(getResourceUrl(URLPATTERN_CONTENT, + false), repositoryId); + + url = url.concat("?path=").concat(path); + + if (Util.isNotEmpty(revision)) + { + url = url.concat("&revision=").concat(revision); + } + + return url; + } + /** * Method description * @@ -206,7 +277,27 @@ public class ScmUrlProvider */ public String getResourceUrl(String urlPart) { - String resourceUrl = baseUrl.concat(urlPart).concat(extension); + return getResourceUrl(urlPart, true); + } + + /** + * Method description + * + * + * @param urlPart + * @param appendExtension + * @since 1.8 + * + * @return + */ + public String getResourceUrl(String urlPart, boolean appendExtension) + { + String resourceUrl = baseUrl.concat(urlPart); + + if (appendExtension) + { + resourceUrl = resourceUrl.concat(extension); + } if (logger.isTraceEnabled()) { diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientRepositoryBrowser.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientRepositoryBrowser.java new file mode 100644 index 0000000000..bbdb9333de --- /dev/null +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientRepositoryBrowser.java @@ -0,0 +1,189 @@ +/** + * 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.BrowserResult; +import sonia.scm.repository.FileObject; +import sonia.scm.repository.Repository; +import sonia.scm.util.AssertUtil; +import sonia.scm.util.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; + +import java.io.IOException; +import java.io.InputStream; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class JerseyClientRepositoryBrowser implements ClientRepositoryBrowser +{ + + /** + * Constructs ... + * + * + * @param session + * @param repository + */ + public JerseyClientRepositoryBrowser(JerseyClientSession session, + Repository repository) + { + this.session = session; + this.repository = repository; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param revision + * @param path + * + * @return + * + * @throws IOException + */ + @Override + public InputStream getContent(String revision, String path) throws IOException + { + InputStream input = null; + String url = + session.getUrlProvider().getRepositoryContentUrl(repository.getId(), path, + revision); + WebResource resource = session.getClient().resource(url); + ClientResponse response = null; + + try + { + response = resource.get(ClientResponse.class); + + if (response.getStatus() != ScmClientException.SC_NOTFOUND) + { + ClientUtil.checkResponse(response, 200); + input = response.getEntityInputStream(); + } + } + finally + { + ClientUtil.close(response); + } + + return input; + } + + /** + * Method description + * + * + * @param revision + * @param path + * + * @return + */ + @Override + public List getFiles(String revision, String path) + { + List files = null; + String url = + session.getUrlProvider().getRepositoryBrowseUrl(repository.getId(), path, + revision); + WebResource resource = session.getClient().resource(url); + ClientResponse response = null; + + try + { + response = resource.get(ClientResponse.class); + + if (response.getStatus() != ScmClientException.SC_NOTFOUND) + { + ClientUtil.checkResponse(response, 200); + + BrowserResult result = response.getEntity(BrowserResult.class); + + AssertUtil.assertIsNotNull(result); + files = new ArrayList(); + + List foList = result.getFiles(); + + if (Util.isNotEmpty(foList)) + { + for (FileObject fo : foList) + { + files.add(new FileObjectWrapper(this, revision, fo)); + } + } + } + } + finally + { + ClientUtil.close(response); + } + + return files; + } + + /** + * Method description + * + * + * @param revision + * + * @return + */ + @Override + public List getFiles(String revision) + { + return getFiles(revision, ""); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Repository repository; + + /** Field description */ + private JerseyClientSession session; +} 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 index d0316b19d7..6d9c2ccb1d 100644 --- 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 @@ -35,9 +35,9 @@ package sonia.scm.client; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.NotSupportedFeatuerException; import sonia.scm.Type; import sonia.scm.repository.Repository; -import sonia.scm.util.AssertUtil; //~--- JDK imports ------------------------------------------------------------ @@ -69,6 +69,22 @@ public class JerseyRepositoryClientHandler //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param repository + * + * @return + * + */ + @Override + public JerseyClientRepositoryBrowser getRepositoryBrowser( + Repository repository) + { + return new JerseyClientRepositoryBrowser(session, repository); + } + /** * Method description *