Skip to content

Instantly share code, notes, and snippets.

@Xerosigma
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xerosigma/64469a30355f5de0228a to your computer and use it in GitHub Desktop.
Save Xerosigma/64469a30355f5de0228a to your computer and use it in GitHub Desktop.
HATEOAS Client
package com.provider.spring.controller;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Future;
import org.springframework.hateoas.Link;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.provider.employee.EmployeeDTO;
import com.provider.http.HttpComponentsClientHttpRequestFactoryBasicAuth;
import com.provider.spring.rest.Resource;
@Controller
public class EmployeeController {
private static final String REL_SELF = "self";
private static final String REL_SEARCH = "search";
private static final String REL_EMPLOYEE = "employee";
private static final String RESOURCE_URI = "http://localhost:8080/employees";
private static final String RESOURCE_URI2 = "http://user:password@localhost:8080";
private RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth("user", "password"));
private List<EmployeeDTO> employees;
@RequestMapping("/employees")
public String getAllEmployees() {
String result = null;
try {
String resultBody = restTemplate.getForObject(RESOURCE_URI, String.class);
ObjectMapper objectMapper = new ObjectMapper();
Resource<EmployeeDTO> resource = objectMapper.readValue(resultBody, Resource.class);
// Get objects with relation "employee"
for(Link l : resource.getLinks()) {
if(l.getRel().equals(REL_EMPLOYEE)) {
// TODO: Construct EmployeeDTO from Link.
employees.add( linkToObject(l, new EmployeeDTO()) );
// TODO: Add EmployeeDTO to list.
}
}
}
catch(Exception e) {
e.printStackTrace();
result = "error";
return result;
}
return result;
}
public <T> T linkToObject(Link link, T type) throws JsonParseException, JsonMappingException, IOException {
T result = null;
// TODO: Map link to result.
Future<T> future = resolveLink(link, type);
return result;
}
@Async
public <T> Future<T> resolveLink(Link link, T type) throws JsonParseException, JsonMappingException, IOException {
T result = null;
String resultBody = restTemplate.getForObject(link.getHref(), String.class);
ObjectMapper objectMapper = new ObjectMapper();
EmployeeDTO<EmployeeDTO> resource = objectMapper.readValue(resultBody, EmployeeDTO.class);
return new AsyncResult<T>(result);
}
}
package com.provider.employee;
import java.io.Serializable;
import java.util.List;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Links;
import com.provider.spring.rest.Page;
import com.provider.spring.rest.Resource;
public class EmployeeDTO<T> extends Resource<T> implements Serializable {
private Link id;
private String name;
private double[] location;
public Links _links;
public EmployeeDTO() {
super();
this._links = null;
}
public EmployeeDTO(T content, Link... _links) {
super(content, _links);
this._links = new Links(_links);
}
public EmployeeDTO(T content, Iterable<Link> _links) {
super(content, _links);
this._links = new Links((List<Link>) _links);
}
public EmployeeDTO(T content, Page page, Link... _links) {
super(content, page, _links);
this._links = new Links(_links);
}
public EmployeeDTO(T content, Iterable<Link> _links, Page page) {
super(content, _links, page);
this._links = new Links((List<Link>) _links);
}
public EmployeeDTO(T content, Page page, String name, double[] location, Link... _links) {
super(content, page, _links);
this.name = name;
this.location = location;
this._links = new Links(_links);
}
public EmployeeDTO(T content, Iterable<Link> _links, Page page, String name, double[] location) {
super(content, _links, page);
this.name = name;
this.location = location;
this._links = new Links((List<Link>) _links);
}
public Link getId() {
return id;
}
public void setId(Link id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double[] getLocation() {
return location;
}
public void setLocation(double[] location) {
this.location = location;
}
}
package com.provider.spring.rest;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.util.Assert;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
/**
* A simple {@link Resource} wrapping a domain object and adding links to it.
*
* @author Oliver Gierke
*/
@XmlRootElement
public class Resource<T> extends ResourceSupport {
private final T content;
private Page page;
/**
* Creates an empty {@link Resource}.
*/
public Resource() {
this.content = null;
this.page = null;
}
/**
* Creates a new {@link Resource} with the given content and {@link Link}s (optional).
*
* @param content must not be {@literal null}.
* @param links the links to add to the {@link Resource}.
*/
public Resource(T content, Link... links) {
this(content, Arrays.asList(links));
}
/**
* Creates a new {@link Resource} with the given content and {@link Link}s.
*
* @param content must not be {@literal null}.
* @param links the links to add to the {@link Resource}.
*/
public Resource(T content, Iterable<Link> links) {
Assert.notNull(content, "Content must not be null!");
Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!");
this.content = content;
this.add(links);
this.page = null;
}
// FIXME:
public Resource(T content, Page page, Link... links) {
this(content, Arrays.asList(links), page);
}
// FIXME:
public Resource(T content, Iterable<Link> links, Page page) {
Assert.notNull(content, "Content must not be null!");
Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!");
this.content = content;
this.add(links);
this.page = page;
}
/**
* Returns the underlying entity.
*
* @return the content
*/
@JsonUnwrapped
@XmlAnyElement
public T getContent() {
return content;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceSupport#toString()
*/
@Override
public String toString() {
return String.format("Resource { content: %s, %s }", getContent(), super.toString());
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !obj.getClass().equals(getClass())) {
return false;
}
Resource<?> that = (Resource<?>) obj;
boolean contentEqual = this.content == null ? that.content == null : this.content.equals(that.content);
return contentEqual ? super.equals(obj) : false;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceSupport#hashCode()
*/
@Override
public int hashCode() {
int result = super.hashCode();
result += content == null ? 0 : 17 * content.hashCode();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment