mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
added AbstractManagerResource
This commit is contained in:
@@ -40,7 +40,7 @@ package sonia.scm;
|
|||||||
* @param <T>
|
* @param <T>
|
||||||
* @param <E>
|
* @param <E>
|
||||||
*/
|
*/
|
||||||
public abstract class ManagerTestBase<T extends TypedObject,
|
public abstract class ManagerTestBase<T extends ModelObject,
|
||||||
E extends Exception> extends AbstractTestBase
|
E extends Exception> extends AbstractTestBase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -33,9 +33,18 @@
|
|||||||
|
|
||||||
package sonia.scm.api.rest.resources;
|
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 ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
@@ -55,51 +64,31 @@ import javax.ws.rs.core.UriInfo;
|
|||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
|
* @param <E>
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractResource<T>
|
public abstract class AbstractManagerResource<T extends ModelObject,
|
||||||
|
E extends Exception>
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/** the logger for AbstractManagerResource */
|
||||||
* Method description
|
private static final Logger logger =
|
||||||
*
|
LoggerFactory.getLogger(AbstractManagerResource.class);
|
||||||
*
|
|
||||||
* @param item
|
//~--- constructors ---------------------------------------------------------
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
protected abstract void addItem(T item) throws Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Constructs ...
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param item
|
* @param manager
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
protected abstract void removeItem(T item) throws Exception;
|
public AbstractManagerResource(Manager<T, E> manager)
|
||||||
|
{
|
||||||
/**
|
this.manager = manager;
|
||||||
* Method description
|
}
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
* @param item
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
protected abstract void updateItem(String name, T item) throws Exception;
|
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
protected abstract Collection<T> getAllItems();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -110,16 +99,6 @@ public abstract class AbstractResource<T>
|
|||||||
*/
|
*/
|
||||||
protected abstract String getId(T item);
|
protected abstract String getId(T item);
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
protected abstract T getItem(String name);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -142,14 +121,18 @@ public abstract class AbstractResource<T>
|
|||||||
*/
|
*/
|
||||||
@POST
|
@POST
|
||||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@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
|
try
|
||||||
{
|
{
|
||||||
addItem(item);
|
manager.create(item);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
logger.error("error during create", ex);
|
||||||
|
|
||||||
throw new WebApplicationException(ex);
|
throw new WebApplicationException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,22 +150,26 @@ public abstract class AbstractResource<T>
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("{name}")
|
@Path("{id}")
|
||||||
public Response delete(@PathParam("name") String name)
|
public Response delete(@PathParam("id") String name)
|
||||||
{
|
{
|
||||||
T item = getItem(name);
|
T item = manager.get(name);
|
||||||
|
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preDelete(item);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
removeItem(item);
|
manager.delete(item);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
logger.error("error during create", ex);
|
||||||
|
|
||||||
throw new WebApplicationException(ex);
|
throw new WebApplicationException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,14 +188,16 @@ public abstract class AbstractResource<T>
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@PUT
|
@PUT
|
||||||
@Path("{name}")
|
@Path("{id}")
|
||||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@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)
|
T item)
|
||||||
{
|
{
|
||||||
|
preUpate(item);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
updateItem(name, item);
|
manager.modify(item);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -222,23 +211,23 @@ public abstract class AbstractResource<T>
|
|||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param name
|
* @param id
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GET
|
@GET
|
||||||
@Path("{name}")
|
@Path("{id}")
|
||||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@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)
|
if (item == null)
|
||||||
{
|
{
|
||||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
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 })
|
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||||
public Collection<T> getAll()
|
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 com.google.inject.Singleton;
|
||||||
|
|
||||||
import sonia.scm.group.Group;
|
import sonia.scm.group.Group;
|
||||||
|
import sonia.scm.group.GroupException;
|
||||||
import sonia.scm.group.GroupManager;
|
import sonia.scm.group.GroupManager;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,7 +52,8 @@ import javax.ws.rs.Path;
|
|||||||
*/
|
*/
|
||||||
@Path("groups")
|
@Path("groups")
|
||||||
@Singleton
|
@Singleton
|
||||||
public class GroupResource extends AbstractResource<Group>
|
public class GroupResource
|
||||||
|
extends AbstractManagerResource<Group, GroupException>
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -70,68 +70,11 @@ public class GroupResource extends AbstractResource<Group>
|
|||||||
@Inject
|
@Inject
|
||||||
public GroupResource(GroupManager groupManager)
|
public GroupResource(GroupManager groupManager)
|
||||||
{
|
{
|
||||||
this.groupManager = groupManager;
|
super(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected Collection<Group> getAllItems()
|
|
||||||
{
|
|
||||||
return groupManager.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -146,20 +89,6 @@ public class GroupResource extends AbstractResource<Group>
|
|||||||
return group.getName();
|
return group.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected Group getItem(String name)
|
|
||||||
{
|
|
||||||
return groupManager.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -171,9 +100,4 @@ public class GroupResource extends AbstractResource<Group>
|
|||||||
{
|
{
|
||||||
return PATH_PART;
|
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.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.web.security.WebSecurityContext;
|
import sonia.scm.web.security.WebSecurityContext;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import sonia.scm.util.HttpUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
@Path("repositories")
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class RepositoryResource extends AbstractResource<Repository>
|
@Path("repositories")
|
||||||
|
public class RepositoryResource
|
||||||
|
extends AbstractManagerResource<Repository, RepositoryException>
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -90,6 +89,7 @@ public class RepositoryResource extends AbstractResource<Repository>
|
|||||||
Provider<WebSecurityContext> securityContextProvider,
|
Provider<WebSecurityContext> securityContextProvider,
|
||||||
Provider<HttpServletRequest> requestProvider)
|
Provider<HttpServletRequest> requestProvider)
|
||||||
{
|
{
|
||||||
|
super(repositoryManager);
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
this.repositoryManager = repositoryManager;
|
this.repositoryManager = repositoryManager;
|
||||||
this.securityContextProvider = securityContextProvider;
|
this.securityContextProvider = securityContextProvider;
|
||||||
@@ -102,64 +102,14 @@ public class RepositoryResource extends AbstractResource<Repository>
|
|||||||
* Method description
|
* 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
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected Collection<Repository> getAllItems()
|
protected Collection<Repository> prepareForReturn(
|
||||||
|
Collection<Repository> repositories)
|
||||||
{
|
{
|
||||||
Collection<Repository> repositories = repositoryManager.getAll();
|
|
||||||
|
|
||||||
for (Repository repository : repositories)
|
for (Repository repository : repositories)
|
||||||
{
|
{
|
||||||
appendUrl(repository);
|
appendUrl(repository);
|
||||||
@@ -169,6 +119,25 @@ public class RepositoryResource extends AbstractResource<Repository>
|
|||||||
return repositories;
|
return repositories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param repository
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Repository prepareForReturn(Repository repository)
|
||||||
|
{
|
||||||
|
appendUrl(repository);
|
||||||
|
prepareRepository(repository);
|
||||||
|
|
||||||
|
return repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -183,26 +152,6 @@ public class RepositoryResource extends AbstractResource<Repository>
|
|||||||
return item.getId();
|
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
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -272,8 +221,6 @@ public class RepositoryResource extends AbstractResource<Repository>
|
|||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import com.google.inject.Singleton;
|
|||||||
|
|
||||||
import sonia.scm.security.EncryptionHandler;
|
import sonia.scm.security.EncryptionHandler;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
import sonia.scm.user.UserException;
|
||||||
import sonia.scm.user.UserManager;
|
import sonia.scm.user.UserManager;
|
||||||
import sonia.scm.util.AssertUtil;
|
import sonia.scm.util.AssertUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
@@ -56,7 +57,7 @@ import javax.ws.rs.Path;
|
|||||||
*/
|
*/
|
||||||
@Path("users")
|
@Path("users")
|
||||||
@Singleton
|
@Singleton
|
||||||
public class UserResource extends AbstractResource<User>
|
public class UserResource extends AbstractManagerResource<User, UserException>
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -78,7 +79,7 @@ public class UserResource extends AbstractResource<User>
|
|||||||
public UserResource(UserManager userManager,
|
public UserResource(UserManager userManager,
|
||||||
EncryptionHandler encryptionHandler)
|
EncryptionHandler encryptionHandler)
|
||||||
{
|
{
|
||||||
this.userManager = userManager;
|
super(userManager);
|
||||||
this.encryptionHandler = encryptionHandler;
|
this.encryptionHandler = encryptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,14 +90,11 @@ public class UserResource extends AbstractResource<User>
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param user
|
* @param user
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void addItem(User user) throws Exception
|
protected void preCreate(User user)
|
||||||
{
|
{
|
||||||
encryptPassword(user);
|
encryptPassword(user);
|
||||||
userManager.create(user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,30 +102,13 @@ public class UserResource extends AbstractResource<User>
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param user
|
* @param user
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void removeItem(User user) throws Exception
|
protected void preUpate(User user)
|
||||||
{
|
|
||||||
userManager.delete(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
* @param user
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void updateItem(String name, User user) throws Exception
|
|
||||||
{
|
{
|
||||||
if (DUMMY_PASSWORT.equals(user.getPassword()))
|
if (DUMMY_PASSWORT.equals(user.getPassword()))
|
||||||
{
|
{
|
||||||
User o = userManager.get(name);
|
User o = manager.get(user.getName());
|
||||||
|
|
||||||
AssertUtil.assertIsNotNull(o);
|
AssertUtil.assertIsNotNull(o);
|
||||||
user.setPassword(o.getPassword());
|
user.setPassword(o.getPassword());
|
||||||
@@ -136,23 +117,19 @@ public class UserResource extends AbstractResource<User>
|
|||||||
{
|
{
|
||||||
encryptPassword(user);
|
encryptPassword(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
userManager.modify(user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
* @param users
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected Collection<User> getAllItems()
|
protected Collection<User> prepareForReturn(Collection<User> users)
|
||||||
{
|
{
|
||||||
Collection<User> users = userManager.getAll();
|
|
||||||
|
|
||||||
if (Util.isNotEmpty(users))
|
if (Util.isNotEmpty(users))
|
||||||
{
|
{
|
||||||
for (User u : users)
|
for (User u : users)
|
||||||
@@ -173,30 +150,27 @@ public class UserResource extends AbstractResource<User>
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected String getId(User user)
|
protected User prepareForReturn(User user)
|
||||||
{
|
{
|
||||||
return user.getName();
|
user.setPassword(DUMMY_PASSWORT);
|
||||||
|
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param name
|
* @param user
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected User getItem(String name)
|
protected String getId(User user)
|
||||||
{
|
{
|
||||||
User user = userManager.get(name);
|
return user.getName();
|
||||||
|
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
user.setPassword(DUMMY_PASSWORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -233,7 +207,4 @@ public class UserResource extends AbstractResource<User>
|
|||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private EncryptionHandler encryptionHandler;
|
private EncryptionHandler encryptionHandler;
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private UserManager userManager;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user