Skip to content

Commit ca7edff

Browse files
author
Paul Warren
committed
Add support for fully-qualified links
- also make the linkrel configurable Fixes #148
1 parent 53c1e4f commit ca7edff

24 files changed

+1194
-529
lines changed

spring-content-autoconfigure/src/main/java/internal/org/springframework/content/rest/boot/autoconfigure/ContentRestAutoConfiguration.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ContentRestAutoConfiguration {
2222
public static class ContentRestProperties {
2323

2424
private URI baseUri;
25+
private boolean fullyQualifiedLinks = false;
2526

2627
public URI getBaseUri() {
2728
return baseUri;
@@ -30,7 +31,15 @@ public URI getBaseUri() {
3031
public void setBaseUri(URI baseUri) {
3132
this.baseUri = baseUri;
3233
}
33-
}
34+
35+
public boolean fullyQualifiedLinks() {
36+
return this.fullyQualifiedLinks;
37+
}
38+
39+
public void setFullyQualifiedLinks(boolean fullyQualifiedLinks) {
40+
this.fullyQualifiedLinks = fullyQualifiedLinks;
41+
}
42+
}
3443

3544
@Bean
3645
public SpringBootContentRestConfigurer springBootContentRestConfigurer() {

spring-content-autoconfigure/src/main/java/internal/org/springframework/content/rest/boot/autoconfigure/SpringBootContentRestConfigurer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ public SpringBootContentRestConfigurer(ContentRestProperties properties) {
2323
@Override
2424
public void configure(RestConfiguration config) {
2525

26-
if (properties == null || properties.getBaseUri() == null)
26+
if (properties == null) {
2727
return;
28+
}
2829

29-
config.setBaseUri(properties.getBaseUri());
30+
if (properties.getBaseUri() != null) {
31+
config.setBaseUri(properties.getBaseUri());
32+
}
33+
34+
config.setFullyQualifiedLinks(properties.fullyQualifiedLinks());
3035
}
3136
}

spring-content-autoconfigure/src/test/java/org/springframework/content/rest/boot/ContentRestAutoConfigurationTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,22 @@ public class ContentRestAutoConfigurationTest {
5050
});
5151
});
5252

