mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
added getContent api
This commit is contained in:
@@ -43,6 +43,7 @@ import sonia.scm.util.Util;
|
|||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -102,6 +103,47 @@ public class HgRepositoryBrowser implements RepositoryBrowser
|
|||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param revision
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InputStream getContent(String revision, String path)
|
||||||
|
throws IOException, RepositoryException
|
||||||
|
{
|
||||||
|
if (Util.isEmpty(revision))
|
||||||
|
{
|
||||||
|
revision = DEFAULT_REVISION;
|
||||||
|
}
|
||||||
|
|
||||||
|
File directory = handler.getDirectory(repository);
|
||||||
|
ProcessBuilder builder =
|
||||||
|
new ProcessBuilder(handler.getConfig().getHgBinary(), "cat", "-r",
|
||||||
|
revision, Util.nonNull(path));
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
{
|
||||||
|
StringBuilder msg = new StringBuilder();
|
||||||
|
|
||||||
|
for (String param : builder.command())
|
||||||
|
{
|
||||||
|
msg.append(param).append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(msg.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.directory(directory).start().getInputStream();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ package sonia.scm.repository;
|
|||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -45,6 +46,21 @@ import java.io.IOException;
|
|||||||
public interface RepositoryBrowser
|
public interface RepositoryBrowser
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param revision
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public InputStream getContent(String revision, String path)
|
||||||
|
throws IOException, RepositoryException;;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -59,11 +59,16 @@ import sonia.scm.repository.RepositoryException;
|
|||||||
import sonia.scm.repository.RepositoryHandler;
|
import sonia.scm.repository.RepositoryHandler;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
import sonia.scm.util.HttpUtil;
|
import sonia.scm.util.HttpUtil;
|
||||||
|
import sonia.scm.util.IOUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
import sonia.scm.web.security.WebSecurityContext;
|
import sonia.scm.web.security.WebSecurityContext;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -78,9 +83,11 @@ import javax.ws.rs.Path;
|
|||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.GenericEntity;
|
import javax.ws.rs.core.GenericEntity;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.StreamingOutput;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -239,6 +246,79 @@ public class RepositoryResource
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @param revision
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GET
|
||||||
|
@Path("{id}/content")
|
||||||
|
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
|
||||||
|
public StreamingOutput getContent(@PathParam("id") String id,
|
||||||
|
@QueryParam("revision") String revision,
|
||||||
|
@QueryParam("path") String path)
|
||||||
|
{
|
||||||
|
StreamingOutput output = null;
|
||||||
|
Repository repository = repositoryManager.get(id);
|
||||||
|
|
||||||
|
if (repository != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RepositoryBrowser browser =
|
||||||
|
repositoryManager.getRepositoryBrowser(repository);
|
||||||
|
|
||||||
|
if (browser != null)
|
||||||
|
{
|
||||||
|
final InputStream content = browser.getContent(revision, path);
|
||||||
|
|
||||||
|
if (content != null)
|
||||||
|
{
|
||||||
|
output = new StreamingOutput()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void write(OutputStream output)
|
||||||
|
throws IOException, WebApplicationException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IOUtil.copy(content, output);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IOUtil.close(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (logger.isWarnEnabled())
|
||||||
|
{
|
||||||
|
logger.warn(
|
||||||
|
"could not find content, repository: {}, path: {}, revision: {}",
|
||||||
|
new Object[] { repository.getId(),
|
||||||
|
path, revision });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (logger.isWarnEnabled())
|
||||||
|
{
|
||||||
|
logger.warn("could not find repository browser for respository {}",
|
||||||
|
repository.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.error("could not retrive content", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user