Merged in feature/plugin_center (pull request #284)

Feature/plugin center
This commit is contained in:
Eduard Heimbuch
2019-07-11 11:31:35 +00:00
36 changed files with 1482 additions and 195 deletions

View File

@@ -8,6 +8,7 @@ import org.apache.shiro.SecurityUtils;
import sonia.scm.SCMContextProvider;
import sonia.scm.config.ConfigurationPermissions;
import sonia.scm.group.GroupPermissions;
import sonia.scm.plugin.PluginPermissions;
import sonia.scm.repository.RepositoryRolePermissions;
import sonia.scm.security.PermissionPermissions;
import sonia.scm.user.UserPermissions;
@@ -34,11 +35,15 @@ public class IndexDtoGenerator extends HalAppenderMapper {
List<Link> autoCompleteLinks = Lists.newArrayList();
builder.self(resourceLinks.index().self());
builder.single(link("uiPlugins", resourceLinks.uiPluginCollection().self()));
if (SecurityUtils.getSubject().isAuthenticated()) {
builder.single(
link("me", resourceLinks.me().self()),
link("logout", resourceLinks.authentication().logout())
);
if (PluginPermissions.read().isPermitted()) {
builder.single(link("plugins", resourceLinks.pluginCollection().self()));
}
if (UserPermissions.list().isPermitted()) {
builder.single(link("users", resourceLinks.userCollection().self()));
}

View File

@@ -0,0 +1,24 @@
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 PluginDto extends HalRepresentation {
private String name;
private String type;
private String version;
private String author;
private String description;
public PluginDto(Links links) {
add(links);
}
}

View File

@@ -0,0 +1,45 @@
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.plugin.PluginWrapper;
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 PluginDtoCollectionMapper {
private final ResourceLinks resourceLinks;
private final PluginDtoMapper mapper;
@Inject
public PluginDtoCollectionMapper(ResourceLinks resourceLinks, PluginDtoMapper mapper) {
this.resourceLinks = resourceLinks;
this.mapper = mapper;
}
public HalRepresentation map(Collection<PluginWrapper> plugins) {
List<PluginDto> dtos = plugins.stream().map(mapper::map).collect(toList());
return new HalRepresentation(createLinks(), embedDtos(dtos));
}
private Links createLinks() {
String baseUrl = resourceLinks.pluginCollection().self();
Links.Builder linksBuilder = linkingTo()
.with(Links.linkingTo().self(baseUrl).build());
return linksBuilder.build();
}
private Embedded embedDtos(List<PluginDto> dtos) {
return embeddedBuilder()
.with("plugins", dtos)
.build();
}
}

View File

@@ -0,0 +1,32 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Links;
import sonia.scm.plugin.PluginWrapper;
import javax.inject.Inject;
import static de.otto.edison.hal.Links.linkingTo;
public class PluginDtoMapper {
private final ResourceLinks resourceLinks;
@Inject
public PluginDtoMapper(ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
}
public PluginDto map(PluginWrapper plugin) {
Links.Builder linksBuilder = linkingTo()
.self(resourceLinks.plugin()
.self(plugin.getPlugin().getInformation().getId(false)));
PluginDto pluginDto = new PluginDto(linksBuilder.build());
pluginDto.setName(plugin.getPlugin().getInformation().getName());
pluginDto.setType(plugin.getPlugin().getInformation().getCategory() != null ? plugin.getPlugin().getInformation().getCategory() : "Miscellaneous");
pluginDto.setVersion(plugin.getPlugin().getInformation().getVersion());
pluginDto.setAuthor(plugin.getPlugin().getInformation().getAuthor());
pluginDto.setDescription(plugin.getPlugin().getInformation().getDescription());
return pluginDto;
}
}

View File

@@ -0,0 +1,87 @@
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.plugin.Plugin;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginPermissions;
import sonia.scm.plugin.PluginWrapper;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
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;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound;
public class PluginResource {
private final PluginLoader pluginLoader;
private final PluginDtoCollectionMapper collectionMapper;
private final PluginDtoMapper mapper;
@Inject
public PluginResource(PluginLoader pluginLoader, PluginDtoCollectionMapper collectionMapper, PluginDtoMapper mapper) {
this.pluginLoader = pluginLoader;
this.collectionMapper = collectionMapper;
this.mapper = mapper;
}
/**
* Returns a collection of installed plugins.
*
* @return collection of installed plugins.
*/
@GET
@Path("")
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(CollectionDto.class)
@Produces(VndMediaType.PLUGIN_COLLECTION)
public Response getInstalledPlugins() {
PluginPermissions.read().check();
List<PluginWrapper> plugins = new ArrayList<>(pluginLoader.getInstalledPlugins());
return Response.ok(collectionMapper.map(plugins)).build();
}
/**
* Returns the installed plugin with the given id.
*
* @param id id of plugin
*
* @return installed plugin with specified id
*/
@GET
@Path("{id}")
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 404, condition = "not found"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(PluginDto.class)
@Produces(VndMediaType.PLUGIN)
public Response getInstalledPlugin(@PathParam("id") String id) {
PluginPermissions.read().check();
Optional<PluginDto> pluginDto = pluginLoader.getInstalledPlugins()
.stream()
.filter(plugin -> id.equals(plugin.getPlugin().getInformation().getId(false)))
.map(mapper::map)
.findFirst();
if (pluginDto.isPresent()) {
return Response.ok(pluginDto.get()).build();
} else {
throw notFound(entity(Plugin.class, id));
}
}
}

View File

@@ -0,0 +1,21 @@
package sonia.scm.api.v2.resources;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.Path;
@Path("v2/")
public class PluginRootResource {
private Provider<PluginResource> pluginResourceProvider;
@Inject
public PluginRootResource(Provider<PluginResource> pluginResourceProvider) {
this.pluginResourceProvider = pluginResourceProvider;
}
@Path("plugins")
public PluginResource plugins() {
return pluginResourceProvider.get();
}
}

View File

@@ -651,6 +651,38 @@ class ResourceLinks {
}
}
public PluginLinks plugin() {
return new PluginLinks(scmPathInfoStore.get());
}
static class PluginLinks {
private final LinkBuilder pluginLinkBuilder;
PluginLinks(ScmPathInfo pathInfo) {
pluginLinkBuilder = new LinkBuilder(pathInfo, PluginRootResource.class, PluginResource.class);
}
String self(String id) {
return pluginLinkBuilder.method("plugins").parameters().method("getInstalledPlugin").parameters(id).href();
}
}
public PluginCollectionLinks pluginCollection() {
return new PluginCollectionLinks(scmPathInfoStore.get());
}
static class PluginCollectionLinks {
private final LinkBuilder pluginCollectionLinkBuilder;
PluginCollectionLinks(ScmPathInfo pathInfo) {
pluginCollectionLinkBuilder = new LinkBuilder(pathInfo, PluginRootResource.class, PluginResource.class);
}
String self() {
return pluginCollectionLinkBuilder.method("plugins").parameters().method("getInstalledPlugins").parameters().href();
}
}
public AuthenticationLinks authentication() {
return new AuthenticationLinks(scmPathInfoStore.get());
}