/*
* Copyright 2012-2016 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 java.util.Map;
import javax.ws.rs.Path;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.hateoas.core.MappingDiscoverer;
import org.springframework.util.Assert;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
/**
* {@link LinkBuilder} to derive URI mappings from a JAX-RS {@link Path} annotation.
*
* @author Oliver Gierke
* @author Kamill Sokol
* @author Andrew Naydyonock
*/
public class JaxRsLinkBuilder extends LinkBuilderSupport<JaxRsLinkBuilder> {
private static final MappingDiscoverer DISCOVERER = new AnnotationMappingDiscoverer(Path.class);
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
/**
* 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 resourceType 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<?> resourceType, Object... parameters) {
Assert.notNull(resourceType, "Controller type must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(resourceType));
UriComponents expandedComponents = HANDLER.expandAndEncode(builder, parameters);
return new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping())//
.slash(expandedComponents, true);
}
/**
* 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 resourceType the class to discover the annotation on, must not be {@literal null}.
* @param parameters map of additional parameters to bind to the URI template declared in the annotation, must not be
* {@literal null}.
* @return
*/
public static JaxRsLinkBuilder linkTo(Class<?> resourceType, Map<String, ?> parameters) {
Assert.notNull(resourceType, "Controller type must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(resourceType));
UriComponents expandedComponents = HANDLER.expandAndEncode(builder, parameters);
return new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping())//
.slash(expandedComponents, true);
}
/*
* (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);
}
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
public CustomUriTemplateHandler() {
setStrictEncoding(true);
}
/*
* (non-Javadoc)
* @see org.springframework.web.util.DefaultUriTemplateHandler#expandAndEncode(org.springframework.web.util.UriComponentsBuilder, java.util.Map)
*/
@Override
public UriComponents expandAndEncode(UriComponentsBuilder builder, Map<String, ?> uriVariables) {
return super.expandAndEncode(builder, uriVariables);
}
/*
* (non-Javadoc)
* @see org.springframework.web.util.DefaultUriTemplateHandler#expandAndEncode(org.springframework.web.util.UriComponentsBuilder, java.lang.Object[])
*/
@Override
public UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriVariables) {
return super.expandAndEncode(builder, uriVariables);
}
}
}