Permalink
Browse files

#13 - Introduced LinkBuilder implementation inspecting JAX-RS @Path a…

…nnotation.

Refactored LinkBuilder(Factory) classes to extract reusable code into common superclass for ControllerLinkBuilder and JaxRsLinkBuilder.
  • Loading branch information...
1 parent acdb0c8 commit 9e728b4660238f96a6ab25a4d63fe443a1c187eb @rgladwell rgladwell committed with olivergierke Sep 12, 2012
View
10 pom.xml
@@ -59,6 +59,7 @@
<properties>
<spring.version>3.1.2.RELEASE</spring.version>
<jackson.version>1.9.7</jackson.version>
+ <jaxrs.version>1.0</jaxrs.version>
<bundlor.failOnWarnings>true</bundlor.failOnWarnings>
</properties>
@@ -88,7 +89,14 @@
<version>${jackson.version}</version>
<optional>true</optional>
</dependency>
-
+
+ <dependency>
+ <groupId>javax.ws.rs</groupId>
+ <artifactId>jsr311-api</artifactId>
+ <version>${jaxrs.version}</version>
+ <optional>true</optional>
+ </dependency>
+
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
@@ -20,7 +20,7 @@
*
* @author Ricardo Gladwell
*/
-public interface LinkBuilderFactory {
+public interface LinkBuilderFactory<T extends LinkBuilder> {
/**
* Creates a new {@link LinkBuilder} with a base of the mapping annotated to the given target clas (controller,
@@ -29,7 +29,7 @@
* @param target must not be {@literal null}.
* @return
*/
- LinkBuilder linkTo(Class<?> target);
+ T linkTo(Class<?> target);
/**
* Creates a new {@link LinkBuilder} with a base of the mapping annotated to the given target class (controller,
@@ -40,5 +40,5 @@
* @param parameters must not be {@literal null}.
* @return
*/
- LinkBuilder linkTo(Class<?> target, Object... parameters);
+ T linkTo(Class<?> target, Object... parameters);
}
@@ -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);
+ }
+}
@@ -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);
+ }
+}
@@ -15,17 +15,11 @@
*/
package org.springframework.hateoas.mvc;
-import java.net.URI;
-
import org.springframework.core.annotation.AnnotationUtils;
-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.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
-import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
@@ -34,24 +28,21 @@
*
* @author Oliver Gierke
*/
-public class ControllerLinkBuilder implements LinkBuilder {
-
- private final UriComponents uriComponents;
+public class ControllerLinkBuilder extends UriComponentsLinkBuilder<ControllerLinkBuilder> {
/**
- * Creates a new {@link ControllerLinkBuilder}.
+ * Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
*
- * @param uriComponents must not be {@literal null}.
+ * @param builder must not be {@literal null}.
*/
private ControllerLinkBuilder(UriComponentsBuilder builder) {
- Assert.notNull(builder);
- this.uriComponents = builder.build();
+ super(builder);
}
/**
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
*
- * @param controller must not be {@literal null}.
+ * @param controller the class to discover the annotation on, must not be {@literal null}.
* @return
*/
public static ControllerLinkBuilder linkTo(Class<?> controller) {
@@ -62,8 +53,9 @@ public static ControllerLinkBuilder linkTo(Class<?> controller) {
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class. The
* additional parameters are used to fill up potentially available path variables in the class scop request mapping.
*
- * @param controller must not be {@literal null}.
- * @param parameters
+ * @param controller 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 ControllerLinkBuilder linkTo(Class<?> controller, Object... parameters) {
@@ -87,63 +79,21 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parame
return builder.slash(template.expand(parameters));
}
- /*
+ /*
* (non-Javadoc)
- * @see org.springframework.hateoas.LinkBuilder#slash(java.lang.Object)
+ * @see org.springframework.hateoas.UriComponentsLinkBuilder#getThis()
*/
- public ControllerLinkBuilder slash(Object object) {
-
- if (object == null) {
- return this;
- }
-
- String[] segments = StringUtils.tokenizeToStringArray(object.toString(), "/");
- return new ControllerLinkBuilder(UriComponentsBuilder.fromUri(uriComponents.toUri()).pathSegment(segments));
- }
-
- /*
- * (non-Javadoc)
- * @see org.springframework.hateoas.LinkBuilder#slash(org.springframework.hateoas.Identifiable)
- */
- public ControllerLinkBuilder 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());
+ @Override
+ protected ControllerLinkBuilder getThis() {
+ return this;
}
- /*
+ /*
* (non-Javadoc)
- * @see java.lang.Object#toString()
+ * @see org.springframework.hateoas.UriComponentsLinkBuilder#createNewInstance(org.springframework.web.util.UriComponentsBuilder)
*/
@Override
- public String toString() {
- return toUri().normalize().toASCIIString();
+ protected ControllerLinkBuilder createNewInstance(UriComponentsBuilder builder) {
+ return new ControllerLinkBuilder(builder);
}
}
@@ -18,12 +18,13 @@
import org.springframework.hateoas.LinkBuilderFactory;
/**
- * Factory for {@link ControllerLinkBuilder} instances.
+ * Factory for {@link UriComponentsLinkBuilder} instances based on the request mapping annotated on the given
+ * controller.
*
* @author Ricardo Gladwell
* @author Oliver Gierke
*/
-public class ControllerLinkBuilderFactory implements LinkBuilderFactory {
+public class ControllerLinkBuilderFactory implements LinkBuilderFactory<ControllerLinkBuilder> {
/*
* (non-Javadoc)
Oops, something went wrong.

0 comments on commit 9e728b4

Please sign in to comment.