Introduce index resource with first links

This commit is contained in:
René Pfeuffer
2018-09-26 17:00:13 +02:00
parent b645901704
commit 357ccc7ddb
7 changed files with 148 additions and 7 deletions

View File

@@ -0,0 +1,11 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
public class IndexDto extends HalRepresentation {
IndexDto(Links links) {
super(links);
}
}

View File

@@ -0,0 +1,34 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Link;
import de.otto.edison.hal.Links;
import org.apache.shiro.SecurityUtils;
import javax.inject.Inject;
public class IndexDtoGenerator {
private final ResourceLinks resourceLinks;
@Inject
public IndexDtoGenerator(ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
}
public IndexDto generate() {
Links.Builder builder = Links.linkingTo();
if (SecurityUtils.getSubject().isAuthenticated()) {
builder.single(
Link.link("me", resourceLinks.me().self()),
Link.link("logout", resourceLinks.authentication().logout())
);
} else {
builder.single(
Link.link("formLogin", resourceLinks.authentication().formLogin()),
Link.link("jsonLogin", resourceLinks.authentication().jsonLogin())
);
}
return new IndexDto(builder.build());
}
}

View File

@@ -0,0 +1,22 @@
package sonia.scm.api.v2.resources;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path(IndexResource.INDEX_PATH_V2)
public class IndexResource {
public static final String INDEX_PATH_V2 = "v2/";
private final IndexDtoGenerator indexDtoGenerator;
@Inject
public IndexResource(IndexDtoGenerator indexDtoGenerator) {
this.indexDtoGenerator = indexDtoGenerator;
}
@GET
public IndexDto getIndex() {
return indexDtoGenerator.generate();
}
}

View File

@@ -3,7 +3,6 @@ package sonia.scm.api.v2.resources;
import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
class ResourceLinks {
@@ -473,4 +472,28 @@ class ResourceLinks {
return uiPluginCollectionLinkBuilder.method("plugins").parameters().method("getInstalledPlugins").parameters().href();
}
}
public AuthenticationLinks authentication() {
return new AuthenticationLinks(scmPathInfoStore.get());
}
static class AuthenticationLinks {
private final LinkBuilder loginLinkBuilder;
AuthenticationLinks(ScmPathInfo pathInfo) {
this.loginLinkBuilder = new LinkBuilder(pathInfo, AuthenticationResource.class);
}
String formLogin() {
return loginLinkBuilder.method("authenticateViaForm").parameters().href();
}
String jsonLogin() {
return loginLinkBuilder.method("authenticateViaJSONBody").parameters().href();
}
String logout() {
return loginLinkBuilder.method("logout").parameters().href();
}
}
}