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:
@@ -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}.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 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.mvc;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Base class to implement {@link LinkBuilder}s based on a Spring MVC {@link UriComponentsBuilder}.
|
||||
*
|
||||
* @author Ricardo Gladwell
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class UriComponentsLinkBuilder<T extends LinkBuilder> implements LinkBuilder {
|
||||
|
||||
private final UriComponents uriComponents;
|
||||
|
||||
/**
|
||||
* Creates a new {@link UriComponentsLinkBuilder} using the given {@link UriComponentsBuilder}.
|
||||
*
|
||||
* @param builder must not be {@literal null}.
|
||||
*/
|
||||
public UriComponentsLinkBuilder(UriComponentsBuilder builder) {
|
||||
|
||||
Assert.notNull(builder);
|
||||
this.uriComponents = builder.build();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#slash(java.lang.Object)
|
||||
*/
|
||||
public T slash(Object object) {
|
||||
|
||||
if (object == null) {
|
||||
return getThis();
|
||||
}
|
||||
|
||||
String[] segments = StringUtils.tokenizeToStringArray(object.toString(), "/");
|
||||
return createNewInstance(UriComponentsBuilder.fromUri(uriComponents.toUri()).pathSegment(segments));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#slash(org.springframework.hateoas.Identifiable)
|
||||
*/
|
||||
public LinkBuilder slash(Identifiable<?> identifyable) {
|
||||
|
||||
if (identifyable == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return slash(identifyable.getId());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#toUri()
|
||||
*/
|
||||
public URI toUri() {
|
||||
return uriComponents.encode().toUri();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#withRel(java.lang.String)
|
||||
*/
|
||||
public Link withRel(String rel) {
|
||||
return new Link(this.toString(), rel);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#withSelfRel()
|
||||
*/
|
||||
public Link withSelfRel() {
|
||||
return new Link(this.toString());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return toUri().normalize().toASCIIString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current concrete instance.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract T getThis();
|
||||
|
||||
/**
|
||||
* Creates a new instance of the sub-class.
|
||||
*
|
||||
* @param builder will never be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
protected abstract T createNewInstance(UriComponentsBuilder builder);
|
||||
}
|
||||
Reference in New Issue
Block a user