Fix default revision for hg and fix encoded slashes in URLs

This commit is contained in:
René Pfeuffer
2018-08-23 15:52:02 +02:00
parent 7177cbd3fe
commit ca563dd874
6 changed files with 66 additions and 24 deletions

View File

@@ -17,10 +17,6 @@ public class BrowserResultMapper {
@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();
@@ -38,6 +34,10 @@ public class BrowserResultMapper {
return browserResultDto;
}
private FileObjectDto mapFileObject(FileObject fileObject, NamespaceAndName namespaceAndName, String revision) {
return fileObjectMapper.map(fileObject, namespaceAndName, revision);
}
private void addLinks(BrowserResult browserResult, BrowserResultDto dto, NamespaceAndName namespaceAndName) {
if (browserResult.getRevision() == null) {
dto.add(Links.linkingTo().self(resourceLinks.source().selfWithoutRevision(namespaceAndName.getNamespace(), namespaceAndName.getName())).build());

View File

@@ -10,6 +10,7 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository;
import javax.inject.Inject;
import java.net.URI;
import static de.otto.edison.hal.Link.link;
@@ -25,16 +26,18 @@ public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObject
@AfterMapping
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
String path = fileObject.getPath();
Links.Builder links = Links.linkingTo()
.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, removeFirstSlash(fileObject.getPath())));
.self(addPath(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path));
if (!dto.isDirectory()) {
links.single(link("content", resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, removeFirstSlash(fileObject.getPath()))));
links.single(link("content", addPath(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path)));
}
dto.add(links.build());
}
private String removeFirstSlash(String source) {
return source.startsWith("/") ? source.substring(1) : source;
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
private String addPath(String sourceWithPath, String path) {
return URI.create(sourceWithPath).resolve(path).toASCIIString();
}
}