#597 - Add ALPS media type and LinkDiscoverer.

Migrate general ALPS material from Spring Data REST into Spring HATEOAS.
This commit is contained in:
Greg Turnquist
2017-06-13 14:14:58 -05:00
committed by Oliver Gierke
parent 312134ee82
commit b0820011e8
5 changed files with 155 additions and 1 deletions

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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 "{}";
}
}

View File

@@ -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"));
}
/**

View File

@@ -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"
} ]
}