Improvements to LinkBuilder API.

Renamed UriComponentsLinkBuilder to LinkBuilderSupport and moved it from mvc into core package. slash(Object object) now transparently unwraps Identifiables<?>.
This commit is contained in:
Oliver Gierke
2012-10-26 15:22:30 +02:00
parent c4bfc8ea72
commit b8add74fc1
6 changed files with 19 additions and 12 deletions

View File

@@ -25,7 +25,8 @@ import java.net.URI;
public interface LinkBuilder {
/**
* Adds the given object's {@link String} representation as sub-resource to the current URI.
* Adds the given object's {@link String} representation as sub-resource to the current URI. Will unwrap
* {@link Identifiable}s to their id value (see {@link Identifiable#getId()}).
*
* @param object
* @return

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.mvc;
package org.springframework.hateoas.core;
import java.net.URI;
@@ -31,16 +31,16 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Ricardo Gladwell
* @author Oliver Gierke
*/
public abstract class UriComponentsLinkBuilder<T extends LinkBuilder> implements LinkBuilder {
public abstract class LinkBuilderSupport<T extends LinkBuilder> implements LinkBuilder {
private final UriComponents uriComponents;
/**
* Creates a new {@link UriComponentsLinkBuilder} using the given {@link UriComponentsBuilder}.
* Creates a new {@link LinkBuilderSupport} using the given {@link UriComponentsBuilder}.
*
* @param builder must not be {@literal null}.
*/
public UriComponentsLinkBuilder(UriComponentsBuilder builder) {
public LinkBuilderSupport(UriComponentsBuilder builder) {
Assert.notNull(builder);
this.uriComponents = builder.build();
@@ -56,6 +56,10 @@ public abstract class UriComponentsLinkBuilder<T extends LinkBuilder> implements
return getThis();
}
if (object instanceof Identifiable) {
return slash((Identifiable<?>) object);
}
String[] segments = StringUtils.tokenizeToStringArray(object.toString(), "/");
return createNewInstance(UriComponentsBuilder.fromUri(uriComponents.toUri()).pathSegment(segments));
}
@@ -64,10 +68,10 @@ public abstract class UriComponentsLinkBuilder<T extends LinkBuilder> implements
* (non-Javadoc)
* @see org.springframework.hateoas.LinkBuilder#slash(org.springframework.hateoas.Identifiable)
*/
public LinkBuilder slash(Identifiable<?> identifyable) {
public T slash(Identifiable<?> identifyable) {
if (identifyable == null) {
return this;
return getThis();
}
return slash(identifyable.getId());

View File

@@ -19,7 +19,7 @@ import javax.ws.rs.Path;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.mvc.UriComponentsLinkBuilder;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
@@ -29,7 +29,7 @@ import org.springframework.web.util.UriTemplate;
*
* @author Oliver Gierke
*/
public class JaxRsLinkBuilder extends UriComponentsLinkBuilder<JaxRsLinkBuilder> {
public class JaxRsLinkBuilder extends LinkBuilderSupport<JaxRsLinkBuilder> {
/**
* Creates a new {@link JaxRsLinkBuilder} from the given {@link UriComponentsBuilder}.

View File

@@ -17,6 +17,7 @@ package org.springframework.hateoas.mvc;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -28,7 +29,7 @@ import org.springframework.web.util.UriTemplate;
*
* @author Oliver Gierke
*/
public class ControllerLinkBuilder extends UriComponentsLinkBuilder<ControllerLinkBuilder> {
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
/**
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.

View File

@@ -16,9 +16,10 @@
package org.springframework.hateoas.mvc;
import org.springframework.hateoas.LinkBuilderFactory;
import org.springframework.hateoas.core.LinkBuilderSupport;
/**
* Factory for {@link UriComponentsLinkBuilder} instances based on the request mapping annotated on the given
* Factory for {@link LinkBuilderSupport} instances based on the request mapping annotated on the given
* controller.
*
* @author Ricardo Gladwell