#13 - Introduced LinkBuilder implementation inspecting JAX-RS @Path annotation.

Refactored LinkBuilder(Factory) classes to extract reusable code into common superclass for ControllerLinkBuilder and JaxRsLinkBuilder.
This commit is contained in:
Ricardo Gladwell
2012-09-12 20:58:06 +01:00
committed by Oliver Gierke
parent acdb0c8a5b
commit 9e728b4660
12 changed files with 353 additions and 81 deletions

View File

@@ -0,0 +1,90 @@
/*
* 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.jaxrs;
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.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
/**
* {@link LinkBuilder} to derive URI mappings from a JAX-RS {@link Path} annotation.
*
* @author Oliver Gierke
*/
public class JaxRsLinkBuilder extends UriComponentsLinkBuilder<JaxRsLinkBuilder> {
/**
* Creates a new {@link JaxRsLinkBuilder} from the given {@link UriComponentsBuilder}.
*
* @param builder must not be {@literal null}.
*/
private JaxRsLinkBuilder(UriComponentsBuilder builder) {
super(builder);
}
/**
* Creates a {@link JaxRsLinkBuilder} instance to link to the {@link Path} mapping tied to the given class.
*
* @param service the class to discover the annotation on, must not be {@literal null}.
* @return
*/
public static JaxRsLinkBuilder linkTo(Class<?> service) {
return linkTo(service, new Object[0]);
}
/**
* Creates a new {@link JaxRsLinkBuilder} instance to link to the {@link Path} mapping tied to the given class binding
* the given parameters to the URI template.
*
* @param service the class to discover the annotation on, must not be {@literal null}.
* @param parameters additional parameters to bind to the URI template declared in the annotation, must not be
* {@literal null}.
* @return
*/
public static JaxRsLinkBuilder linkTo(Class<?> service, Object... parameters) {
Path annotation = AnnotationUtils.findAnnotation(service, Path.class);
String path = (String) AnnotationUtils.getValue(annotation);
JaxRsLinkBuilder builder = new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping());
UriTemplate template = new UriTemplate(path);
return builder.slash(template.expand(parameters));
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.UriComponentsLinkBuilder#getThis()
*/
@Override
protected JaxRsLinkBuilder getThis() {
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.UriComponentsLinkBuilder#createNewInstance(org.springframework.web.util.UriComponentsBuilder)
*/
@Override
protected JaxRsLinkBuilder createNewInstance(UriComponentsBuilder builder) {
return new JaxRsLinkBuilder(builder);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.jaxrs;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.LinkBuilderFactory;
/**
* Factory for {@link LinkBuilder} instances based on the path mapping annotated on the given JAX-RS service.
*
* @author Ricardo Gladwell
* @author Oliver Gierke
*/
public class JaxRsLinkBuilderFactory implements LinkBuilderFactory<JaxRsLinkBuilder> {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.LinkBuilderFactory#linkTo(java.lang.Class)
*/
public JaxRsLinkBuilder linkTo(Class<?> service) {
return JaxRsLinkBuilder.linkTo(service);
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.LinkBuilderFactory#linkTo(java.lang.Class, java.lang.Object[])
*/
@Override
public JaxRsLinkBuilder linkTo(Class<?> service, Object... parameters) {
return JaxRsLinkBuilder.linkTo(service, parameters);
}
}