From ae758e2a33e0f1f25d5c48978e9e2e8d56b9d819 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 3 Apr 2011 11:12:23 +0200 Subject: [PATCH] added getChangesets method to RepositoryResource --- .../java/sonia/scm/repository/Changesets.java | 102 ++++++++++++++++++ .../scm/repository/HgChangesetViewer.java | 25 +---- .../scm/repository/HgRepositoryHandler.java | 4 +- .../rest/resources/RepositoryResource.java | 78 ++++++++++++++ 4 files changed, 185 insertions(+), 24 deletions(-) create mode 100644 plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/Changesets.java diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/Changesets.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/Changesets.java new file mode 100644 index 0000000000..4a7fc66553 --- /dev/null +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/Changesets.java @@ -0,0 +1,102 @@ +/** + * 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; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sebastian Sdorra + */ +@XmlRootElement(name = "changesets") +@XmlAccessorType(XmlAccessType.FIELD) +public class Changesets +{ + + /** + * Constructs ... + * + */ + public Changesets() {} + + /** + * Constructs ... + * + * + * @param changesets + */ + public Changesets(List changesets) + { + this.changesets = changesets; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public List getChangesets() + { + return changesets; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param changesets + */ + public void setChangesets(List changesets) + { + this.changesets = changesets; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlElement(name = "changeset") + private List changesets; +} diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java index 9168033ee1..eba4bc7c23 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java @@ -61,8 +61,6 @@ import java.util.regex.Pattern; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; /** * @@ -76,7 +74,7 @@ public class HgChangesetViewer implements ChangesetViewer /** Field description */ public static final String TEMPLATE = - "{rev}:{short}{author|escape}{desc|escape}{date|isodatesec}\n"; + "{rev}:{node|short}{author|escape}{desc|escape}{date|isodatesec}\n"; /** the logger for HgChangesetViewer */ private static final Logger logger = @@ -132,9 +130,9 @@ public class HgChangesetViewer implements ChangesetViewer Unmarshaller unmarshaller = handler.createChangesetUnmarshaller(); Changesets cs = (Changesets) unmarshaller.unmarshal(reader); - if ((cs != null) && Util.isNotEmpty(cs.changesets)) + if ((cs != null) && Util.isNotEmpty(cs.getChangesets())) { - changesets = cs.changesets; + changesets = cs.getChangesets(); } else if (logger.isWarnEnabled()) { @@ -233,23 +231,6 @@ public class HgChangesetViewer implements ChangesetViewer return handler.getDirectory(repository).getAbsolutePath(); } - //~--- inner classes -------------------------------------------------------- - - /** - * Class description - * - * @author Sebastian Sdorra - */ - @XmlRootElement(name = "changesets") - private static class Changesets - { - - /** Field description */ - @XmlElement(name = "changeset") - private List changesets; - } - - //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java index d11ae8b3d0..9f6e510369 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java @@ -104,12 +104,12 @@ public class HgRepositoryHandler try { - changesetContext = JAXBContext.newInstance(Changeset.class); + changesetContext = JAXBContext.newInstance(Changesets.class); } catch (JAXBException ex) { throw new ConfigurationException( - "could not create JAXBContext for Changeset-Class", ex); + "could not create JAXBContext for Changesets-Class", ex); } } diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java index 9b7face367..35c4daaa6c 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java @@ -40,6 +40,8 @@ import com.google.inject.Provider; import com.google.inject.Singleton; import sonia.scm.config.ScmConfiguration; +import sonia.scm.repository.Changeset; +import sonia.scm.repository.ChangesetViewer; import sonia.scm.repository.Permission; import sonia.scm.repository.PermissionType; import sonia.scm.repository.PermissionUtil; @@ -48,17 +50,26 @@ import sonia.scm.repository.RepositoryException; import sonia.scm.repository.RepositoryHandler; import sonia.scm.repository.RepositoryManager; import sonia.scm.util.HttpUtil; +import sonia.scm.util.Util; import sonia.scm.web.security.WebSecurityContext; //~--- JDK imports ------------------------------------------------------------ import java.util.ArrayList; import java.util.Collection; +import java.util.List; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.GenericEntity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; /** * @@ -98,6 +109,73 @@ public class RepositoryResource setDisableCache(false); } + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param id + * @param startId + * @param max + * + * @return + */ + @GET + @Path("{id}/changesets") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response getChangesets(@PathParam("id") String id, + @QueryParam("changeset") String startId, + @DefaultValue("25") @QueryParam("max") int max) + { + Response response = null; + Repository repository = repositoryManager.get(id); + + if (repository != null) + { + RepositoryHandler handler = + repositoryManager.getHandler(repository.getType()); + + if (handler != null) + { + ChangesetViewer changesetViewer = + handler.getChangesetViewer(repository); + + if (changesetViewer != null) + { + List changesets = null; + + if (Util.isNotEmpty(startId)) + { + changesets = changesetViewer.getChangesets(startId, max); + } + else + { + changesets = changesetViewer.getLastChangesets(max); + } + + response = + Response.ok(new GenericEntity>(changesets) {} + ).build(); + } + else + { + response = Response.status(Response.Status.NOT_FOUND).build(); + } + } + else + { + response = Response.status(Response.Status.NOT_FOUND).build(); + } + } + else + { + response = Response.status(Response.Status.NOT_FOUND).build(); + } + + return response; + } + //~--- methods -------------------------------------------------------------- /**