Skip to content

Commit f5a6441

Browse files
committed
Added provider interface for relation types by entity type.
1 parent 2c49f39 commit f5a6441

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas;
17+
18+
/**
19+
* @author Oliver Gierke
20+
*/
21+
public interface RelProvider {
22+
23+
String getRelForCollectionResource(Class<?> type);
24+
25+
String getRelForSingleResource(Class<?> type);
26+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mvc;
17+
18+
import org.springframework.core.annotation.AnnotationUtils;
19+
import org.springframework.hateoas.ExposesResourceFor;
20+
import org.springframework.hateoas.RelProvider;
21+
import org.springframework.util.Assert;
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* @author Oliver Gierke
26+
*/
27+
public class ControllerRelProvider implements RelProvider {
28+
29+
private final Class<?> entityType;
30+
private final String collectionResourceRel;
31+
private final String singleResourceRel;
32+
33+
public ControllerRelProvider(Class<?> controller) {
34+
35+
ExposesResourceFor annotation = AnnotationUtils.findAnnotation(controller, ExposesResourceFor.class);
36+
Assert.notNull(annotation);
37+
38+
this.entityType = annotation.value();
39+
this.singleResourceRel = StringUtils.uncapitalize(entityType.getSimpleName());
40+
this.collectionResourceRel = singleResourceRel + "List";
41+
}
42+
43+
/*
44+
* (non-Javadoc)
45+
* @see org.springframework.hateoas.RelProvider#getRelForCollectionResource(java.lang.Class)
46+
*/
47+
@Override
48+
public String getRelForCollectionResource(Class<?> type) {
49+
return collectionResourceRel;
50+
}
51+
52+
/*
53+
* (non-Javadoc)
54+
* @see org.springframework.hateoas.RelProvider#getRelForSingleResource(java.lang.Class)
55+
*/
56+
@Override
57+
public String getRelForSingleResource(Class<?> type) {
58+
return singleResourceRel;
59+
}
60+
}

0 commit comments

Comments
 (0)