Add branch collection endpoint

This commit is contained in:
René Pfeuffer
2018-08-07 15:15:12 +02:00
parent 8f4c48b381
commit 14611d89d0
11 changed files with 213 additions and 17 deletions

View File

@@ -1,18 +1,65 @@
package sonia.scm.api.v2.resources;
import javax.ws.rs.DefaultValue;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.Branches;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.CommandNotSupportedException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.IOUtil;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.io.IOException;
public class BranchCollectionResource {
private final RepositoryManager manager;
private final RepositoryServiceFactory servicefactory;
private final BranchCollectionToDtoMapper branchCollectionToDtoMapper;
@Inject
public BranchCollectionResource(RepositoryManager manager, RepositoryServiceFactory servicefactory, BranchCollectionToDtoMapper branchCollectionToDtoMapper) {
this.manager = manager;
this.servicefactory = servicefactory;
this.branchCollectionToDtoMapper = branchCollectionToDtoMapper;
}
@GET
@Path("")
public Response getAll(@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("pageSize") int pageSize,
@QueryParam("sortBy") String sortBy,
@DefaultValue("false") @QueryParam("desc") boolean desc) {
throw new UnsupportedOperationException();
@Produces(VndMediaType.BRANCH_COLLECTION)
@TypeHint(CollectionDto.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"group\" privilege"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException, RepositoryException {
RepositoryService repositoryService;
try {
repositoryService = servicefactory.create(new NamespaceAndName(namespace, name));
} catch (RepositoryNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
}
try {
Branches branches = repositoryService.getBranchesCommand().getBranches();
return Response.ok(branchCollectionToDtoMapper.map(namespace, name, branches.getBranches())).build();
} catch (CommandNotSupportedException ex) {
return Response.status(Response.Status.BAD_REQUEST).build();
} finally {
IOUtil.close(repositoryService);
}
}
}

View File

@@ -0,0 +1,46 @@
package sonia.scm.api.v2.resources;
import com.google.inject.Inject;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import sonia.scm.repository.Branch;
import sonia.scm.repository.NamespaceAndName;
import java.util.Collection;
import java.util.List;
import static de.otto.edison.hal.Embedded.embeddedBuilder;
import static de.otto.edison.hal.Links.linkingTo;
import static java.util.stream.Collectors.toList;
public class BranchCollectionToDtoMapper {
private final ResourceLinks resourceLinks;
private final BranchToBranchDtoMapper branchToDtoMapper;
@Inject
public BranchCollectionToDtoMapper(BranchToBranchDtoMapper branchToDtoMapper, ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
this.branchToDtoMapper = branchToDtoMapper;
}
public HalRepresentation map(String namespace, String name, Collection<Branch> branches) {
List<BranchDto> dtos = branches.stream().map(branch -> branchToDtoMapper.map(branch, new NamespaceAndName(namespace, name))).collect(toList());
return new HalRepresentation(createLinks(namespace, name), embedDtos(dtos));
}
private Links createLinks(String namespace, String name) {
String baseUrl = resourceLinks.branchCollection().self(namespace, name);
Links.Builder linksBuilder = linkingTo()
.with(Links.linkingTo().self(baseUrl).build());
return linksBuilder.build();
}
private Embedded embedDtos(List<BranchDto> dtos) {
return embeddedBuilder()
.with("branches", dtos)
.build();
}
}

View File

@@ -0,0 +1,19 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @Setter @NoArgsConstructor
public class BranchDto extends HalRepresentation {
private String name;
private String revision;
@Override
protected HalRepresentation add(Links links) {
return super.add(links);
}
}

View File

@@ -0,0 +1,30 @@
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.web.VndMediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
public class BranchResource {
@GET
@Path("")
@Produces(VndMediaType.BRANCH)
@TypeHint(BranchDto.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user has no privileges to read the branch"),
@ResponseCode(code = 404, condition = "not found, no branch with the specified name for the repository available"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("branch") String branch) {
return null;
}
}

View File

@@ -7,10 +7,17 @@ import javax.ws.rs.Path;
public class BranchRootResource {
private final Provider<BranchCollectionResource> branchCollectionResource;
private final Provider<BranchResource> branchResource;
@Inject
public BranchRootResource(Provider<BranchCollectionResource> branchCollectionResource) {
public BranchRootResource(Provider<BranchCollectionResource> branchCollectionResource, Provider<BranchResource> branchResource) {
this.branchCollectionResource = branchCollectionResource;
this.branchResource = branchResource;
}
@Path("{branch}")
public BranchResource getBranchResource() {
return branchResource.get();
}
@Path("")

View File

@@ -0,0 +1,30 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Links;
import org.mapstruct.AfterMapping;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import sonia.scm.repository.Branch;
import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
import static de.otto.edison.hal.Links.linkingTo;
@Mapper
public abstract class BranchToBranchDtoMapper {
@Inject
private ResourceLinks resourceLinks;
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
public abstract BranchDto map(Branch branch, @Context NamespaceAndName namespaceAndName);
@AfterMapping
void appendLinks(@MappingTarget BranchDto target, @Context NamespaceAndName namespaceAndName) {
Links.Builder linksBuilder = linkingTo().self(resourceLinks.branch().self(namespaceAndName, target.getName()));
target.add(linksBuilder.build());
}
}

View File

@@ -21,6 +21,8 @@ public class MapperModule extends AbstractModule {
bind(RepositoryToRepositoryDtoMapper.class).to(Mappers.getMapper(RepositoryToRepositoryDtoMapper.class).getClass());
bind(RepositoryDtoToRepositoryMapper.class).to(Mappers.getMapper(RepositoryDtoToRepositoryMapper.class).getClass());
bind(BranchToBranchDtoMapper.class).to(Mappers.getMapper(BranchToBranchDtoMapper.class).getClass());
bind(UriInfoStore.class).in(ServletScopes.REQUEST);
}
}

View File

@@ -136,7 +136,7 @@ public class RepositoryResource {
}
@Path("branches/")
public BranchRootResource branches() {
public BranchRootResource branches(@PathParam("namespace") String namespace, @PathParam("name") String name) {
return branchRootResource.get();
}

View File

@@ -1,5 +1,7 @@
package sonia.scm.api.v2.resources;
import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
import javax.ws.rs.core.UriInfo;
@@ -181,6 +183,22 @@ class ResourceLinks {
}
}
public BranchLinks branch() {
return new BranchLinks(uriInfoStore.get());
}
static class BranchLinks {
private final LinkBuilder branchLinkBuilder;
BranchLinks(UriInfo uriInfo) {
branchLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryResource.class, BranchRootResource.class, BranchResource.class);
}
String self(NamespaceAndName namespaceAndName, String branch) {
return branchLinkBuilder.method("getRepositoryResource").parameters(namespaceAndName.getNamespace(), namespaceAndName.getName()).method("branches").parameters().method("getBranchResource").parameters().method("get").parameters(branch).href();
}
}
public BranchCollectionLinks branchCollection() {
return new BranchCollectionLinks(uriInfoStore.get());
}