Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hal 47: additional hal-support for spring-hateoas #60

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
test-output/
.settings/
.project
.classpath
26 changes: 26 additions & 0 deletions src/main/java/org/springframework/hateoas/RelProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2013 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;

/**
* @author Oliver Gierke
*/
public interface RelProvider {

String getRelForCollectionResource(Object type);

String getRelForSingleResource(Object type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package org.springframework.hateoas.config;

import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.*;
import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerBeanDefinition;
import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerWithGeneratedName;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -103,12 +104,12 @@ private AbstractBeanDefinition getLinkDiscovererBeanDefinition(HypermediaType ty
AbstractBeanDefinition definition;

switch (type) {
case HAL:
definition = new RootBeanDefinition(HalLinkDiscoverer.class);
break;
case DEFAULT:
default:
definition = new RootBeanDefinition(DefaultLinkDiscoverer.class);
case HAL:
definition = new RootBeanDefinition(HalLinkDiscoverer.class);
break;
case DEFAULT:
default:
definition = new RootBeanDefinition(DefaultLinkDiscoverer.class);
}

definition.setSource(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2013 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.hal;

import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;

/**
* @author Oliver Gierke
*/
public class AnnotationRelProvider implements RelProvider {

/*
* (non-Javadoc)
* @see org.springframework.hateoas.RelProvider#getRelForCollectionResource(java.lang.Object)
*/
@Override
public String getRelForCollectionResource(Object resource) {
// check for hateoas wrapper type
if (Resource.class.isInstance(resource)) {
resource = ((Resource) resource).getContent();
}

HalRelation annotation = resource.getClass().getAnnotation(HalRelation.class);
if (annotation == null || HalRelation.NO_RELATION.equals(annotation.collectionRelation())) {
return null;
}
return annotation.value();
}

/*
* (non-Javadoc)
* @see org.springframework.hateoas.RelProvider#getRelForSingleResource(java.lang.Object)
*/
@Override
public String getRelForSingleResource(Object resource) {
// check for hateoas wrapper type
if (Resource.class.isInstance(resource)) {
resource = ((Resource) resource).getContent();
}

HalRelation annotation = resource.getClass().getAnnotation(HalRelation.class);
if (annotation == null || HalRelation.NO_RELATION.equals(annotation.value())) {
return null;
}
return annotation.value();
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/springframework/hateoas/hal/HalRelation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.springframework.hateoas.hal;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface HalRelation {

public static final String NO_RELATION = "";

String value() default NO_RELATION;

String collectionRelation() default NO_RELATION;

}
Loading