overall cleanup
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.web.dto.Foo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FooService {
|
||||
|
||||
public FooService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public Foo getById(final Long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Long create(final Foo resource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package org.baeldung.web.controller;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.baeldung.persistence.service.FooService;
|
||||
import org.baeldung.web.dto.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
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;
|
||||
|
||||
@Controller
|
||||
public class FooController {
|
||||
|
||||
@Autowired
|
||||
private FooService service;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
// API
|
||||
|
||||
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
|
||||
final Foo resourceById = Preconditions.checkNotNull(service.getById(id));
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
|
||||
return resourceById;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "admin/foo", method = RequestMethod.POST)
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
final Long idOfCreatedResource = service.create(resource);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
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 + "\"";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package org.baeldung.web.controller;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpServletRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public long getIdOfNewResource() {
|
||||
return idOfNewResource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.baeldung.web.controller;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpServletRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
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 = LinkUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
response.addHeader("Link", linkHeaderValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.baeldung.web.dto;
|
||||
|
||||
public class Foo {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user