Skip to content

Commit 1335805

Browse files
committed
ACC-2100 use UriTemplateMatcher from contentgrid-hateoas
1 parent b738aa5 commit 1335805

File tree

3 files changed

+33
-54
lines changed

3 files changed

+33
-54
lines changed

contentgrid-appserver-rest/src/main/java/com/contentgrid/appserver/rest/property/handler/XToManyRelationRequestHandler.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.contentgrid.appserver.query.engine.api.exception.EntityNotFoundException;
1717
import com.contentgrid.appserver.rest.EntityRestController;
1818
import com.contentgrid.appserver.rest.converter.UriListHttpServletRequestConverter;
19+
import com.contentgrid.hateoas.spring.links.UriTemplateMatcher;
1920
import java.net.URI;
2021
import java.util.List;
2122
import java.util.Map;
@@ -50,6 +51,15 @@ protected Optional<Relation> findProperty(Application application, Entity entity
5051
.filter(relation -> relation instanceof OneToManyRelation || relation instanceof ManyToManyRelation);
5152
}
5253

54+
private UriTemplateMatcher<EntityId> getMatcherForTargetEntity(Application application, Relation relation) {
55+
var targetPathSegment = relation.getTargetEndPoint().getEntity().getPathSegment();
56+
return UriTemplateMatcher.<EntityId>builder()
57+
.matcherFor(methodOn(EntityRestController.class)
58+
.getEntity(application, targetPathSegment, null),
59+
params -> EntityId.of(UUID.fromString(params.get("instanceId"))))
60+
.build();
61+
}
62+
5363
@Override
5464
protected ResponseEntity<Object> getProperty(
5565
Application application,
@@ -70,24 +80,6 @@ protected ResponseEntity<Object> getProperty(
7080
}
7181
}
7282

