mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
added AbstractResource
This commit is contained in:
@@ -68,8 +68,6 @@ public class SecurityFilter implements Filter
|
||||
String uri =
|
||||
request.getRequestURI().substring(request.getContextPath().length());
|
||||
|
||||
System.out.println( uri + "" + uri.startsWith( URL_AUTHENTICATION ) );
|
||||
|
||||
if (uri.startsWith(URL_AUTHENTICATION)
|
||||
|| (request.getSession(true).getAttribute("auth") != null))
|
||||
{
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractResource<T>
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*/
|
||||
protected abstract void addItem(T item);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*/
|
||||
protected abstract void removeItem(T item);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
* @param item
|
||||
*/
|
||||
protected abstract void updateItem(String name, T item);
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract T[] getAllItems();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getId(T item);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract T getItem(String name);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getPathPart();
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param uriInfo
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response add(@Context UriInfo uriInfo, T item)
|
||||
{
|
||||
addItem(item);
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(
|
||||
getPathPart().concat("/").concat(getId(item)))).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{name}")
|
||||
public Response delete(@PathParam("name") String name)
|
||||
{
|
||||
T item = getItem(name);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
removeItem(item);
|
||||
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param uriInfo
|
||||
* @param name
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PUT
|
||||
@Path("{name}")
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response update(@Context UriInfo uriInfo,
|
||||
@PathParam("name") String name, T item)
|
||||
{
|
||||
T updateItem = getItem(name);
|
||||
|
||||
if (updateItem == null)
|
||||
{
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
updateItem(name, item);
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(getId(item))).build();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Path("{name}")
|
||||
public T get(@PathParam("name") String name)
|
||||
{
|
||||
T item = getItem(name);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
public T[] getAll()
|
||||
{
|
||||
return getAllItems();
|
||||
}
|
||||
}
|
||||
@@ -18,20 +18,9 @@ import java.util.LinkedHashMap;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -40,9 +29,14 @@ import javax.ws.rs.core.UriInfo;
|
||||
@Singleton
|
||||
@Path("groups")
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public class GroupResource
|
||||
public class GroupResource extends AbstractResource<Group>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String PATH_PART = "groups";
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -63,19 +57,24 @@ public class GroupResource
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param group
|
||||
*
|
||||
* @return
|
||||
* @param item
|
||||
*/
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response add(Group group)
|
||||
@Override
|
||||
protected void addItem(Group item)
|
||||
{
|
||||
groupStore.put(group.getName(), group);
|
||||
groupStore.put(item.getName(), item);
|
||||
}
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(
|
||||
"groups/".concat(group.getName()))).build();
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*/
|
||||
@Override
|
||||
protected void removeItem(Group item)
|
||||
{
|
||||
groupStore.remove(item.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,56 +82,46 @@ public class GroupResource
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
* @param item
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{name}")
|
||||
public Response delete(@PathParam("name") String name)
|
||||
@Override
|
||||
protected void updateItem(String name, Group item)
|
||||
{
|
||||
Group group = groupStore.get(name);
|
||||
|
||||
if (group == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
groupStore.remove(name);
|
||||
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
* @param group
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PUT
|
||||
@Path("{name}")
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response update(@PathParam("name") String name, Group group)
|
||||
{
|
||||
Group updateGroup = groupStore.get(name);
|
||||
|
||||
if (updateGroup == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
updateGroup.setName(name);
|
||||
updateGroup.setMembers(group.getMembers());
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(group.getName())).build();
|
||||
group.setMembers(item.getMembers());
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected Group[] getAllItems()
|
||||
{
|
||||
Collection<Group> groupCollection = groupStore.values();
|
||||
|
||||
return groupCollection.toArray(new Group[groupCollection.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected String getId(Group item)
|
||||
{
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -141,18 +130,10 @@ public class GroupResource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Path("{name}")
|
||||
public Group get(@PathParam("name") String name)
|
||||
@Override
|
||||
protected Group getItem(String name)
|
||||
{
|
||||
Group group = groupStore.get(name);
|
||||
|
||||
if (group == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
return group;
|
||||
return groupStore.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,20 +142,14 @@ public class GroupResource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
public Group[] getAll()
|
||||
@Override
|
||||
protected String getPathPart()
|
||||
{
|
||||
Collection<Group> groupCollection = groupStore.values();
|
||||
|
||||
return groupCollection.toArray(new Group[groupCollection.size()]);
|
||||
return PATH_PART;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private LinkedHashMap<String, Group> groupStore;
|
||||
|
||||
/** Field description */
|
||||
@Context
|
||||
private UriInfo uriInfo;
|
||||
}
|
||||
|
||||
@@ -19,20 +19,9 @@ import java.util.LinkedHashMap;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -41,9 +30,14 @@ import javax.ws.rs.core.UriInfo;
|
||||
@Singleton
|
||||
@Path("repositories")
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public class RepositoryResource
|
||||
public class RepositoryResource extends AbstractResource<Repository>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String PATH_PART = "repositories";
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -72,85 +66,76 @@ public class RepositoryResource
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param uriInfo
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
* @param item
|
||||
*/
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response add(@Context UriInfo uriInfo, Repository repository)
|
||||
@Override
|
||||
protected void addItem(Repository item)
|
||||
{
|
||||
repositoryStore.put(repository.getName(), repository);
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(
|
||||
"repositories/".concat(repository.getName()))).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{name}")
|
||||
public Response delete(@PathParam("name") String name)
|
||||
{
|
||||
Repository repository = repositoryStore.get(name);
|
||||
|
||||
if (repository == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
repositoryStore.remove(name);
|
||||
|
||||
return Response.noContent().build();
|
||||
repositoryStore.put(item.getName(), item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param uriInfo
|
||||
* @param name
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
* @param item
|
||||
*/
|
||||
@PUT
|
||||
@Path("{name}")
|
||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response update(@Context UriInfo uriInfo,
|
||||
@PathParam("name") String name, Repository repository)
|
||||
@Override
|
||||
protected void removeItem(Repository item)
|
||||
{
|
||||
Repository updateRepository = repositoryStore.get(name);
|
||||
repositoryStore.remove(item.getName());
|
||||
}
|
||||
|
||||
if (updateRepository == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
* @param item
|
||||
*/
|
||||
@Override
|
||||
protected void updateItem(String name, Repository item)
|
||||
{
|
||||
Repository repository = repositoryStore.get(name);
|
||||
|
||||
updateRepository.setName(name);
|
||||
updateRepository.setDescription(repository.getDescription());
|
||||
updateRepository.setContact(repository.getContact());
|
||||
|
||||
return Response.created(
|
||||
uriInfo.getAbsolutePath().resolve(repository.getName())).build();
|
||||
repository.setContact(item.getContact());
|
||||
repository.setDescription(item.getDescription());
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected Repository[] getAllItems()
|
||||
{
|
||||
Collection<Repository> repositoryCollection = repositoryStore.values();
|
||||
|
||||
return repositoryCollection.toArray(
|
||||
new Repository[repositoryCollection.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected String getId(Repository item)
|
||||
{
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -159,18 +144,10 @@ public class RepositoryResource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Path("{name}")
|
||||
public Repository get(@PathParam("name") String name)
|
||||
@Override
|
||||
protected Repository getItem(String name)
|
||||
{
|
||||
Repository repository = repositoryStore.get(name);
|
||||
|
||||
if (repository == null)
|
||||
{
|
||||
throw new WebApplicationException(Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
return repository;
|
||||
return repositoryStore.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,13 +156,10 @@ public class RepositoryResource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
public Repository[] getAll()
|
||||
@Override
|
||||
protected String getPathPart()
|
||||
{
|
||||
Collection<Repository> repositoryCollection = repositoryStore.values();
|
||||
|
||||
return repositoryCollection.toArray(
|
||||
new Repository[repositoryCollection.size()]);
|
||||
return PATH_PART;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user