diff --git a/src/main/java/org/springframework/hateoas/MediaTypes.java b/src/main/java/org/springframework/hateoas/MediaTypes.java index f8d09a14..c5e6df09 100644 --- a/src/main/java/org/springframework/hateoas/MediaTypes.java +++ b/src/main/java/org/springframework/hateoas/MediaTypes.java @@ -23,6 +23,7 @@ import org.springframework.http.MediaType; * @author Oliver Gierke * @author Przemek Nowak * @author Drummond Dawson + * @author Greg Turnquist */ public class MediaTypes { @@ -45,4 +46,14 @@ public class MediaTypes { * Public constant media type for {@code application/hal+json;charset=UTF-8}. */ public static final MediaType HAL_JSON_UTF8 = MediaType.valueOf(HAL_JSON_UTF8_VALUE); + + /** + * A string equivalient of {@link MediaTypes#ALPS_JSON}. + */ + public static final String ALPS_JSON_VALUE = "application/alps+json"; + + /** + * Public constant media type for {@code application/alps+json}. + */ + public static final MediaType ALPS_JSON = MediaType.parseMediaType(ALPS_JSON_VALUE); } diff --git a/src/main/java/org/springframework/hateoas/alps/AlpsLinkDiscoverer.java b/src/main/java/org/springframework/hateoas/alps/AlpsLinkDiscoverer.java new file mode 100644 index 00000000..533521b3 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/alps/AlpsLinkDiscoverer.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.alps; + +import org.springframework.hateoas.LinkDiscoverer; +import org.springframework.hateoas.MediaTypes; +import org.springframework.hateoas.core.JsonPathLinkDiscoverer; + +/** + * {@link LinkDiscoverer} implementation to find ALPS-based links. + * + * @author Greg Turnquist + */ +public class AlpsLinkDiscoverer extends JsonPathLinkDiscoverer { + + public AlpsLinkDiscoverer() { + super("$.descriptors[?(@.name == '%s')].href", MediaTypes.ALPS_JSON); + } + +} diff --git a/src/test/java/org/springframework/hateoas/alps/AlpsLinkDiscoverUnitTest.java b/src/test/java/org/springframework/hateoas/alps/AlpsLinkDiscoverUnitTest.java new file mode 100644 index 00000000..d591f1be --- /dev/null +++ b/src/test/java/org/springframework/hateoas/alps/AlpsLinkDiscoverUnitTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.alps; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.nio.charset.Charset; + +import org.junit.Test; + +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.ResourceLoader; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.LinkDiscoverer; +import org.springframework.hateoas.core.AbstractLinkDiscovererUnitTest; +import org.springframework.util.StreamUtils; + +/** + * @author Greg Turnquist + */ +public class AlpsLinkDiscoverUnitTest extends AbstractLinkDiscovererUnitTest { + + LinkDiscoverer discoverer = new AlpsLinkDiscoverer(); + + ResourceLoader loader = new DefaultResourceLoader(); + + @Test + public void discoversFullyQualifiedRel() { + + Link link = getDiscoverer().findLinkWithRel("http://foo.com/bar", getInputString()); + + assertThat(link, is(notNullValue())); + assertThat(link.getHref(), is("fullRelHref")); + } + + /** + * Return the {@link LinkDiscoverer} to be tested. + * + * @return + */ + @Override + protected LinkDiscoverer getDiscoverer() { + return discoverer; + } + + /** + * Return the JSON structure we expect to find the links in. + * + * @return + */ + @Override + protected String getInputString() { + try { + return StreamUtils.copyToString( + loader.getResource("classpath:org/springframework/hateoas/alps/link-discoverer.json").getInputStream(), + Charset.defaultCharset()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getInputStringWithoutLinkContainer() { + return "{}"; + } +} diff --git a/src/test/java/org/springframework/hateoas/hal/HalLinkDiscovererUnitTest.java b/src/test/java/org/springframework/hateoas/hal/HalLinkDiscovererUnitTest.java index e1bc3fb7..1cc5fb32 100644 --- a/src/test/java/org/springframework/hateoas/hal/HalLinkDiscovererUnitTest.java +++ b/src/test/java/org/springframework/hateoas/hal/HalLinkDiscovererUnitTest.java @@ -19,6 +19,8 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.junit.Test; + +import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkDiscoverer; import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.core.AbstractLinkDiscovererUnitTest; @@ -40,7 +42,11 @@ public class HalLinkDiscovererUnitTest extends AbstractLinkDiscovererUnitTest { */ @Test public void discoversFullyQualifiedRel() { - assertThat(getDiscoverer().findLinkWithRel("http://foo.com/bar", SAMPLE), is(notNullValue())); + + Link link = getDiscoverer().findLinkWithRel("http://foo.com/bar", SAMPLE); + + assertThat(link, is(notNullValue())); + assertThat(link.getHref(), is("fullRelHref")); } /** diff --git a/src/test/resources/org/springframework/hateoas/alps/link-discoverer.json b/src/test/resources/org/springframework/hateoas/alps/link-discoverer.json new file mode 100644 index 00000000..fa149fab --- /dev/null +++ b/src/test/resources/org/springframework/hateoas/alps/link-discoverer.json @@ -0,0 +1,23 @@ +{ + "version" : "1.0", + "doc" : { + "href" : "http://example.org/samples/full/doc.html" + }, + "descriptors" : [ { + "id" : "sfirst descriptor", + "name" : "relation", + "href" : "firstHref" + }, { + "id" : "second description", + "name" : "relation", + "href" : "secondHref" + }, { + "id" : "self descriptor", + "name" : "self", + "href" : "selfHref" + }, { + "id" : "fully qualified descriptor", + "name" : "http://foo.com/bar", + "href" : "fullRelHref" + } ] +} \ No newline at end of file