discoverability work
This commit is contained in:
parent
279ccd2a55
commit
8b409e07a6
|
@ -8,14 +8,14 @@ import org.springframework.context.ApplicationEvent;
|
|||
public class ResourceCreated extends ApplicationEvent {
|
||||
private final HttpServletResponse response;
|
||||
private final HttpServletRequest request;
|
||||
private final long idOfCreatedResource;
|
||||
private final long idOfNewResource;
|
||||
|
||||
public ResourceCreated(final Object source, final HttpServletRequest request, final HttpServletResponse response, final long idOfCreatedResource) {
|
||||
public ResourceCreated(final Object source, final HttpServletRequest request, final HttpServletResponse response, final long idOfNewResource) {
|
||||
super(source);
|
||||
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.idOfCreatedResource = idOfCreatedResource;
|
||||
this.idOfNewResource = idOfNewResource;
|
||||
}
|
||||
|
||||
// API
|
||||
|
@ -28,8 +28,8 @@ public class ResourceCreated extends ApplicationEvent {
|
|||
return request;
|
||||
}
|
||||
|
||||
public long getIdOfCreatedResource() {
|
||||
return idOfCreatedResource;
|
||||
public long getIdOfNewResource() {
|
||||
return idOfNewResource;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
@Component
|
||||
class ResourceCreatedDiscoverabilityListener implements ApplicationListener<ResourceCreated> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final ResourceCreated resourceCreatedEvent) {
|
||||
Preconditions.checkNotNull(resourceCreatedEvent);
|
||||
|
||||
final HttpServletRequest request = resourceCreatedEvent.getRequest();
|
||||
final HttpServletResponse response = resourceCreatedEvent.getResponse();
|
||||
final long idOfNewResource = resourceCreatedEvent.getIdOfNewResource();
|
||||
|
||||
addLinkHeaderOnResourceCreation(request, response, idOfNewResource);
|
||||
}
|
||||
|
||||
void addLinkHeaderOnResourceCreation(final HttpServletRequest request, final HttpServletResponse response, final long idOfNewResource) {
|
||||
final String requestUrl = request.getRequestURL().toString();
|
||||
final URI uri = new UriTemplate("{requestUrl}/{idOfNewResource}").expand(requestUrl, idOfNewResource);
|
||||
response.setHeader(HttpHeaders.LOCATION, uri.toASCIIString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Component
|
||||
class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener<SingleResourceRetrieved> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final SingleResourceRetrieved resourceRetrievedEvent) {
|
||||
Preconditions.checkNotNull(resourceRetrievedEvent);
|
||||
|
||||
final HttpServletRequest request = resourceRetrievedEvent.getRequest();
|
||||
final HttpServletResponse response = resourceRetrievedEvent.getResponse();
|
||||
addLinkHeaderOnSingleResourceRetrieval(request, response);
|
||||
}
|
||||
|
||||
void addLinkHeaderOnSingleResourceRetrieval(final HttpServletRequest request, final HttpServletResponse response) {
|
||||
final StringBuffer requestURL = request.getRequestURL();
|
||||
final int positionOfLastSlash = requestURL.lastIndexOf("/");
|
||||
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);
|
||||
|
||||
// final String linkHeaderValue = RESTURLUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
// response.addHeader(LINK_HEADER, linkHeaderValue);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue