mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
added AbstractManagerResource
This commit is contained in:
@@ -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 <T>
|
||||
* @param <E>
|
||||
*/
|
||||
public abstract class AbstractResource<T>
|
||||
public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
E extends Exception>
|
||||
{
|
||||
|
||||
/**
|
||||
* 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<T, E> manager)
|
||||
{
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract Collection<T> getAllItems();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -110,16 +99,6 @@ public abstract class AbstractResource<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
@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<T>
|
||||
* @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<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<T>
|
||||
//~--- 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<T>
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Collection<T> 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<T> prepareForReturn(Collection<T> items)
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
protected Manager<T, E> manager;
|
||||
}
|
||||
@@ -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<Group>
|
||||
public class GroupResource
|
||||
extends AbstractManagerResource<Group, GroupException>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -70,68 +70,11 @@ public class GroupResource extends AbstractResource<Group>
|
||||
@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<Group> getAllItems()
|
||||
{
|
||||
return groupManager.getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -146,20 +89,6 @@ public class GroupResource extends AbstractResource<Group>
|
||||
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<Group>
|
||||
{
|
||||
return PATH_PART;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private GroupManager groupManager;
|
||||
}
|
||||
|
||||
@@ -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<Repository>
|
||||
@Path("repositories")
|
||||
public class RepositoryResource
|
||||
extends AbstractManagerResource<Repository, RepositoryException>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -90,6 +89,7 @@ public class RepositoryResource extends AbstractResource<Repository>
|
||||
Provider<WebSecurityContext> securityContextProvider,
|
||||
Provider<HttpServletRequest> requestProvider)
|
||||
{
|
||||
super(repositoryManager);
|
||||
this.configuration = configuration;
|
||||
this.repositoryManager = repositoryManager;
|
||||
this.securityContextProvider = securityContextProvider;
|
||||
@@ -102,64 +102,14 @@ public class RepositoryResource extends AbstractResource<Repository>
|
||||
* 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<Repository> getAllItems()
|
||||
protected Collection<Repository> prepareForReturn(
|
||||
Collection<Repository> repositories)
|
||||
{
|
||||
Collection<Repository> repositories = repositoryManager.getAll();
|
||||
|
||||
for (Repository repository : repositories)
|
||||
{
|
||||
appendUrl(repository);
|
||||
@@ -169,6 +119,25 @@ public class RepositoryResource extends AbstractResource<Repository>
|
||||
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<Repository>
|
||||
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<Repository>
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -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<User>
|
||||
public class UserResource extends AbstractManagerResource<User, UserException>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -78,7 +79,7 @@ public class UserResource extends AbstractResource<User>
|
||||
public UserResource(UserManager userManager,
|
||||
EncryptionHandler encryptionHandler)
|
||||
{
|
||||
this.userManager = userManager;
|
||||
super(userManager);
|
||||
this.encryptionHandler = encryptionHandler;
|
||||
}
|
||||
|
||||
@@ -89,14 +90,11 @@ public class UserResource extends AbstractResource<User>
|
||||
*
|
||||
*
|
||||
* @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<User>
|
||||
*
|
||||
*
|
||||
* @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<User>
|
||||
{
|
||||
encryptPassword(user);
|
||||
}
|
||||
|
||||
userManager.modify(user);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param users
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected Collection<User> getAllItems()
|
||||
protected Collection<User> prepareForReturn(Collection<User> users)
|
||||
{
|
||||
Collection<User> users = userManager.getAll();
|
||||
|
||||
if (Util.isNotEmpty(users))
|
||||
{
|
||||
for (User u : users)
|
||||
@@ -173,30 +150,27 @@ public class UserResource extends AbstractResource<User>
|
||||
* @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<User>
|
||||
|
||||
/** Field description */
|
||||
private EncryptionHandler encryptionHandler;
|
||||
|
||||
/** Field description */
|
||||
private UserManager userManager;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user