new static access to current request used for discoverability

This commit is contained in:
eugenp 2014-02-13 01:07:17 +02:00
parent 8939fff6ee
commit 860285bef1
3 changed files with 7 additions and 16 deletions

View File

@ -2,7 +2,6 @@ package org.baeldung.web.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
@ -48,10 +47,10 @@ public class FooController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET) @RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody @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)); final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, request, response)); eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
return resourceById; return resourceById;
} }

View File

@ -1,18 +1,15 @@
package org.baeldung.web.hateoas.event; package org.baeldung.web.hateoas.event;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
public class SingleResourceRetrievedEvent extends ApplicationEvent { public class SingleResourceRetrievedEvent extends ApplicationEvent {
private final HttpServletResponse response; 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); super(source);
this.request = request;
this.response = response; this.response = response;
} }
@ -22,8 +19,4 @@ public class SingleResourceRetrievedEvent extends ApplicationEvent {
return response; return response;
} }
public HttpServletRequest getRequest() {
return request;
}
} }

View File

@ -1,12 +1,12 @@
package org.baeldung.web.hateoas.listener; package org.baeldung.web.hateoas.listener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent; import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
import org.baeldung.web.util.LinkUtil; import org.baeldung.web.util.LinkUtil;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
@ -18,13 +18,12 @@ class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListe
public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) { public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) {
Preconditions.checkNotNull(resourceRetrievedEvent); Preconditions.checkNotNull(resourceRetrievedEvent);
final HttpServletRequest request = resourceRetrievedEvent.getRequest();
final HttpServletResponse response = resourceRetrievedEvent.getResponse(); final HttpServletResponse response = resourceRetrievedEvent.getResponse();
addLinkHeaderOnSingleResourceRetrieval(request, response); addLinkHeaderOnSingleResourceRetrieval(response);
} }
void addLinkHeaderOnSingleResourceRetrieval(final HttpServletRequest request, final HttpServletResponse response) { void addLinkHeaderOnSingleResourceRetrieval(final HttpServletResponse response) {
final StringBuffer requestURL = request.getRequestURL(); final String requestURL = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toASCIIString();
final int positionOfLastSlash = requestURL.lastIndexOf("/"); final int positionOfLastSlash = requestURL.lastIndexOf("/");
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash); final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);