mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
Move info initialization to filter
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
|
|
||||||
public class ScmPathInfoStore {
|
public class ScmPathInfoStore {
|
||||||
|
|
||||||
private ScmPathInfo pathInfo;
|
private ScmPathInfo pathInfo;
|
||||||
@@ -10,11 +8,11 @@ public class ScmPathInfoStore {
|
|||||||
return pathInfo;
|
return pathInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFromRestRequest(UriInfo uriInfo) {
|
public void set(ScmPathInfo info) {
|
||||||
if (this.pathInfo != null) {
|
if (this.pathInfo != null) {
|
||||||
throw new IllegalStateException("UriInfo already set");
|
throw new IllegalStateException("UriInfo already set");
|
||||||
}
|
}
|
||||||
this.pathInfo = uriInfo::getBaseUri;
|
this.pathInfo = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import sonia.scm.api.v2.resources.ScmPathInfoStore;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.ws.rs.container.ContainerRequestContext;
|
import javax.ws.rs.container.ContainerRequestContext;
|
||||||
import javax.ws.rs.container.ContainerRequestFilter;
|
import javax.ws.rs.container.ContainerRequestFilter;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
import javax.ws.rs.ext.Provider;
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
@Provider
|
@Provider
|
||||||
@@ -19,6 +20,7 @@ public class UriInfoFilter implements ContainerRequestFilter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void filter(ContainerRequestContext requestContext) {
|
public void filter(ContainerRequestContext requestContext) {
|
||||||
storeProvider.get().setFromRestRequest(requestContext.getUriInfo());
|
UriInfo uriInfo = requestContext.getUriInfo();
|
||||||
|
storeProvider.get().set(uriInfo::getBaseUri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import org.junit.Test;
|
|||||||
import sonia.scm.PageResult;
|
import sonia.scm.PageResult;
|
||||||
import sonia.scm.group.Group;
|
import sonia.scm.group.Group;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -28,7 +27,7 @@ import static sonia.scm.PageResult.createPage;
|
|||||||
|
|
||||||
public class GroupCollectionToDtoMapperTest {
|
public class GroupCollectionToDtoMapperTest {
|
||||||
|
|
||||||
private final UriInfo uriInfo = mock(UriInfo.class);
|
private final ScmPathInfo uriInfo = mock(ScmPathInfo.class);
|
||||||
private final ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
private final ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
||||||
private final ResourceLinks resourceLinks = new ResourceLinks(scmPathInfoStore);
|
private final ResourceLinks resourceLinks = new ResourceLinks(scmPathInfoStore);
|
||||||
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
|
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
|
||||||
@@ -41,10 +40,10 @@ public class GroupCollectionToDtoMapperTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() throws URISyntaxException {
|
public void init() throws URISyntaxException {
|
||||||
scmPathInfoStore.setFromRestRequest(uriInfo);
|
scmPathInfoStore.set(uriInfo);
|
||||||
URI baseUri = new URI("http://example.com/base/");
|
URI baseUri = new URI("http://example.com/base/");
|
||||||
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
||||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
when(uriInfo.getApiRestUri()).thenReturn(baseUri);
|
||||||
subjectThreadState.bind();
|
subjectThreadState.bind();
|
||||||
ThreadContext.bind(subject);
|
ThreadContext.bind(subject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package sonia.scm.api.v2.resources;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
@@ -15,22 +14,22 @@ public class ScmPathInfoStoreTest {
|
|||||||
public void shouldReturnSetInfo() {
|
public void shouldReturnSetInfo() {
|
||||||
URI someUri = URI.create("/anything");
|
URI someUri = URI.create("/anything");
|
||||||
|
|
||||||
UriInfo uriInfo = mock(UriInfo.class);
|
ScmPathInfo uriInfo = mock(ScmPathInfo.class);
|
||||||
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
||||||
|
|
||||||
when(uriInfo.getBaseUri()).thenReturn(someUri);
|
when(uriInfo.getApiRestUri()).thenReturn(someUri);
|
||||||
|
|
||||||
scmPathInfoStore.setFromRestRequest(uriInfo);
|
scmPathInfoStore.set(uriInfo);
|
||||||
|
|
||||||
assertSame(someUri, scmPathInfoStore.get().getApiRestUri());
|
assertSame(someUri, scmPathInfoStore.get().getApiRestUri());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void shouldFailIfSetTwice() {
|
public void shouldFailIfSetTwice() {
|
||||||
UriInfo uriInfo = mock(UriInfo.class);
|
ScmPathInfo uriInfo = mock(ScmPathInfo.class);
|
||||||
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
||||||
|
|
||||||
scmPathInfoStore.setFromRestRequest(uriInfo);
|
scmPathInfoStore.set(uriInfo);
|
||||||
scmPathInfoStore.setFromRestRequest(uriInfo);
|
scmPathInfoStore.set(uriInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user