Added links to sources ressource / refactored code and fixed issues

This commit is contained in:
Philipp Czora
2018-08-14 17:16:10 +02:00
parent f375e076e4
commit f3925fa311
9 changed files with 120 additions and 54 deletions

View File

@@ -1,6 +1,7 @@
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;
@@ -17,6 +18,12 @@ public class BrowserResultDto extends HalRepresentation implements Iterable<File
private String branch;
private List<FileObjectDto> files;
@Override
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
protected HalRepresentation add(Links links) {
return super.add(links);
}
@Override
public Iterator<FileObjectDto> iterator() {
Iterator<FileObjectDto> it = null;

View File

@@ -1,18 +1,27 @@
package sonia.scm.api.v2.resources;
import org.mapstruct.Mapper;
import de.otto.edison.hal.Links;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@Mapper
public abstract class BrowserResultMapper extends BaseMapper<BrowserResult, BrowserResultDto> {
public class BrowserResultMapper {
abstract FileObjectDto mapFileObject(FileObject fileObject);
@Inject
private FileObjectMapper fileObjectMapper;
public BrowserResultDto map(BrowserResult browserResult) {
@Inject
private ResourceLinks resourceLinks;
private FileObjectDto mapFileObject(FileObject fileObject, NamespaceAndName namespaceAndName, String revision) {
return fileObjectMapper.map(fileObject, namespaceAndName, revision);
}
public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName) {
BrowserResultDto browserResultDto = new BrowserResultDto();
browserResultDto.setTag(browserResult.getTag());
@@ -21,11 +30,17 @@ public abstract class BrowserResultMapper extends BaseMapper<BrowserResult, Brow
List<FileObjectDto> fileObjectDtoList = new ArrayList<>();
for (FileObject fileObject : browserResult.getFiles()) {
fileObjectDtoList.add(mapFileObject(fileObject));
fileObjectDtoList.add(mapFileObject(fileObject, namespaceAndName, browserResult.getRevision()));
}
browserResultDto.setFiles(fileObjectDtoList);
this.addLinks(browserResult, browserResultDto, namespaceAndName);
return browserResultDto;
}
private void addLinks(BrowserResult browserResult, BrowserResultDto dto, NamespaceAndName namespaceAndName) {
dto.add(Links.linkingTo().self(resourceLinks.source().self(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision())).build());
}
}

View File

@@ -1,8 +1,25 @@
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.MappingTarget;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
@Mapper
public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObjectDto> {
@Inject
private ResourceLinks resourceLinks;
protected abstract FileObjectDto map(FileObject fileObject, @Context NamespaceAndName namespaceAndName, @Context String revision);
@AfterMapping
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
dto.add(Links.linkingTo().self(resourceLinks.source().withPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, fileObject.getName())).build());
}
}

View File

@@ -26,7 +26,7 @@ public class MapperModule extends AbstractModule {
bind(BranchToBranchDtoMapper.class).to(Mappers.getMapper(BranchToBranchDtoMapper.class).getClass());
bind(BrowserResultMapper.class).to(Mappers.getMapper(BrowserResultMapper.class).getClass());
bind(FileObjectMapper.class).to(Mappers.getMapper(FileObjectMapper.class).getClass());
bind(UriInfoStore.class).in(ServletScopes.REQUEST);
}

View File

@@ -36,7 +36,7 @@ public abstract class RepositoryToRepositoryDtoMapper extends BaseMapper<Reposit
linksBuilder.single(link("tags", resourceLinks.tagCollection().self(target.getNamespace(), target.getName())));
linksBuilder.single(link("branches", resourceLinks.branchCollection().self(target.getNamespace(), target.getName())));
linksBuilder.single(link("changesets", resourceLinks.changeset().self(target.getNamespace(), target.getName())));
linksBuilder.single(link("sources", resourceLinks.source().self(target.getNamespace(), target.getName())));
linksBuilder.single(link("sources", resourceLinks.source().withoutRevision(target.getNamespace(), target.getName())));
target.add(linksBuilder.build());
}
}

View File

@@ -290,13 +290,21 @@ class ResourceLinks {
sourceLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryResource.class, SourceRootResource.class);
}
String self(String namespace, String name) {
String self(String namespace, String name, String revision) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name, revision).method("sources").parameters().method("getAll").parameters().href();
}
String withoutRevision(String namespace, String name) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("getAll").parameters().href();
}
public String source(String namespace, String name, String revision) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("get").parameters(revision).href();
}
public String withPath(String namespace, String name, String revision, String path) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("get").parameters(revision, path).href();
}
}
public PermissionCollectionLinks permissionCollection() {

View File

@@ -23,7 +23,6 @@ public class SourceRootResource {
private final BrowserResultMapper browserResultMapper;
@Inject
public SourceRootResource(RepositoryServiceFactory serviceFactory, BrowserResultMapper browserResultMapper) {
this.serviceFactory = serviceFactory;
@@ -35,23 +34,30 @@ public class SourceRootResource {
@Path("")
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) {
BrowserResult browserResult = null;
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
BrowserResult browserResult;
Response response;
NamespaceAndName namespaceAndName = new NamespaceAndName(namespace, name);
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
BrowseCommandBuilder browseCommand = repositoryService.getBrowseCommand();
browseCommand.setPath("/");
browserResult = browseCommand.getBrowserResult();
if (browserResult != null) {
response = Response.ok(browserResultMapper.map(browserResult, namespaceAndName)).build();
} else {
response = Response.status(Response.Status.NOT_FOUND).build();
}
} catch (RepositoryNotFoundException e) {
e.printStackTrace();
} catch (RepositoryException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
response = Response.status(Response.Status.NOT_FOUND).build();
} catch (RepositoryException | IOException e) {
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.ok(browserResultMapper.map(browserResult)).build();
return response;
}
@GET
@Path("{revision}")
@Path("{revision}/{path: .*}")
public Response get() {
throw new UnsupportedOperationException();
}