HATEOAS cleanup

This commit is contained in:
eugenp 2014-02-13 00:59:29 +02:00
parent 18cdfdc62a
commit 8939fff6ee
9 changed files with 24 additions and 79 deletions

View File

@ -8,10 +8,10 @@ import javax.servlet.http.HttpServletResponse;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService; import org.baeldung.persistence.service.IFooService;
import org.baeldung.web.exception.MyResourceNotFoundException; import org.baeldung.web.exception.MyResourceNotFoundException;
import org.baeldung.web.hateoas.PaginatedResultsRetrievedEvent; import org.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
import org.baeldung.web.util.ResourceCreated; import org.baeldung.web.hateoas.event.ResourceCreatedEvent;
import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
import org.baeldung.web.util.RestPreconditions; import org.baeldung.web.util.RestPreconditions;
import org.baeldung.web.util.SingleResourceRetrieved;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -51,7 +51,7 @@ public class FooController {
public Foo findById(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) { public Foo findById(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response)); eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, request, response));
return resourceById; return resourceById;
} }
@ -83,7 +83,7 @@ public class FooController {
Preconditions.checkNotNull(resource); Preconditions.checkNotNull(resource);
final Long idOfCreatedResource = service.create(resource).getId(); final Long idOfCreatedResource = service.create(resource).getId();
eventPublisher.publishEvent(new ResourceCreated(this, response, idOfCreatedResource)); eventPublisher.publishEvent(new ResourceCreatedEvent(this, response, idOfCreatedResource));
} }
} }

View File

@ -1,33 +0,0 @@
package org.baeldung.web.hateoas;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationEvent;
public class ResourceCreated extends ApplicationEvent {
private final HttpServletResponse response;
private final HttpServletRequest request;
private final long idOfNewResource;
public ResourceCreated(final Object source, final HttpServletRequest request, final HttpServletResponse response, final long idOfNewResource) {
super(source);
this.request = request;
this.response = response;
this.idOfNewResource = idOfNewResource;
}
public HttpServletResponse getResponse() {
return response;
}
public HttpServletRequest getRequest() {
return request;
}
public long getIdOfNewResource() {
return idOfNewResource;
}
}

View File

@ -1,26 +0,0 @@
package org.baeldung.web.hateoas;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationEvent;
public class SingleResourceRetrieved extends ApplicationEvent {
private final HttpServletResponse response;
private final HttpServletRequest request;
public SingleResourceRetrieved(final Object source, final HttpServletRequest request, final HttpServletResponse response) {
super(source);
this.request = request;
this.response = response;
}
public HttpServletResponse getResponse() {
return response;
}
public HttpServletRequest getRequest() {
return request;
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.web.hateoas; package org.baeldung.web.hateoas.event;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,14 +1,14 @@
package org.baeldung.web.util; package org.baeldung.web.hateoas.event;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
public class ResourceCreated extends ApplicationEvent { public class ResourceCreatedEvent extends ApplicationEvent {
private final HttpServletResponse response; private final HttpServletResponse response;
private final long idOfNewResource; private final long idOfNewResource;
public ResourceCreated(final Object source, final HttpServletResponse response, final long idOfNewResource) { public ResourceCreatedEvent(final Object source, final HttpServletResponse response, final long idOfNewResource) {
super(source); super(source);
this.response = response; this.response = response;

View File

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

View File

@ -1,7 +1,8 @@
package org.baeldung.web.hateoas; package org.baeldung.web.hateoas.listener;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
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;

View File

@ -1,10 +1,11 @@
package org.baeldung.web.util; package org.baeldung.web.hateoas.listener;
import java.net.URI; import java.net.URI;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.baeldung.web.hateoas.event.ResourceCreatedEvent;
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 org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@ -12,10 +13,10 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@Component @Component
class ResourceCreatedDiscoverabilityListener implements ApplicationListener<ResourceCreated> { class ResourceCreatedDiscoverabilityListener implements ApplicationListener<ResourceCreatedEvent> {
@Override @Override
public void onApplicationEvent(final ResourceCreated resourceCreatedEvent) { public void onApplicationEvent(final ResourceCreatedEvent resourceCreatedEvent) {
Preconditions.checkNotNull(resourceCreatedEvent); Preconditions.checkNotNull(resourceCreatedEvent);
final HttpServletResponse response = resourceCreatedEvent.getResponse(); final HttpServletResponse response = resourceCreatedEvent.getResponse();

View File

@ -1,8 +1,10 @@
package org.baeldung.web.util; package org.baeldung.web.hateoas.listener;
import javax.servlet.http.HttpServletRequest; 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.util.LinkUtil;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -10,10 +12,10 @@ import com.google.common.base.Preconditions;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
@Component @Component
class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener<SingleResourceRetrieved> { class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener<SingleResourceRetrievedEvent> {
@Override @Override
public void onApplicationEvent(final SingleResourceRetrieved resourceRetrievedEvent) { public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) {
Preconditions.checkNotNull(resourceRetrievedEvent); Preconditions.checkNotNull(resourceRetrievedEvent);
final HttpServletRequest request = resourceRetrievedEvent.getRequest(); final HttpServletRequest request = resourceRetrievedEvent.getRequest();