From 860285bef121daf3f15eda44f79fa6f91b5aa91d Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 13 Feb 2014 01:07:17 +0200 Subject: [PATCH] new static access to current request used for discoverability --- .../java/org/baeldung/web/controller/FooController.java | 5 ++--- .../web/hateoas/event/SingleResourceRetrievedEvent.java | 9 +-------- .../SingleResourceRetrievedDiscoverabilityListener.java | 9 ++++----- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java index 1462b4c9ca..f8fcbbe1dc 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java @@ -2,7 +2,6 @@ package org.baeldung.web.controller; import java.util.List; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.baeldung.persistence.model.Foo; @@ -48,10 +47,10 @@ public class FooController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody - public Foo findById(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) { + public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) { final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); - eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, request, response)); + eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); return resourceById; } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java b/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java index e71fde51e2..0c9eb889e6 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java @@ -1,18 +1,15 @@ package org.baeldung.web.hateoas.event; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationEvent; public class SingleResourceRetrievedEvent extends ApplicationEvent { private final HttpServletResponse response; - private final HttpServletRequest request; - public SingleResourceRetrievedEvent(final Object source, final HttpServletRequest request, final HttpServletResponse response) { + public SingleResourceRetrievedEvent(final Object source, final HttpServletResponse response) { super(source); - this.request = request; this.response = response; } @@ -22,8 +19,4 @@ public class SingleResourceRetrievedEvent extends ApplicationEvent { return response; } - public HttpServletRequest getRequest() { - return request; - } - } \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java b/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java index 3dd57cb78d..32407e9f2b 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java @@ -1,12 +1,12 @@ package org.baeldung.web.hateoas.listener; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent; import org.baeldung.web.util.LinkUtil; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import com.google.common.base.Preconditions; import com.google.common.net.HttpHeaders; @@ -18,13 +18,12 @@ class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListe public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) { Preconditions.checkNotNull(resourceRetrievedEvent); - final HttpServletRequest request = resourceRetrievedEvent.getRequest(); final HttpServletResponse response = resourceRetrievedEvent.getResponse(); - addLinkHeaderOnSingleResourceRetrieval(request, response); + addLinkHeaderOnSingleResourceRetrieval(response); } - void addLinkHeaderOnSingleResourceRetrieval(final HttpServletRequest request, final HttpServletResponse response) { - final StringBuffer requestURL = request.getRequestURL(); + void addLinkHeaderOnSingleResourceRetrieval(final HttpServletResponse response) { + final String requestURL = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toASCIIString(); final int positionOfLastSlash = requestURL.lastIndexOf("/"); final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);