Commits

Sebastian Sdorra committed 8edb5fb

added AbstractResource

Comments (0)

Files changed (4)

scm-webapp/src/main/java/sonia/scm/api/rest/SecurityFilter.java

       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))
       {

scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractResource.java

+/*
+ * 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();
+  }
+}

scm-webapp/src/main/java/sonia/scm/api/rest/resources/GroupResource.java

 
 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;
 
 /**
  *
 @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 ...
    *
    * 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);
-
-    return Response.created(
-        uriInfo.getAbsolutePath().resolve(
-          "groups/".concat(group.getName()))).build();
+    groupStore.put(item.getName(), item);
   }
 
   /**
    * Method description
    *
    *
-   * @param name
-   *
-   * @return
+   * @param item
    */
-  @DELETE
-  @Path("{name}")
-  public Response delete(@PathParam("name") String name)
+  @Override
+  protected void removeItem(Group item)
   {
-    Group group = groupStore.get(name);
-
-    if (group == null)
-    {
-      throw new WebApplicationException(Status.NOT_FOUND);
-    }
-
-    groupStore.remove(name);
-
-    return Response.noContent().build();
+    groupStore.remove(item.getName());
   }
 
   /**
    * Method description
    *
    *
-   *
    * @param name
-   * @param group
-   *
-   * @return
+   * @param item
    */
-  @PUT
-  @Path("{name}")
-  @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-  public Response update(@PathParam("name") String name, Group group)
+  @Override
+  protected void updateItem(String name, Group item)
   {
-    Group updateGroup = groupStore.get(name);
+    Group group = 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
    *
    *
-   * @param name
-   *
    * @return
    */
-  @GET
-  @Path("{name}")
-  public Group get(@PathParam("name") String name)
-  {
-    Group group = groupStore.get(name);
-
-    if (group == null)
-    {
-      throw new WebApplicationException(Status.NOT_FOUND);
-    }
-
-    return group;
-  }
-
-  /**
-   * Method description
-   *
-   *
-   * @return
-   */
-  @GET
-  public Group[] getAll()
+  @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
+   *
+   *
+   * @param name
+   *
+   * @return
+   */
+  @Override
+  protected Group getItem(String name)
+  {
+    return groupStore.get(name);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  @Override
+  protected String getPathPart()
+  {
+    return PATH_PART;
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
   private LinkedHashMap<String, Group> groupStore;
-
-  /** Field description */
-  @Context
-  private UriInfo uriInfo;
 }

scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java

 
 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;
 
 /**
  *
 @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 ...
    *
   //~--- 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 item
+   */
+  @Override
+  protected void removeItem(Repository item)
+  {
+    repositoryStore.remove(item.getName());
+  }
+
+  /**
+   * 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 updateItem(String name, Repository item)
   {
-    Repository updateRepository = repositoryStore.get(name);
+    Repository repository = repositoryStore.get(name);
 
-    if (updateRepository == null)
-    {
-      throw new WebApplicationException(Status.NOT_FOUND);
-    }
-
-    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
    *
    *
-   * @param name
-   *
    * @return
    */
-  @GET
-  @Path("{name}")
-  public Repository get(@PathParam("name") String name)
-  {
-    Repository repository = repositoryStore.get(name);
-
-    if (repository == null)
-    {
-      throw new WebApplicationException(Status.NOT_FOUND);
-    }
-
-    return repository;
-  }
-
-  /**
-   * Method description
-   *
-   *
-   * @return
-   */
-  @GET
-  public Repository[] getAll()
+  @Override
+  protected Repository[] getAllItems()
   {
     Collection<Repository> repositoryCollection = repositoryStore.values();
 
         new Repository[repositoryCollection.size()]);
   }
 
+  /**
+   * Method description
+   *
+   *
+   * @param item
+   *
+   * @return
+   */
+  @Override
+  protected String getId(Repository item)
+  {
+    return item.getName();
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param name
+   *
+   * @return
+   */
+  @Override
+  protected Repository getItem(String name)
+  {
+    return repositoryStore.get(name);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  @Override
+  protected String getPathPart()
+  {
+    return PATH_PART;
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */