diff --git a/scm-test/src/main/java/sonia/scm/ManagerTestBase.java b/scm-test/src/main/java/sonia/scm/ManagerTestBase.java index 5499bc4526..ccf34a0629 100644 --- a/scm-test/src/main/java/sonia/scm/ManagerTestBase.java +++ b/scm-test/src/main/java/sonia/scm/ManagerTestBase.java @@ -40,7 +40,7 @@ package sonia.scm; * @param * @param */ -public abstract class ManagerTestBase extends AbstractTestBase { diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractManagerResource.java similarity index 69% rename from scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractResource.java rename to scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractManagerResource.java index 4375198c30..3fc9e82a04 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractManagerResource.java @@ -33,9 +33,18 @@ package sonia.scm.api.rest.resources; +//~--- non-JDK imports -------------------------------------------------------- + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.Manager; +import sonia.scm.ModelObject; + //~--- JDK imports ------------------------------------------------------------ import java.util.Collection; + import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -55,51 +64,31 @@ import javax.ws.rs.core.UriInfo; * @author Sebastian Sdorra * * @param + * @param */ -public abstract class AbstractResource +public abstract class AbstractManagerResource { - /** - * Method description - * - * - * @param item - * - * @throws Exception - */ - protected abstract void addItem(T item) throws Exception; + /** the logger for AbstractManagerResource */ + private static final Logger logger = + LoggerFactory.getLogger(AbstractManagerResource.class); + + //~--- constructors --------------------------------------------------------- /** - * Method description + * Constructs ... * * - * @param item - * - * @throws Exception + * @param manager */ - protected abstract void removeItem(T item) throws Exception; - - /** - * Method description - * - * - * @param name - * @param item - * - * @throws Exception - */ - protected abstract void updateItem(String name, T item) throws Exception; + public AbstractManagerResource(Manager manager) + { + this.manager = manager; + } //~--- get methods ---------------------------------------------------------- - /** - * Method description - * - * - * @return - */ - protected abstract Collection getAllItems(); - /** * Method description * @@ -110,16 +99,6 @@ public abstract class AbstractResource */ protected abstract String getId(T item); - /** - * Method description - * - * - * @param name - * - * @return - */ - protected abstract T getItem(String name); - /** * Method description * @@ -142,14 +121,18 @@ public abstract class AbstractResource */ @POST @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public Response add(@Context UriInfo uriInfo, T item) + public Response create(@Context UriInfo uriInfo, T item) { + preCreate(item); + try { - addItem(item); + manager.create(item); } catch (Exception ex) { + logger.error("error during create", ex); + throw new WebApplicationException(ex); } @@ -167,22 +150,26 @@ public abstract class AbstractResource * @return */ @DELETE - @Path("{name}") - public Response delete(@PathParam("name") String name) + @Path("{id}") + public Response delete(@PathParam("id") String name) { - T item = getItem(name); + T item = manager.get(name); if (item == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } + preDelete(item); + try { - removeItem(item); + manager.delete(item); } catch (Exception ex) { + logger.error("error during create", ex); + throw new WebApplicationException(ex); } @@ -190,25 +177,27 @@ public abstract class AbstractResource } /** - * Method description + * Method description * * * * - * @param uriInfo - * @param name - * @param item + * @param uriInfo + * @param name + * @param item * */ @PUT - @Path("{name}") + @Path("{id}") @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public void update(@Context UriInfo uriInfo, @PathParam("name") String name, + public void update(@Context UriInfo uriInfo, @PathParam("id") String name, T item) { + preUpate(item); + try { - updateItem(name, item); + manager.modify(item); } catch (Exception ex) { @@ -219,26 +208,26 @@ public abstract class AbstractResource //~--- get methods ---------------------------------------------------------- /** - * Method description + * Method description * * - * @param name + * @param id * - * @return + * @return */ @GET - @Path("{name}") + @Path("{id}") @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public T get(@PathParam("name") String name) + public T get(@PathParam("id") String id) { - T item = getItem(name); + T item = manager.get(id); if (item == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } - return item; + return prepareForReturn(item); } /** @@ -251,6 +240,63 @@ public abstract class AbstractResource @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public Collection getAll() { - return getAllItems(); + return prepareForReturn(manager.getAll()); } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param item + */ + protected void preCreate(T item) {} + + /** + * Method description + * + * + * @param item + */ + protected void preDelete(T item) {} + + /** + * Method description + * + * + * @param item + */ + protected void preUpate(T item) {} + + /** + * Method description + * + * + * @param item + * + * @return + */ + protected T prepareForReturn(T item) + { + return item; + } + + /** + * Method description + * + * + * @param items + * + * @return + */ + protected Collection prepareForReturn(Collection items) + { + return items; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + protected Manager manager; } diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/GroupResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/GroupResource.java index 38c21a4ca6..558d456bc3 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/GroupResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/GroupResource.java @@ -39,12 +39,11 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import sonia.scm.group.Group; +import sonia.scm.group.GroupException; import sonia.scm.group.GroupManager; //~--- JDK imports ------------------------------------------------------------ -import java.util.Collection; - import javax.ws.rs.Path; /** @@ -53,7 +52,8 @@ import javax.ws.rs.Path; */ @Path("groups") @Singleton -public class GroupResource extends AbstractResource +public class GroupResource + extends AbstractManagerResource { /** Field description */ @@ -70,68 +70,11 @@ public class GroupResource extends AbstractResource @Inject public GroupResource(GroupManager groupManager) { - this.groupManager = groupManager; - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param group - * - * @throws Exception - */ - @Override - protected void addItem(Group group) throws Exception - { - groupManager.create(group); - } - - /** - * Method description - * - * - * @param group - * - * @throws Exception - */ - @Override - protected void removeItem(Group group) throws Exception - { - groupManager.delete(group); - } - - /** - * Method description - * - * - * @param name - * @param group - * - * @throws Exception - */ - @Override - protected void updateItem(String name, Group group) throws Exception - { - groupManager.modify(group); + super(groupManager); } //~--- get methods ---------------------------------------------------------- - /** - * Method description - * - * - * @return - */ - @Override - protected Collection getAllItems() - { - return groupManager.getAll(); - } - /** * Method description * @@ -146,20 +89,6 @@ public class GroupResource extends AbstractResource return group.getName(); } - /** - * Method description - * - * - * @param name - * - * @return - */ - @Override - protected Group getItem(String name) - { - return groupManager.get(name); - } - /** * Method description * @@ -171,9 +100,4 @@ public class GroupResource extends AbstractResource { return PATH_PART; } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private GroupManager groupManager; } 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 644c6d27c8..f1203490d4 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 @@ -47,27 +47,26 @@ import sonia.scm.repository.Repository; import sonia.scm.repository.RepositoryException; import sonia.scm.repository.RepositoryHandler; import sonia.scm.repository.RepositoryManager; +import sonia.scm.util.HttpUtil; import sonia.scm.web.security.WebSecurityContext; //~--- JDK imports ------------------------------------------------------------ -import java.io.IOException; - import java.util.ArrayList; import java.util.Collection; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Path; -import sonia.scm.util.HttpUtil; /** * * @author Sebastian Sdorra */ -@Path("repositories") @Singleton -public class RepositoryResource extends AbstractResource +@Path("repositories") +public class RepositoryResource + extends AbstractManagerResource { /** Field description */ @@ -90,6 +89,7 @@ public class RepositoryResource extends AbstractResource Provider securityContextProvider, Provider requestProvider) { + super(repositoryManager); this.configuration = configuration; this.repositoryManager = repositoryManager; this.securityContextProvider = securityContextProvider; @@ -102,64 +102,14 @@ public class RepositoryResource extends AbstractResource * Method description * * - * @param item - * - * @throws IOException - * @throws RepositoryException - */ - @Override - protected void addItem(Repository item) - throws RepositoryException, IOException - { - repositoryManager.create(item); - } - - /** - * Method description - * - * - * @param item - * - * @throws IOException - * @throws RepositoryException - */ - @Override - protected void removeItem(Repository item) - throws RepositoryException, IOException - { - repositoryManager.delete(item); - } - - /** - * Method description - * - * - * @param name - * @param item - * - * @throws IOException - * @throws RepositoryException - */ - @Override - protected void updateItem(String name, Repository item) - throws RepositoryException, IOException - { - repositoryManager.modify(item); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * * + * @param repositories * @return */ @Override - protected Collection getAllItems() + protected Collection prepareForReturn( + Collection repositories) { - Collection repositories = repositoryManager.getAll(); - for (Repository repository : repositories) { appendUrl(repository); @@ -169,6 +119,25 @@ public class RepositoryResource extends AbstractResource return repositories; } + /** + * Method description + * + * + * @param repository + * + * @return + */ + @Override + protected Repository prepareForReturn(Repository repository) + { + appendUrl(repository); + prepareRepository(repository); + + return repository; + } + + //~--- get methods ---------------------------------------------------------- + /** * Method description * @@ -183,26 +152,6 @@ public class RepositoryResource extends AbstractResource return item.getId(); } - /** - * Method description - * - * - * - * @param id - * - * @return - */ - @Override - protected Repository getItem(String id) - { - Repository repository = repositoryManager.get(id); - - appendUrl(repository); - prepareRepository(repository); - - return repository; - } - /** * Method description * @@ -272,8 +221,6 @@ public class RepositoryResource extends AbstractResource //~--- get methods ---------------------------------------------------------- - - /** * Method description * diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/UserResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/UserResource.java index dd7dfb575d..893687b209 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/UserResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/UserResource.java @@ -40,6 +40,7 @@ import com.google.inject.Singleton; import sonia.scm.security.EncryptionHandler; import sonia.scm.user.User; +import sonia.scm.user.UserException; import sonia.scm.user.UserManager; import sonia.scm.util.AssertUtil; import sonia.scm.util.Util; @@ -56,7 +57,7 @@ import javax.ws.rs.Path; */ @Path("users") @Singleton -public class UserResource extends AbstractResource +public class UserResource extends AbstractManagerResource { /** Field description */ @@ -78,7 +79,7 @@ public class UserResource extends AbstractResource public UserResource(UserManager userManager, EncryptionHandler encryptionHandler) { - this.userManager = userManager; + super(userManager); this.encryptionHandler = encryptionHandler; } @@ -89,14 +90,11 @@ public class UserResource extends AbstractResource * * * @param user - * - * @throws Exception */ @Override - protected void addItem(User user) throws Exception + protected void preCreate(User user) { encryptPassword(user); - userManager.create(user); } /** @@ -104,30 +102,13 @@ public class UserResource extends AbstractResource * * * @param user - * - * @throws Exception */ @Override - protected void removeItem(User user) throws Exception - { - userManager.delete(user); - } - - /** - * Method description - * - * - * @param name - * @param user - * - * @throws Exception - */ - @Override - protected void updateItem(String name, User user) throws Exception + protected void preUpate(User user) { if (DUMMY_PASSWORT.equals(user.getPassword())) { - User o = userManager.get(name); + User o = manager.get(user.getName()); AssertUtil.assertIsNotNull(o); user.setPassword(o.getPassword()); @@ -136,23 +117,19 @@ public class UserResource extends AbstractResource { encryptPassword(user); } - - userManager.modify(user); } - //~--- get methods ---------------------------------------------------------- - /** * Method description * * + * @param users + * * @return */ @Override - protected Collection getAllItems() + protected Collection prepareForReturn(Collection users) { - Collection users = userManager.getAll(); - if (Util.isNotEmpty(users)) { for (User u : users) @@ -173,30 +150,27 @@ public class UserResource extends AbstractResource * @return */ @Override - protected String getId(User user) + protected User prepareForReturn(User user) { - return user.getName(); + user.setPassword(DUMMY_PASSWORT); + + return user; } + //~--- get methods ---------------------------------------------------------- + /** * Method description * * - * @param name + * @param user * * @return */ @Override - protected User getItem(String name) + protected String getId(User user) { - User user = userManager.get(name); - - if (user != null) - { - user.setPassword(DUMMY_PASSWORT); - } - - return user; + return user.getName(); } /** @@ -233,7 +207,4 @@ public class UserResource extends AbstractResource /** Field description */ private EncryptionHandler encryptionHandler; - - /** Field description */ - private UserManager userManager; }