added repository browser client api

This commit is contained in:
Sebastian Sdorra
2011-09-16 10:09:11 +02:00
parent 668600b65d
commit 2cbde2fd17
6 changed files with 578 additions and 2 deletions

View File

@@ -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<FileObjectWrapper> getFiles(String revision, String path);
/**
* Method description
*
*
* @param revision
*
* @return
*/
public List<FileObjectWrapper> getFiles(String revision);
}

View File

@@ -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<FileObjectWrapper> getChildren()
{
List<FileObjectWrapper> 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;
}

View File

@@ -35,6 +35,7 @@ package sonia.scm.client;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.NotSupportedFeatuerException;
import sonia.scm.Type; import sonia.scm.Type;
import sonia.scm.repository.Repository; import sonia.scm.repository.Repository;
@@ -49,6 +50,20 @@ import java.util.Collection;
public interface RepositoryClientHandler extends ClientHandler<Repository> public interface RepositoryClientHandler extends ClientHandler<Repository>
{ {
/**
* Method description
*
*
* @param repository
*
* @return
* @since 1.8
*
* @throws NotSupportedFeatuerException
*/
public ClientRepositoryBrowser getRepositoryBrowser(Repository repository)
throws NotSupportedFeatuerException;
/** /**
* Method description * Method description
* *

View File

@@ -38,6 +38,12 @@ package sonia.scm.client;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.text.MessageFormat;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
@@ -73,6 +79,12 @@ public class ScmUrlProvider
/** Field description */ /** Field description */
public static final String URLPART_USERS = "users"; 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 */ /** the logger for classVar */
private static final Logger logger = private static final Logger logger =
LoggerFactory.getLogger(ScmUrlProvider.class); LoggerFactory.getLogger(ScmUrlProvider.class);
@@ -183,6 +195,65 @@ public class ScmUrlProvider
return getResourceUrl(URLPART_REPOSITORIES); 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 * Method description
* *
@@ -206,7 +277,27 @@ public class ScmUrlProvider
*/ */
public String getResourceUrl(String urlPart) 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()) if (logger.isTraceEnabled())
{ {

View File

@@ -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<FileObjectWrapper> getFiles(String revision, String path)
{
List<FileObjectWrapper> 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<FileObjectWrapper>();
List<FileObject> 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<FileObjectWrapper> getFiles(String revision)
{
return getFiles(revision, "");
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Repository repository;
/** Field description */
private JerseyClientSession session;
}

View File

@@ -35,9 +35,9 @@ package sonia.scm.client;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.NotSupportedFeatuerException;
import sonia.scm.Type; import sonia.scm.Type;
import sonia.scm.repository.Repository; import sonia.scm.repository.Repository;
import sonia.scm.util.AssertUtil;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -69,6 +69,22 @@ public class JerseyRepositoryClientHandler
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param repository
*
* @return
*
*/
@Override
public JerseyClientRepositoryBrowser getRepositoryBrowser(
Repository repository)
{
return new JerseyClientRepositoryBrowser(session, repository);
}
/** /**
* Method description * Method description
* *