Skip to content

Commit 6ffd867

Browse files
author
Eugen
committed
rest discoverability work
1 parent 8b409e0 commit 6ffd867

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

spring-security-rest/src/main/java/org/baeldung/web/controller/FooController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.baeldung.web.controller;
22

3+
import java.net.URI;
4+
35
import javax.servlet.http.HttpServletRequest;
46
import javax.servlet.http.HttpServletResponse;
57

@@ -15,6 +17,7 @@
1517
import org.springframework.web.bind.annotation.RequestMethod;
1618
import org.springframework.web.bind.annotation.ResponseBody;
1719
import org.springframework.web.bind.annotation.ResponseStatus;
20+
import org.springframework.web.util.UriTemplate;
1821

1922
import com.google.common.base.Preconditions;
2023

@@ -47,4 +50,14 @@ public void create(@RequestBody final Foo resource, final HttpServletRequest req
4750
eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource));
4851
}
4952

53+
@RequestMapping(value = "admin", method = RequestMethod.GET)
54+
@ResponseStatus(value = HttpStatus.NO_CONTENT)
55+
public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) {
56+
final String rootUri = request.getRequestURL().toString();
57+
58+
final URI fooUri = new UriTemplate("{rootUri}/{resource}").expand(rootUri, "foo");
59+
final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
60+
response.addHeader("Link", linkToFoo);
61+
}
62+
5063
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.web.controller;
2+
3+
import javax.servlet.http.HttpServletResponse;
4+
5+
/**
6+
* Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object
7+
*/
8+
public final class LinkUtil {
9+
10+
private LinkUtil() {
11+
throw new AssertionError();
12+
}
13+
14+
//
15+
16+
/**
17+
* Creates a Link Header to be stored in the {@link HttpServletResponse} to provide Discoverability features to the user
18+
*
19+
* @param uri
20+
* the base uri
21+
* @param rel
22+
* the relative path
23+
*
24+
* @return the complete url
25+
*/
26+
public static String createLinkHeader(final String uri, final String rel) {
27+
return "<" + uri + ">; rel=\"" + rel + "\"";
28+
}
29+
30+
}

spring-security-rest/src/main/java/org/baeldung/web/controller/SingleResourceRetrievedDiscoverabilityListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ void addLinkHeaderOnSingleResourceRetrieval(final HttpServletRequest request, fi
2525
final int positionOfLastSlash = requestURL.lastIndexOf("/");
2626
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);
2727

28-
// final String linkHeaderValue = RESTURLUtil.createLinkHeader(uriForResourceCreation, "collection");
29-
// response.addHeader(LINK_HEADER, linkHeaderValue);
28+
final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection");
29+
response.addHeader("Link", linkHeaderValue);
3030
}
3131

3232
}

0 commit comments

Comments
 (0)