73-
private PathSegmentName parseEntityPathSegment(URI uri) {
74-
var path = uri.getPath().split("/");
75-
if (path.length == 3 && path[0].isEmpty()) {
76-
return PathSegmentName.of(path[1]);
77-
} else {
78-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid path %s".formatted(uri.getPath()));
79-
}
80-
}
81-
82-
private EntityId parseEntityId(URI uri) {
83-
var path = uri.getPath().split("/");
84-
if (path.length == 3 && path[0].isEmpty()) {
85-
return EntityId.of(UUID.fromString(path[2]));
86-
} else {
87-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid path %s".formatted(uri.getPath()));
88-
}
89-
}
90-
9183
@Override
9284
protected ResponseEntity<Object> postProperty(
9385
Application application,
@@ -99,19 +91,17 @@ protected ResponseEntity<Object> postProperty(
9991
if (body.isEmpty()) {
10092
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "No entity url provided.");
10193
}
102-
var targetPathSegment = property.getTargetEndPoint().getEntity().getPathSegment();
10394
var dataBuilder = XToManyRelationData.builder()
10495
.entity(property.getSourceEndPoint().getEntity().getName())
10596
.name(property.getSourceEndPoint().getName());
97+
var matcher = getMatcherForTargetEntity(application, property);
10698

10799
for (var element : body) {
108-
var pathSegment = parseEntityPathSegment(element);
109-
if (!targetPathSegment.equals(pathSegment)) {
110-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
111-
"Invalid target entity. Expected %s, got %s.".formatted(targetPathSegment, pathSegment));
100+
var maybeId = matcher.tryMatch(element.toString());
101+
if (maybeId.isEmpty()) {
102+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid target entity.");
112103
}
113-
var targetId = parseEntityId(element);
114-
dataBuilder.ref(targetId);
104+
dataBuilder.ref(maybeId.get());
115105
}
116106
try {
117107
datamodelApi.addRelationItems(application, dataBuilder.build(), instanceId);

contentgrid-appserver-rest/src/main/java/com/contentgrid/appserver/rest/property/handler/XToOneRelationRequestHandler.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.contentgrid.appserver.query.engine.api.exception.EntityNotFoundException;
1717
import com.contentgrid.appserver.rest.EntityRestController;
1818
import com.contentgrid.appserver.rest.converter.UriListHttpServletRequestConverter;
19+
import com.contentgrid.hateoas.spring.links.UriTemplateMatcher;
1920
import java.net.URI;
2021
import java.util.List;
2122
import java.util.Optional;
@@ -48,6 +49,15 @@ protected Optional<Relation> findProperty(Application application, Entity entity
4849
.filter(relation -> relation instanceof OneToOneRelation || relation instanceof ManyToOneRelation);
4950
}
5051

52+
private UriTemplateMatcher<EntityId> getMatcherForTargetEntity(Application application, Relation relation) {
53+
var targetPathSegment = relation.getTargetEndPoint().getEntity().getPathSegment();
54+
return UriTemplateMatcher.<EntityId>builder()
55+
.matcherFor(methodOn(EntityRestController.class)
56+
.getEntity(application, targetPathSegment, null),
57+
params -> EntityId.of(UUID.fromString(params.get("instanceId"))))
58+
.build();
59+
}
60+
5161
@Override
5262
protected ResponseEntity<Object> getProperty(
5363
Application application,
@@ -67,24 +77,6 @@ protected ResponseEntity<Object> getProperty(
6777
}
6878
}
6979

70-
private PathSegmentName parseEntityPathSegment(URI uri) {
71-
var path = uri.getPath().split("/");
72-
if (path.length == 3 && path[0].isEmpty()) {
73-
return PathSegmentName.of(path[1]);
74-
} else {
75-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid path %s".formatted(uri.getPath()));
76-
}
77-
}
78-
79-
private EntityId parseEntityId(URI uri) {
80-
var path = uri.getPath().split("/");
81-
if (path.length == 3 && path[0].isEmpty()) {
82-
return EntityId.of(UUID.fromString(path[2]));
83-
} else {
84-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid path %s".formatted(uri.getPath()));
85-
}
86-
}
87-
8880
@Override
8981
protected ResponseEntity<Object> postProperty(
9082
Application application,
@@ -112,18 +104,15 @@ protected ResponseEntity<Object> putProperty(
112104
if (body.size() > 1) {
113105
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Multiple targets not supported.");
114106
}
115-
var targetPathSegment = property.getTargetEndPoint().getEntity().getPathSegment();
116107
var element = body.getFirst();
117-
118-
var pathSegment = parseEntityPathSegment(element);
119-
if (!targetPathSegment.equals(pathSegment)) {
120-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid target entity. Expected %s, got %s.".formatted(targetPathSegment, pathSegment));
108+
var maybeId = getMatcherForTargetEntity(application, property).tryMatch(element.toString());
109+
if (maybeId.isEmpty()) {
110+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid target entity.");
121111
}
122-
var targetId = parseEntityId(element);
123112
var data = XToOneRelationData.builder()
124113
.entity(property.getSourceEndPoint().getEntity().getName())
125114
.name(property.getSourceEndPoint().getName())
126-
.ref(targetId)
115+
.ref(maybeId.get())
127116
.build();
128117
try {
129118
datamodelApi.setRelation(application, data, instanceId);

contentgrid-appserver-rest/src/test/java/com/contentgrid/appserver/rest/property/handler/RelationRequestHandlerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void clearManyToManyRelation() throws Exception {
268268
}
269269

270270
@Test
271-
void addOneToManyRelationData() throws Exception {
271+
void addOneToManyRelationItems() throws Exception {
272272
var invoice1 = EntityId.of(UUID.randomUUID());
273273
var invoice2 = EntityId.of(UUID.randomUUID());
274274

@@ -287,7 +287,7 @@ void addOneToManyRelationData() throws Exception {
287287
}
288288

289289
@Test
290-
void addManyToManyRelationData() throws Exception {
290+
void addManyToManyRelationItems() throws Exception {
291291
var invoice1 = EntityId.of(UUID.randomUUID());
292292
var invoice2 = EntityId.of(UUID.randomUUID());
293293

@@ -306,7 +306,7 @@ void addManyToManyRelationData() throws Exception {
306306
}
307307

308308
@Test
309-
void removeOneToManyRelationData() throws Exception {
309+
void removeOneToManyRelationItem() throws Exception {
310310
mockMvc.perform(delete("/persons/{sourceId}/invoices/{targetId}", PERSON_ID, INVOICE_ID))
311311
.andExpect(status().isNoContent());
312312

@@ -316,7 +316,7 @@ void removeOneToManyRelationData() throws Exception {
316316
}
317317

318318
@Test
319-
void removeManyToManyRelationData() throws Exception {
319+
void removeManyToManyRelationItem() throws Exception {
320320
mockMvc.perform(delete("/products/{sourceId}/invoices/{targetId}", PRODUCT_ID, INVOICE_ID))
321321
.andExpect(status().isNoContent());
322322

0 commit comments

Comments
 (0)