mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Introduce adapter for resource classes and managers
This commit is contained in:
@@ -3,7 +3,6 @@ package sonia.scm.api.v2.resources;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.api.rest.resources.AbstractManagerResource;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupException;
|
||||
import sonia.scm.group.GroupManager;
|
||||
@@ -17,21 +16,20 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.Request;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.Collection;
|
||||
|
||||
@Produces(VndMediaType.GROUP)
|
||||
public class GroupResource extends AbstractManagerResource<Group, GroupException> {
|
||||
public class GroupResource {
|
||||
|
||||
private final GroupToGroupDtoMapper groupToGroupDtoMapper;
|
||||
private final ResourceManagerAdapter<Group, GroupDto, GroupException> adapter;
|
||||
|
||||
@Inject
|
||||
public GroupResource(GroupManager manager, GroupToGroupDtoMapper groupToGroupDtoMapper) {
|
||||
super(manager);
|
||||
this.groupToGroupDtoMapper = groupToGroupDtoMapper;
|
||||
this.adapter = new ResourceManagerAdapter<>(manager);
|
||||
}
|
||||
|
||||
@Path("")
|
||||
@@ -44,12 +42,7 @@ public class GroupResource extends AbstractManagerResource<Group, GroupException
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response get(@Context Request request, @Context UriInfo uriInfo, @PathParam("id") String id) {
|
||||
Group group = manager.get(id);
|
||||
if (group == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
GroupDto groupDto = groupToGroupDtoMapper.map(group);
|
||||
return Response.ok(groupDto).build();
|
||||
return adapter.get(id, groupToGroupDtoMapper::map);
|
||||
}
|
||||
|
||||
@Path("")
|
||||
@@ -63,19 +56,4 @@ public class GroupResource extends AbstractManagerResource<Group, GroupException
|
||||
public Response update(@PathParam("id") String id) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GenericEntity<Collection<Group>> createGenericEntity(Collection<Group> items) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getId(Group item) {
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPathPart() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import sonia.scm.Manager;
|
||||
import sonia.scm.ModelObject;
|
||||
import sonia.scm.api.rest.resources.AbstractManagerResource;
|
||||
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ResourceManagerAdapter<T extends ModelObject, D extends HalRepresentation, E extends Exception> extends AbstractManagerResource<T, E> {
|
||||
|
||||
public ResourceManagerAdapter(Manager<T, E> manager) {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
public Response get(String id, Function<T, D> mapper) {
|
||||
T entity = manager.get(id);
|
||||
if (entity == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
D dto = mapper.apply(entity);
|
||||
return Response.ok(dto).build();
|
||||
}
|
||||
|
||||
public Response update(String id, D dto, Function<T, T> mapper) {
|
||||
T existingEntity = manager.get(id);
|
||||
T changedEntity = mapper.apply(existingEntity);
|
||||
return update(id, changedEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GenericEntity<Collection<T>> createGenericEntity(Collection<T> items) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getId(T item) {
|
||||
return item.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPathPart() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package sonia.scm.api.v2.resources;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.api.rest.resources.AbstractManagerResource;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
@@ -17,24 +16,23 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.Request;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.Collection;
|
||||
|
||||
|
||||
@Produces(VndMediaType.USER)
|
||||
public class UserResource extends AbstractManagerResource<User, UserException> {
|
||||
public class UserResource {
|
||||
|
||||
private final UserDtoToUserMapper dtoToUserMapper;
|
||||
private final UserToUserDtoMapper userToDtoMapper;
|
||||
|
||||
private final ResourceManagerAdapter<User, UserDto, UserException> adapter;
|
||||
|
||||
@Inject
|
||||
public UserResource(UserDtoToUserMapper dtoToUserMapper, UserToUserDtoMapper userToDtoMapper, UserManager manager) {
|
||||
super(manager);
|
||||
this.dtoToUserMapper = dtoToUserMapper;
|
||||
this.userToDtoMapper = userToDtoMapper;
|
||||
this.adapter = new ResourceManagerAdapter<>(manager);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,12 +54,7 @@ public class UserResource extends AbstractManagerResource<User, UserException> {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response get(@Context Request request, @Context UriInfo uriInfo, @PathParam("id") String id) {
|
||||
User user = manager.get(id);
|
||||
if (user == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
UserDto userDto = userToDtoMapper.map(user);
|
||||
return Response.ok(userDto).build();
|
||||
return adapter.get(id, userToDtoMapper::map);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,11 +73,8 @@ public class UserResource extends AbstractManagerResource<User, UserException> {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||
public Response update(@Context UriInfo uriInfo,
|
||||
@PathParam("id") String name, UserDto userDto) {
|
||||
String originalPassword = manager.get(name).getPassword();
|
||||
User user = dtoToUserMapper.map(userDto, originalPassword);
|
||||
return update(name, user);
|
||||
public Response update(@Context UriInfo uriInfo, @PathParam("id") String name, UserDto userDto) {
|
||||
return adapter.update(name, userDto, existing -> dtoToUserMapper.map(userDto, existing.getPassword()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,23 +93,7 @@ public class UserResource extends AbstractManagerResource<User, UserException> {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||
@Override
|
||||
public Response delete(@PathParam("id") String name) {
|
||||
return super.delete(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GenericEntity<Collection<User>> createGenericEntity(Collection<User> items) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getId(User item) {
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPathPart() {
|
||||
throw new UnsupportedOperationException();
|
||||
return adapter.delete(name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user