diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/FooController.java index ce3b59e825..8768796e29 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/FooController.java @@ -1,5 +1,7 @@ package org.baeldung.web.controller; +import java.net.URI; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -15,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.util.UriTemplate; import com.google.common.base.Preconditions; @@ -47,4 +50,14 @@ public class FooController { eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource)); } + @RequestMapping(value = "admin", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.NO_CONTENT) + public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) { + final String rootUri = request.getRequestURL().toString(); + + final URI fooUri = new UriTemplate("{rootUri}/{resource}").expand(rootUri, "foo"); + final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection"); + response.addHeader("Link", linkToFoo); + } + } diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/LinkUtil.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/LinkUtil.java new file mode 100644 index 0000000000..a41ebb5a5c --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/LinkUtil.java @@ -0,0 +1,30 @@ +package org.baeldung.web.controller; + +import javax.servlet.http.HttpServletResponse; + +/** + * Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object + */ +public final class LinkUtil { + + private LinkUtil() { + throw new AssertionError(); + } + + // + + /** + * Creates a Link Header to be stored in the {@link HttpServletResponse} to provide Discoverability features to the user + * + * @param uri + * the base uri + * @param rel + * the relative path + * + * @return the complete url + */ + public static String createLinkHeader(final String uri, final String rel) { + return "<" + uri + ">; rel=\"" + rel + "\""; + } + +} diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/SingleResourceRetrievedDiscoverabilityListener.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/SingleResourceRetrievedDiscoverabilityListener.java index b7384b0196..45cd7c4d13 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/SingleResourceRetrievedDiscoverabilityListener.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/SingleResourceRetrievedDiscoverabilityListener.java @@ -25,8 +25,8 @@ class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListe final int positionOfLastSlash = requestURL.lastIndexOf("/"); final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash); - // final String linkHeaderValue = RESTURLUtil.createLinkHeader(uriForResourceCreation, "collection"); - // response.addHeader(LINK_HEADER, linkHeaderValue); + final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection"); + response.addHeader("Link", linkHeaderValue); } } \ No newline at end of file