diff --git a/etc/migrate-to-1.0.sh b/etc/migrate-to-1.0.sh index acf92a87..a303577d 100755 --- a/etc/migrate-to-1.0.sh +++ b/etc/migrate-to-1.0.sh @@ -66,6 +66,7 @@ s/hateoas\.core\.Relation/hateoas.server.core.Relation/g;\ \ s/hateoas\.hal/hateoas.mediatype.hal/g;\ \ +s/hateoas\.mvc\.BasicLinkBuilder/hateoas.server.mvc.BasicLinkBuilder/g;\ s/hateoas\.mvc\.ControllerLinkBuilder/hateoas.server.mvc.WebMvcLinkBuilder/g;\ s/hateoas\.mvc\.ControllerLinkBuilderFactory/hateoas.server.mvc.WebMvcLinkBuilderFactory/g;\ s/hateoas\.mvc\.HeaderLinksResponseEntity/hateoas.server.core.HeaderLinksResponseEntity/g;\ diff --git a/src/main/java/org/springframework/hateoas/server/mvc/BasicLinkBuilder.java b/src/main/java/org/springframework/hateoas/server/mvc/BasicLinkBuilder.java new file mode 100644 index 00000000..ab6176c9 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/server/mvc/BasicLinkBuilder.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2019 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.server.mvc; + +import java.util.List; + +import org.springframework.hateoas.Affordance; +import org.springframework.hateoas.server.LinkBuilder; +import org.springframework.hateoas.server.core.LinkBuilderSupport; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponentsBuilder; + +/** + * Simples {@link LinkBuilder} implementation possible. Exposes a link to the current servlet mapping only. + * + * @author Oliver Gierke + */ +public class BasicLinkBuilder extends LinkBuilderSupport { + + /** + * Creates a new {@link BasicLinkBuilder} using the given {@link UriComponentsBuilder}. + * + * @param builder must not be {@literal null}. + */ + private BasicLinkBuilder(UriComponentsBuilder builder) { + super(builder); + } + + private BasicLinkBuilder(UriComponentsBuilder builder, List affordances) { + super(builder, affordances); + } + + /** + * Creates a new {@link BasicLinkBuilder} to link to the current servlet mapping. + * + * @return + */ + public static BasicLinkBuilder linkToCurrentMapping() { + return new BasicLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()); + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.server.core.LinkBuilderSupport#createNewInstance(org.springframework.web.util.UriComponentsBuilder, java.util.List) + */ + @Override + protected BasicLinkBuilder createNewInstance(UriComponentsBuilder builder, List affordances) { + return new BasicLinkBuilder(builder, affordances); + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.mvc.LinkBuilderSupport#getThis() + */ + @Override + protected BasicLinkBuilder getThis() { + return this; + } +}