Skip to content

Commit 585e773

Browse files
authored
Merge pull request #36 from xenit-eu/management-platform-commits
Add commits from contentgrid-management-platform
2 parents ccc9348 + 364570e commit 585e773

File tree

7 files changed

+88
-450
lines changed

7 files changed

+88
-450
lines changed

src/main/java/com/contentgrid/hateoas/client/hal/HalDocument.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212
import java.util.NoSuchElementException;
13+
import java.util.Objects;
1314
import java.util.Optional;
1415
import lombok.Data;
1516
import lombok.NonNull;
@@ -49,25 +50,36 @@ public HalLink getSelfLink() {
4950
return this.getLink("self").orElseThrow();
5051
}
5152

52-
public Optional<HalLink> getLink(@NonNull String name) {
53-
return Optional.ofNullable(this.links.get(name))
54-
.flatMap(links -> {
55-
if (links.size() == 0) {
53+
public Optional<HalLink> getLink(@NonNull String rel, @NonNull String name) {
54+
return getLinks(rel).flatMap(linksForRel -> linksForRel.stream()
55+
.filter(link -> Objects.equals(name, link.getName()))
56+
.findFirst());
57+
}
58+
59+
public Optional<HalLink> getLink(@NonNull String rel) {
60+
return Optional.ofNullable(this.links.get(rel))
61+
.flatMap(linksForRel -> {
62+
if (linksForRel.isEmpty()) {
5663
return Optional.empty();
57-
} else if (links.size() == 1) {
58-
return links.stream().findFirst();
64+
} else if (linksForRel.size() == 1) {
65+
return linksForRel.stream().findFirst();
5966
}
60-
throw new RuntimeException("Expected 1 link, but has %s links".formatted(links.size()));
67+
throw new RuntimeException("Expected 1 link, but has %s links".formatted(linksForRel.size()));
6168
});
6269
}
6370

64-
public Optional<List<HalLink>> getLinks(@NonNull String name) {
65-
return Optional.ofNullable(this.links.get(name));
71+
public Optional<List<HalLink>> getLinks(@NonNull String rel) {
72+
return Optional.ofNullable(this.links.get(rel));
73+
}
74+
75+
public HalLink getRequiredLink(@NonNull String rel) {
76+
return this.getLink(rel)
77+
.orElseThrow(() -> new NoSuchElementException("Link with rel %s not found".formatted(rel)));
6678
}
6779

68-
public HalLink getRequiredLink(@NonNull String name) {
69-
return this.getLink(name)
70-
.orElseThrow(() -> new NoSuchElementException("Link with name %s not found".formatted(name)));
80+
public HalLink getRequiredLink(@NonNull String rel, @NonNull String name) {
81+
return this.getLink(rel, name)
82+
.orElseThrow(() -> new NoSuchElementException("Link with rel %s and name %s not found".formatted(rel, name)));
7183
}
7284

7385
public Optional<List<HalDocument>> getEmbedded(@NonNull String name) {
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
package com.contentgrid.hateoas.client.hal;
22

33
import java.net.URI;
4+
import java.util.Map;
45
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
57
import lombok.Data;
6-
import lombok.Getter;
78
import lombok.NonNull;
8-
import lombok.RequiredArgsConstructor;
9+
import org.springframework.hateoas.UriTemplate;
10+
import org.springframework.lang.Nullable;
911

1012
@Data
11-
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
13+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
1214
public class HalLink {
1315

1416
@NonNull
1517
String href;
1618

19+
@Nullable
20+
String name;
21+
22+
boolean templated;
23+
1724
public URI getURI() {
1825
return URI.create(this.href);
1926
}
2027

28+
public URI expand(Map<String, Object> variables) {
29+
return UriTemplate.of(href).expand(variables);
30+
}
31+
2132
public static HalLink from(@NonNull URI uri) {
22-
return new HalLink(uri.toString());
33+
return from(uri, null);
34+
}
35+
36+
public static HalLink from(@NonNull URI uri, @Nullable String name) {
37+
return new HalLink(uri.toString(), name, false);
38+
}
39+
40+
public static HalLink templated(@NonNull UriTemplate uriTemplate) {
41+
return templated(uriTemplate, null);
42+
}
43+
44+
public static HalLink templated(@NonNull UriTemplate uriTemplate, @Nullable String name) {
45+
return new HalLink(uriTemplate.toString(), name, true);
2346
}
2447

2548
@Override
2649
public String toString() {
27-
return "{href=%s}".formatted(this.href);
50+
return "{href=%s%s}".formatted(this.href, this.name == null ? "":", name=" + this.name);
2851
}
2952
}

src/main/java/com/contentgrid/hateoas/client/hal/HalResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import java.util.Objects;
44
import org.springframework.http.ResponseEntity;
5-
import org.springframework.lang.Nullable;
65

76
public interface HalResponse {
87

98
<T> ResponseEntity<T> toEntity(Class<T> bodyType);
109

10+
default ResponseEntity<Void> toVoidEntity() {
11+
return this.toEntity(Void.class);
12+
}
13+
1114
default ResponseEntity<HalDocument> toHalEntity() {
1215
return this.toEntity(HalDocument.class);
1316
}

src/main/java/com/contentgrid/hateoas/client/hal/forms/DefaultHalFormsClient.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.hateoas.MediaTypes;
1919
import org.springframework.http.HttpMethod;
2020
import org.springframework.http.MediaType;
21-
import org.springframework.http.ResponseEntity;
21+
import org.springframework.util.MultiValueMap;
2222
import org.springframework.web.client.RestClient;
2323
import org.springframework.web.client.RestClient.RequestBodySpec;
2424
import org.springframework.web.client.RestClient.RequestHeadersSpec;
@@ -49,7 +49,7 @@ public HalFormsBodyRequest requestTemplate(@NonNull HalDocument document, @NonNu
4949
.contentType(contentType)
5050
.accept(MediaTypes.HAL_FORMS_JSON);
5151

52-
return new DefaultHalFormsBodyRequest(bodySpec, template.getProperties());
52+
return new DefaultHalFormsBodyRequest(bodySpec, contentType, template.getProperties());
5353
}
5454

5555
public Optional<HalFormsTemplate> getTemplate(@NonNull HalDocument document, @NonNull String name) {
@@ -99,6 +99,9 @@ static class DefaultHalFormsBodyRequest implements HalFormsBodyRequest {
9999
@NonNull
100100
private final RequestBodySpec request;
101101

102+
@NonNull
103+
private final MediaType contentType;
104+
102105
@NonNull
103106
private final List<HalFormsProperty> properties;
104107

@@ -118,8 +121,14 @@ public HalFormsBodyRequest properties(Function<HalFormsProperty, Object> valueFu
118121
body.put(property.name, valueFunction.apply(property));
119122
}
120123

121-
request.body(body, new ParameterizedTypeReference<Map<String, Object>>(){
122-
});
124+
if (contentType.includes(MediaType.APPLICATION_FORM_URLENCODED) || contentType.includes(MediaType.MULTIPART_FORM_DATA)) {
125+
// body must be a MultiValuedMap because the HttpMessageConverter only supports MultiValuedMap
126+
request.body(MultiValueMap.fromSingleValue(body), new ParameterizedTypeReference<MultiValueMap<String, Object>>() {
127+
});
128+
} else {
129+
request.body(body, new ParameterizedTypeReference<Map<String, Object>>() {
130+
});
131+
}
123132

124133
return this;
125134
}

0 commit comments

Comments
 (0)