mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
Use request scoped store to transfer UriInfo
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package sonia.scm.api.rest;
|
||||
|
||||
import sonia.scm.api.v2.resources.UriInfoStore;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerRequestFilter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
public class UriInfoFilter implements ContainerRequestFilter {
|
||||
|
||||
private final javax.inject.Provider<UriInfoStore> storeProvider;
|
||||
|
||||
@Inject
|
||||
public UriInfoFilter(javax.inject.Provider<UriInfoStore> storeProvider) {
|
||||
this.storeProvider = storeProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) {
|
||||
storeProvider.get().set(requestContext.getUriInfo());
|
||||
}
|
||||
}
|
||||
@@ -35,12 +35,14 @@ public class GroupCollectionResource extends AbstractManagerResource<Group, Grou
|
||||
public static final int DEFAULT_PAGE_SIZE = 10;
|
||||
private final GroupDtoToGroupMapper dtoToGroupMapper;
|
||||
private final GroupToGroupDtoMapper groupToDtoMapper;
|
||||
private final GroupCollectionToDtoMapper groupCollectionToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public GroupCollectionResource(GroupManager manager, GroupDtoToGroupMapper dtoToGroupMapper, GroupToGroupDtoMapper groupToDtoMapper) {
|
||||
public GroupCollectionResource(GroupManager manager, GroupDtoToGroupMapper dtoToGroupMapper, GroupToGroupDtoMapper groupToDtoMapper, GroupCollectionToDtoMapper groupCollectionToDtoMapper) {
|
||||
super(manager);
|
||||
this.dtoToGroupMapper = dtoToGroupMapper;
|
||||
this.groupToDtoMapper = groupToDtoMapper;
|
||||
this.groupCollectionToDtoMapper = groupCollectionToDtoMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +94,7 @@ public class GroupCollectionResource extends AbstractManagerResource<Group, Grou
|
||||
@QueryParam("desc") boolean desc) {
|
||||
PageResult<Group> pageResult = fetchPage(sortby, desc, page, pageSize);
|
||||
|
||||
return Response.ok(new GroupCollectionToDtoMapper(groupToDtoMapper).map(uriInfo, page, pageSize, pageResult)).build();
|
||||
return Response.ok(groupCollectionToDtoMapper.map(page, pageSize, pageResult)).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,7 +9,6 @@ import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupPermissions;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -24,33 +23,35 @@ import static sonia.scm.api.v2.resources.ResourceLinks.groupCollection;
|
||||
public class GroupCollectionToDtoMapper {
|
||||
|
||||
private final GroupToGroupDtoMapper groupToDtoMapper;
|
||||
private final UriInfoStore uriInfoStore;
|
||||
|
||||
@Inject
|
||||
public GroupCollectionToDtoMapper(GroupToGroupDtoMapper groupToDtoMapper) {
|
||||
public GroupCollectionToDtoMapper(GroupToGroupDtoMapper groupToDtoMapper, UriInfoStore uriInfoStore) {
|
||||
this.groupToDtoMapper = groupToDtoMapper;
|
||||
this.uriInfoStore = uriInfoStore;
|
||||
}
|
||||
|
||||
public GroupCollectionDto map(UriInfo uriInfo, int pageNumber, int pageSize, PageResult<Group> pageResult) {
|
||||
public GroupCollectionDto map(int pageNumber, int pageSize, PageResult<Group> pageResult) {
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.hasMore());
|
||||
List<GroupDto> dtos = pageResult.getEntities().stream().map(user -> groupToDtoMapper.map(user, uriInfo)).collect(Collectors.toList());
|
||||
List<GroupDto> dtos = pageResult.getEntities().stream().map(user -> groupToDtoMapper.map(user, uriInfoStore.get())).collect(Collectors.toList());
|
||||
|
||||
GroupCollectionDto groupCollectionDto = new GroupCollectionDto(
|
||||
createLinks(uriInfo, paging),
|
||||
createLinks(paging),
|
||||
embedDtos(dtos)
|
||||
);
|
||||
groupCollectionDto.setPage(pageNumber);
|
||||
return groupCollectionDto;
|
||||
}
|
||||
|
||||
private static Links createLinks(UriInfo uriInfo, NumberedPaging page) {
|
||||
String baseUrl = groupCollection(uriInfo).self();
|
||||
private Links createLinks(NumberedPaging page) {
|
||||
String baseUrl = groupCollection(uriInfoStore.get()).self();
|
||||
|
||||
Links.Builder linksBuilder = linkingTo()
|
||||
.with(page.links(
|
||||
fromTemplate(baseUrl + "{?page,pageSize}"),
|
||||
EnumSet.allOf(PagingRel.class)));
|
||||
if (GroupPermissions.create().isPermitted()) {
|
||||
linksBuilder.single(link("create", groupCollection(uriInfo).create()));
|
||||
linksBuilder.single(link("create", groupCollection(uriInfoStore.get()).create()));
|
||||
}
|
||||
return linksBuilder.build();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.servlet.ServletScopes;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
public class MapperModule extends AbstractModule {
|
||||
@@ -8,8 +9,12 @@ public class MapperModule extends AbstractModule {
|
||||
protected void configure() {
|
||||
bind(UserDtoToUserMapper.class).to(Mappers.getMapper(UserDtoToUserMapper.class).getClass());
|
||||
bind(UserToUserDtoMapper.class).to(Mappers.getMapper(UserToUserDtoMapper.class).getClass());
|
||||
bind(UserCollectionToDtoMapper.class);
|
||||
|
||||
bind(GroupToGroupDtoMapper.class).to(Mappers.getMapper(GroupToGroupDtoMapper.class).getClass());
|
||||
bind(GroupDtoToGroupMapper.class).to(Mappers.getMapper(GroupDtoToGroupMapper.class).getClass());
|
||||
bind(GroupToGroupDtoMapper.class).to(Mappers.getMapper(GroupToGroupDtoMapper.class).getClass());
|
||||
bind(GroupCollectionToDtoMapper.class);
|
||||
|
||||
bind(UriInfoStore.class).in(ServletScopes.REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
public class UriInfoStore {
|
||||
|
||||
private UriInfo uriInfo;
|
||||
|
||||
public UriInfo get() {
|
||||
return uriInfo;
|
||||
}
|
||||
|
||||
public void set(UriInfo uriInfo) {
|
||||
if (this.uriInfo != null) {
|
||||
throw new IllegalStateException("UriInfo already set");
|
||||
}
|
||||
this.uriInfo = uriInfo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user