53-
Context("given an environment specifying a base uri", () -> {
53+
Context("given an environment specifying rest properties", () -> {
5454
BeforeEach(() -> {
5555
System.setProperty("spring.content.rest.base-uri", "/contentApi");
56+
System.setProperty("spring.content.rest.content-links", "false");
5657
});
5758
AfterEach(() -> {
5859
System.clearProperty("spring.content.rest.base-uri");
5960
});
60-
It("should have a filesystem properties bean with the correct root set", () -> {
61+
It("should have a filesystem properties bean with the correct properties set", () -> {
6162
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
6263
context.register(TestConfig.class);
6364
context.setServletContext(new MockServletContext());
6465
context.refresh();
6566

66-
assertThat(context.getBean(ContentRestAutoConfiguration.ContentRestProperties.class).getBaseUri(),
67-
is(URI.create("/contentApi")));
67+
assertThat(context.getBean(ContentRestAutoConfiguration.ContentRestProperties.class).getBaseUri(), is(URI.create("/contentApi")));
68+
assertThat(context.getBean(ContentRestAutoConfiguration.ContentRestProperties.class).fullyQualifiedLinks(), is(false));
6869

6970
assertThat(context.getBean(SpringBootContentRestConfigurer.class), is(not(nullValue())));
7071

spring-content-autoconfigure/src/test/java/org/springframework/content/rest/boot/SpringBootContentRestConfigurerTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
1515
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
1616
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.JustBeforeEach;
17+
import static org.mockito.ArgumentMatchers.anyBoolean;
1718
import static org.mockito.ArgumentMatchers.anyObject;
1819
import static org.mockito.ArgumentMatchers.eq;
1920
import static org.mockito.Mockito.mock;
@@ -56,6 +57,17 @@ public class SpringBootContentRestConfigurerTest {
5657
});
5758
});
5859

60+
Context("given a fullyQualifiedLinks property setting", () -> {
61+
62+
BeforeEach(() -> {
63+
properties.setFullyQualifiedLinks(true);
64+
});
65+
66+
It("should set the property on the RestConfiguration", () -> {
67+
verify(restConfig).setFullyQualifiedLinks(eq(true));
68+
});
69+
});
70+
5971
Context("given a null base uri property", () -> {
6072

6173
It("should not set the property on the RestConfiguration", () -> {
@@ -71,6 +83,7 @@ public class SpringBootContentRestConfigurerTest {
7183

7284
It("should not set the property on the RestConfiguration", () -> {
7385
verify(restConfig, never()).setBaseUri(anyObject());
86+
verify(restConfig, never()).setFullyQualifiedLinks(anyBoolean());
7487
});
7588
});
7689
});

spring-content-rest/src/main/asciidoc/rest-baseuri.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ class CustomContentRestMvcConfiguration {
3232
}
3333
----
3434
====
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
== Fully Qualified Links
2+
By default, and where possible, Spring Content REST exports Spring Resources to shortened link URIs. These will often
3+
match the Spring Data Rest Entity URI.
4+
5+
Given the following example:
6+
7+
====
8+
[source, java]
9+
----
10+
@Entity
11+
public class Dvd {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
private Long id;
15+
16+
@ContentId
17+
private UUID contentId;
18+
19+
@ContentLength
20+
private Long contentLength;
21+
22+
@MimeType
23+
private String mimeType;
24+
25+
// getters and setters
26+
}
27+
28+
public interface DvdRepository extends CrudRepository<Dvd, Long> {}
29+
30+
public interface DvdStore extends ContentStore<Dvd, UUID> {}
31+
----
32+
====
33+
34+
As there is only a single associated Spring Resource, Spring Content REST will generate the following URI:
35+
36+
====
37+
[source, java]
38+
----
39+
"_links" : {
40+
"self" : {
41+
...
42+
},
43+
"dvd" : {
44+
...
45+
},
46+
"dvds" : {
47+
"href" : "http://localhost:8080/dvds/1"
48+
}
49+
}
50+
----
51+
====
52+
53+
To generate fully qualified link URIs set the following property:
54+
55+
====
56+
[source, java]
57+
----
58+
spring.content.rest.fullyQualifiedLinks=true
59+
----
60+
====
61+
62+
Or if you are not using Spring Boot, you can do the following:
63+
64+
====
65+
[source, java]
66+
----
67+
@Configuration
68+
class CustomContentRestMvcConfiguration {
69+
70+
@Bean
71+
public ContentRestConfigurer contentRestConfigurer() {
72+
73+
return new ContentRestConfigurer() {
74+
75+
@Override
76+
public void configure(RestConfiguration config) {
77+
config.setFullyQualifiedLinks(true);
78+
}
79+
};
80+
}
81+
}
82+
----
83+
====
84+
85+
Spring Content REST will now generate links as follows:
86+
87+
====
88+
[source, java]
89+
----
90+
"_links" : {
91+
"self" : {
92+
...
93+
},
94+
"dvd" : {
95+
...
96+
},
97+
"content" : {
98+
"href" : "http://localhost:8080/dvds/1/content"
99+
}
100+
}
101+
----
102+
====
103+
104+
where `content` is the extracted property name taken from the field `contentId`.

spring-content-rest/src/main/asciidoc/rest-index.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ include::{spring-versions-jpa-docs}/jpaversions-rest.adoc[leveloffset=+1]
3636
include::rest-cors.adoc[leveloffset=+1]
3737

3838
include::rest-baseuri.adoc[leveloffset=+1]
39+
include::rest-linkrel.adoc[leveloffset=+1]
40+
include::rest-fullyqualifiedlinks.adoc[leveloffset=+1]
3941
//[[appendix]]
4042
//= Appendix
4143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
== Changing the link relation
2+
3+
For each exported Spring Resource, Spring Content REST will generate a suitable linkrel.
4+
5+
However, it can sometimes be useful to control this yourself.
6+
7+
Given the following example:
8+
9+
====
10+
[source, java]
11+
----
12+
@Entity
13+
public class Dvd {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private Long id;
17+
18+
@ContentId
19+
private UUID contentId;
20+
21+
@ContentLength
22+
private Long contentLength;
23+
24+
@MimeType
25+
private String mimeType;
26+
27+
// getters and setters
28+
}
29+
30+
public interface DvdRepository extends CrudRepository<Dvd, Long> {}
31+
32+
public interface DvdStore extends ContentStore<Dvd, UUID> {}
33+
----
34+
====
35+
36+
Spring Content REST will export the Spring Resource to the following linkrel:
37+
38+
====
39+
[source, java]
40+
----
41+
"_links" : {
42+
"self" : {
43+
...
44+
},
45+
"dvd" : {
46+
...
47+
},
48+
"dvds" : {
49+
"href" : "http://localhost:8080/dvds/1"
50+
}
51+
}
52+
----
53+
====
54+
55+
Specifying a `linkRel` on the StoreRestResource, as follows:
56+
57+
====
58+
[source, java]
59+
----
60+
@StoreRestResource(linkRel="content")
61+
public interface DvdStore extends ContentStore<Dvd, UUID> {}
62+
----
63+
====
64+
65+
will result in the following linkrel instead:
66+
67+
====
68+
[source, java]
69+
----
70+
"_links" : {
71+
"self" : {
72+
...
73+
},
74+
"dvd" : {
75+
...
76+
},
77+
"content" : {
78+
"href" : "http://localhost:8080/dvds/1"
79+
}
80+
}
81+
----
82+
====
83+

0 commit comments

Comments
 (0)