Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed http client from builder, to move to halclient.

  • Loading branch information...
commit 4107d6092b11a97383907f9fa0efc17967348b36 1 parent ca5193c
@talios authored
View
6 README.md
@@ -27,9 +27,3 @@ Halbuilder is a simple Java API for generating and consuming HAL documents confo
ResourceFactory resourceFactory = new ResourceFactory();
Resource resource = resourceFactory.newResource(new InputStreamReader(Some.class.getResourceAsStream("/test.xml")));
-
-### Reading Remote Resources
-
- ResourceFactory resourceFactory = new ResourceFactory();
-
- Future<Resource> resource = resourceFactory.openResource("http://example.com/mike");
View
7 pom.xml
@@ -139,12 +139,5 @@
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
- <dependency>
- <groupId>com.ning</groupId>
- <artifactId>async-http-client</artifactId>
- <version>1.7.0</version>
- </dependency>
-
-
</dependencies>
</project>
View
32 src/main/java/com/theoryinpractise/halbuilder/ResourceFactory.java
@@ -5,9 +5,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
-import com.ning.http.client.AsyncCompletionHandler;
-import com.ning.http.client.AsyncHttpClient;
-import com.ning.http.client.Response;
import com.theoryinpractise.halbuilder.impl.ContentType;
import com.theoryinpractise.halbuilder.impl.api.Renderer;
import com.theoryinpractise.halbuilder.impl.json.JsonRenderer;
@@ -21,25 +18,22 @@
import com.theoryinpractise.halbuilder.spi.ResourceException;
import java.io.BufferedReader;
-import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
-import java.util.concurrent.Future;
import static java.lang.String.format;
public class ResourceFactory {
- public static String HALXML = "application/hal+xml";
- public static String HALJSON = "application/hal+json";
+ public static String HAL_XML = "application/hal+xml";
+ public static String HAL_JSON = "application/hal+json";
private Map<ContentType, Class<? extends Renderer>> contentRenderers = Maps.newHashMap();
private TreeMap<String, String> namespaces = Maps.newTreeMap(Ordering.usingToString());
private List<Link> links = Lists.newArrayList();
private String baseHref;
- private AsyncHttpClient httpClient;
public ResourceFactory() {
this("http://localhost");
@@ -47,9 +41,8 @@ public ResourceFactory() {
public ResourceFactory(String baseHref) {
this.baseHref = baseHref;
- this.contentRenderers.put(new ContentType(HALXML), XmlRenderer.class);
- this.contentRenderers.put(new ContentType(HALJSON), JsonRenderer.class);
- this.httpClient = new AsyncHttpClient();
+ this.contentRenderers.put(new ContentType(HAL_XML), XmlRenderer.class);
+ this.contentRenderers.put(new ContentType(HAL_JSON), JsonRenderer.class);
}
public String getBaseHref() {
@@ -91,10 +84,9 @@ public Resource newResource(String href) {
return resource;
}
-
public ReadableResource newResource(Reader reader) {
try {
- Reader bufferedReader = reader.markSupported() ? reader : new BufferedReader(reader);
+ Reader bufferedReader = new BufferedReader(reader);
bufferedReader.mark(1);
char firstChar = (char) bufferedReader.read();
bufferedReader.reset();
@@ -111,19 +103,6 @@ public ReadableResource newResource(Reader reader) {
}
}
- public Future<ReadableResource> openResource(String href) {
- try {
- return httpClient.prepareGet(href).execute(new AsyncCompletionHandler<ReadableResource>() {
- @Override
- public ReadableResource onCompleted(Response response) throws Exception {
- return newResource(new InputStreamReader(response.getResponseBodyAsStream()));
- }
- });
- } catch (Exception e) {
- throw new ResourceException(e);
- }
- }
-
public Renderer<String> lookupRenderer(String contentType) {
for (Map.Entry<ContentType, Class<? extends Renderer>> entry : contentRenderers.entrySet()) {
@@ -136,7 +115,6 @@ public ReadableResource onCompleted(Response response) throws Exception {
throw new ResourceException(e);
}
}
-
}
throw new IllegalArgumentException("Unsupported contentType: " + contentType);
View
19 src/main/java/com/theoryinpractise/halbuilder/impl/resources/BaseResource.java
@@ -78,17 +78,24 @@ public Link getResourceLink() {
}
public Optional<Link> getLinkByRel(String rel) {
- try {
- return Optional.of(Iterables.getOnlyElement(getLinksByRel(rel)));
- } catch (NoSuchElementException e) {
- return Optional.absent();
- }
+ return Optional.fromNullable(Iterables.getFirst(getLinksByRel(rel), null));
}
public List<Link> getLinksByRel(final String rel) {
final String resolvedRelType = resolvableUri.matcher(rel).matches() ? resolveRelativeHref(rel) : rel;
final String curiedRel = currieHref(resolvedRelType);
- return ImmutableList.copyOf(Iterables.filter(getLinks(), new Predicate<Link>() {
+ final ImmutableList.Builder<Link> linkBuilder = ImmutableList.builder();
+
+ linkBuilder.addAll(getLinksByRel(this, curiedRel));
+ for (Resource resource : resources) {
+ linkBuilder.addAll(getLinksByRel(resource, curiedRel));
+ }
+
+ return linkBuilder.build();
+ }
+
+ private List<Link> getLinksByRel(ReadableResource resource, final String curiedRel) {
+ return ImmutableList.copyOf(Iterables.filter(resource.getLinks(), new Predicate<Link>() {
public boolean apply(@Nullable Link relatable) {
return Iterables.contains(WHITESPACE_SPLITTER.split(relatable.getRel()), curiedRel);
}
View
2  src/main/java/com/theoryinpractise/halbuilder/impl/resources/ImmutableResource.java
@@ -47,10 +47,8 @@ public Link getResourceLink() {
}
public String renderContent(String contentType) {
-
Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
return renderAsString(renderer);
-
}
private String renderAsString(final Renderer renderer) {
View
5 src/main/java/com/theoryinpractise/halbuilder/spi/Link.java
@@ -49,10 +49,6 @@ public String getRel() {
return hreflang;
}
- public Future<ReadableResource> openResource() {
- return resourceFactory.openResource(getHref());
- }
-
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
@@ -69,6 +65,5 @@ public String toString() {
sb.append("/>");
return sb.toString();
-
}
}
View
4 src/main/java/com/theoryinpractise/halbuilder/spi/RenderableResource.java
@@ -8,8 +8,4 @@
String renderContent(String contentType);
-// String renderContent(ResourceFactory.HALJSON);
-
-// String renderContent(ResourceFactory.HALXML);
-
}
View
24 src/test/java/com/theoryinpractise/halbuilder/RenderingTest.java
@@ -81,8 +81,8 @@ public void testCustomerHal() {
.withProperty("expired", Boolean.FALSE)
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
}
@@ -95,8 +95,8 @@ public void testHalWithBean() {
.withBean(new Customer(123456, "Example Resource", 33))
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
}
@@ -108,8 +108,8 @@ public void testHalWithFields() {
.withFields(new OtherCustomer(123456, "Example Resource", 33))
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
}
@@ -127,8 +127,8 @@ public void testHalWithSubResources() {
.withProperty("optional", true))
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleWithSubresourceXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleWithSubresourceJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithSubresourceXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithSubresourceJson);
}
@@ -140,8 +140,8 @@ public void testHalWithBeanSubResource() {
.withBeanBasedSubresource("ns:user role:admin", "/user/11", new Customer(11, "Example User", 32))
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleWithSubresourceXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleWithSubresourceJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithSubresourceXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithSubresourceJson);
}
@@ -154,8 +154,8 @@ public void testHalWithBeanMultipleSubResources() {
.withBeanBasedSubresource("ns:user role:admin", "/user/12", new Customer(12, "Example User", 32))
.asRenderableResource();
- assertThat(party.renderContent(ResourceFactory.HALXML)).isEqualTo(exampleWithMultipleSubresourcesXml);
- assertThat(party.renderContent(ResourceFactory.HALJSON)).isEqualTo(exampleWithMultipleSubresourcesJson);
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithMultipleSubresourcesXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithMultipleSubresourcesJson);
}
View
16 src/test/java/com/theoryinpractise/halbuilder/ResourceTest.java
@@ -14,7 +14,7 @@ public void testUndeclaredLinkNamespace() {
resourceFactory.newResource("/test")
.withLink("http://localhost/test/2", "td:test")
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
}
@Test(expectedExceptions = ResourceException.class)
@@ -22,7 +22,7 @@ public void testUndeclaredResourceNamespace() {
resourceFactory.newResource("http://localhost/test")
.withSubresource("td:test", resourceFactory.newResource("/"))
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
}
@Test(expectedExceptions = ResourceException.class)
@@ -30,7 +30,7 @@ public void testUndeclaredResourceLinkNamespace() {
resourceFactory.newResource("http://localhost/test")
.withSubresource("test", resourceFactory.newResource("/").withLink("/", "td:test"))
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
}
@Test(expectedExceptions = ResourceException.class)
@@ -39,14 +39,14 @@ public void testDuplicatePropertyDefinitions() {
.withProperty("name", "Example User")
.withProperty("name", "Example User")
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
}
@Test
public void testHalResourceHrefShouldBeFullyQualified() {
String xml = resourceFactory.newResource("/test")
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/test");
}
@@ -56,7 +56,7 @@ public void testRelativeLinksRenderFullyQualified() {
String xml = resourceFactory.newResource("/")
.withLink("/test", "test")
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/test");
}
@@ -66,7 +66,7 @@ public void testRelativeResourceRenderFullyQualified() {
String xml = resourceFactory.newResource("/")
.withSubresource("test", resourceFactory.newResource("subresource"))
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/subresource");
}
@@ -79,7 +79,7 @@ public void testRelativeResourceLinksRenderFullyQualified() {
.withLink("/sublink1", "sub")
.withLink("~/sublink2", "sub2"))
.asRenderableResource()
- .renderContent(ResourceFactory.HALXML);
+ .renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/sublink1");
assertThat(xml).contains("http://localhost/subresource/sublink2");
View
25 src/test/java/com/theoryinpractise/halbuilder/UrlHalTest.java
@@ -1,25 +0,0 @@
-package com.theoryinpractise.halbuilder;
-
-import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import org.testng.annotations.Test;
-
-import java.net.MalformedURLException;
-import java.util.concurrent.ExecutionException;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class UrlHalTest {
-
- @Test
- public void testHalViaUrl() throws MalformedURLException, ExecutionException, InterruptedException {
-
- ReadableResource resource = new ResourceFactory().openResource(
- "https://raw.github.com/talios/halbuilder/develop/" +
- "src/test/resources/com/theoryinpractise/halbuilder/example.xml").get();
-
- assertThat(resource.getProperties().get("name")).isEqualTo("Example Resource");
-
-
- }
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.