|
| 1 | +/* |
| 2 | + * Copyright 2012-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 | + */ |
1 | 16 | package org.springframework.data.rest.repository.support;
|
2 | 17 |
|
| 18 | +import static org.springframework.data.rest.repository.support.ResourceStringUtils.*; |
3 | 19 | import static org.springframework.core.annotation.AnnotationUtils.*;
|
4 | 20 | import static org.springframework.util.StringUtils.*;
|
5 | 21 |
|
|
14 | 30 |
|
15 | 31 | /**
|
16 | 32 | * Helper methods to get the default rel and path values or to use values supplied by annotations.
|
17 |
| - * |
| 33 | + * |
18 | 34 | * @author Jon Brisbin
|
| 35 | + * @author Florent Biville |
| 36 | + * @author Oliver Gierke |
19 | 37 | */
|
20 | 38 | @Deprecated
|
21 | 39 | public abstract class ResourceMappingUtils {
|
22 | 40 |
|
23 |
| - protected ResourceMappingUtils() { |
24 |
| - } |
25 |
| - |
26 |
| - public static String findRel(Class<?> type) { |
27 |
| - RestResource anno; |
28 |
| - if(null != (anno = findAnnotation(type, RestResource.class))) { |
29 |
| - if(hasText(anno.rel())) { |
30 |
| - return anno.rel(); |
31 |
| - } |
32 |
| - } |
33 |
| - |
34 |
| - return uncapitalize(type.getSimpleName().replaceAll("Repository", "")); |
35 |
| - } |
36 |
| - |
37 |
| - public static String findRel(Method method) { |
38 |
| - RestResource anno; |
39 |
| - if(null != (anno = findAnnotation(method, RestResource.class))) { |
40 |
| - if(hasText(anno.rel())) { |
41 |
| - return anno.rel(); |
42 |
| - } |
43 |
| - } |
44 |
| - |
45 |
| - return method.getName(); |
46 |
| - } |
47 |
| - |
48 |
| - public static String formatRel(RepositoryRestConfiguration config, |
49 |
| - RepositoryInformation repoInfo, |
50 |
| - PersistentProperty<?> persistentProperty) { |
51 |
| - if(null == persistentProperty) { |
52 |
| - return null; |
53 |
| - } |
54 |
| - |
55 |
| - ResourceMapping repoMapping = getResourceMapping(config, repoInfo); |
56 |
| - ResourceMapping entityMapping = getResourceMapping(config, persistentProperty.getOwner()); |
57 |
| - ResourceMapping propertyMapping = entityMapping.getResourceMappingFor(persistentProperty.getName()); |
58 |
| - |
59 |
| - return String.format("%s.%s.%s", |
60 |
| - repoMapping.getRel(), |
61 |
| - entityMapping.getRel(), |
62 |
| - (null != propertyMapping ? propertyMapping.getRel() : persistentProperty.getName())); |
63 |
| - } |
64 |
| - |
65 |
| - public static String findPath(Class<?> type) { |
66 |
| - RestResource anno; |
67 |
| - if(null != (anno = findAnnotation(type, RestResource.class))) { |
68 |
| - if(hasText(anno.path())) { |
69 |
| - return anno.path(); |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - return uncapitalize(type.getSimpleName().replaceAll("Repository", "")); |
74 |
| - } |
75 |
| - |
76 |
| - public static String findPath(Method method) { |
77 |
| - RestResource anno; |
78 |
| - if(null != (anno = findAnnotation(method, RestResource.class))) { |
79 |
| - if(hasText(anno.path())) { |
80 |
| - return anno.path(); |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - return method.getName(); |
85 |
| - } |
86 |
| - |
87 |
| - public static boolean findExported(Class<?> type) { |
88 |
| - RestResource anno; |
89 |
| - return null == (anno = findAnnotation(type, RestResource.class)) || anno.exported(); |
90 |
| - } |
91 |
| - |
92 |
| - public static boolean findExported(Method method) { |
93 |
| - RestResource anno; |
94 |
| - return null == (anno = findAnnotation(method, RestResource.class)) || anno.exported(); |
95 |
| - } |
96 |
| - |
97 |
| - public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config, |
98 |
| - RepositoryInformation repoInfo) { |
99 |
| - if(null == repoInfo) { |
100 |
| - return null; |
101 |
| - } |
102 |
| - Class<?> repoType = repoInfo.getRepositoryInterface(); |
103 |
| - ResourceMapping mapping = (null != config ? config.getResourceMappingForRepository(repoType) : null); |
104 |
| - return merge(repoType, mapping); |
105 |
| - } |
106 |
| - |
107 |
| - public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config, |
108 |
| - PersistentEntity<?, ?> persistentEntity) { |
109 |
| - if(null == persistentEntity) { |
110 |
| - return null; |
111 |
| - } |
112 |
| - Class<?> domainType = persistentEntity.getType(); |
113 |
| - ResourceMapping mapping = (null != config ? config.getResourceMappingForDomainType(domainType) : null); |
114 |
| - return merge(domainType, mapping); |
115 |
| - } |
116 |
| - |
117 |
| - public static ResourceMapping merge(Method method, ResourceMapping mapping) { |
118 |
| - ResourceMapping defaultMapping = new ResourceMapping( |
119 |
| - findRel(method), |
120 |
| - findPath(method), |
121 |
| - findExported(method) |
122 |
| - ); |
123 |
| - if(null != mapping) { |
124 |
| - return new ResourceMapping( |
125 |
| - (null != mapping.getRel() ? mapping.getRel() : defaultMapping.getRel()), |
126 |
| - (null != mapping.getPath() ? mapping.getPath() : defaultMapping.getPath()), |
127 |
| - (mapping.isExported() != defaultMapping.isExported() ? mapping.isExported() : defaultMapping.isExported()) |
128 |
| - ); |
129 |
| - } |
130 |
| - return defaultMapping; |
131 |
| - } |
132 |
| - |
133 |
| - public static ResourceMapping merge(Class<?> type, ResourceMapping mapping) { |
134 |
| - ResourceMapping defaultMapping = new ResourceMapping( |
135 |
| - findRel(type), |
136 |
| - findPath(type), |
137 |
| - findExported(type) |
138 |
| - ); |
139 |
| - if(null != mapping) { |
140 |
| - return new ResourceMapping( |
141 |
| - (null != mapping.getRel() ? mapping.getRel() : defaultMapping.getRel()), |
142 |
| - (null != mapping.getPath() ? mapping.getPath() : defaultMapping.getPath()), |
143 |
| - (mapping.isExported() != defaultMapping.isExported() ? mapping.isExported() : defaultMapping.isExported())) |
144 |
| - .addResourceMappings(mapping.getResourceMappings()); |
145 |
| - } |
146 |
| - return defaultMapping; |
147 |
| - } |
| 41 | + protected ResourceMappingUtils() {} |
| 42 | + |
| 43 | + public static String findRel(Class<?> type) { |
| 44 | + |
| 45 | + RestResource anno = findAnnotation(type, RestResource.class); |
| 46 | + if (anno != null) { |
| 47 | + if (hasText(anno.rel())) { |
| 48 | + return anno.rel(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return uncapitalize(type.getSimpleName().replaceAll("Repository", "")); |
| 53 | + } |
| 54 | + |
| 55 | + public static String findRel(Method method) { |
| 56 | + |
| 57 | + RestResource anno = findAnnotation(method, RestResource.class); |
| 58 | + |
| 59 | + if (anno != null) { |
| 60 | + if (hasText(anno.rel())) { |
| 61 | + return anno.rel(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return method.getName(); |
| 66 | + } |
| 67 | + |
| 68 | + public static String formatRel(RepositoryRestConfiguration config, RepositoryInformation repoInfo, |
| 69 | + PersistentProperty<?> persistentProperty) { |
| 70 | + |
| 71 | + if (persistentProperty == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + ResourceMapping repoMapping = getResourceMapping(config, repoInfo); |
| 76 | + ResourceMapping entityMapping = getResourceMapping(config, persistentProperty.getOwner()); |
| 77 | + ResourceMapping propertyMapping = entityMapping.getResourceMappingFor(persistentProperty.getName()); |
| 78 | + |
| 79 | + return String.format("%s.%s.%s", repoMapping.getRel(), entityMapping.getRel(), |
| 80 | + (null != propertyMapping ? propertyMapping.getRel() : persistentProperty.getName())); |
| 81 | + } |
| 82 | + |
| 83 | + public static String findPath(Class<?> type) { |
| 84 | + |
| 85 | + RestResource anno = findAnnotation(type, RestResource.class); |
| 86 | + |
| 87 | + if (anno != null) { |
| 88 | + if (hasTextExceptSlash(anno.path())) { |
| 89 | + return removeLeadingSlash(anno.path()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return uncapitalize(type.getSimpleName().replaceAll("Repository", "")); |
| 94 | + } |
| 95 | + |
| 96 | + public static String findPath(Method method) { |
| 97 | + |
| 98 | + RestResource anno = findAnnotation(method, RestResource.class); |
| 99 | + |
| 100 | + if (anno != null) { |
| 101 | + if (hasTextExceptSlash(anno.path())) { |
| 102 | + return removeLeadingSlash(anno.path()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return method.getName(); |
| 107 | + } |
| 108 | + |
| 109 | + public static boolean findExported(Class<?> type) { |
| 110 | + RestResource anno = findAnnotation(type, RestResource.class); |
| 111 | + return anno == null || anno.exported(); |
| 112 | + } |
| 113 | + |
| 114 | + public static boolean findExported(Method method) { |
| 115 | + RestResource anno = findAnnotation(method, RestResource.class); |
| 116 | + return anno == null || anno.exported(); |
| 117 | + } |
| 118 | + |
| 119 | + public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config, RepositoryInformation repoInfo) { |
| 120 | + if (null == repoInfo) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + Class<?> repoType = repoInfo.getRepositoryInterface(); |
| 124 | + ResourceMapping mapping = (null != config ? config.getResourceMappingForRepository(repoType) : null); |
| 125 | + return merge(repoType, mapping); |
| 126 | + } |
| 127 | + |
| 128 | + public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config, |
| 129 | + PersistentEntity<?, ?> persistentEntity) { |
| 130 | + if (null == persistentEntity) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + Class<?> domainType = persistentEntity.getType(); |
| 134 | + ResourceMapping mapping = (null != config ? config.getResourceMappingForDomainType(domainType) : null); |
| 135 | + return merge(domainType, mapping); |
| 136 | + } |
| 137 | + |
| 138 | + public static ResourceMapping merge(Method method, ResourceMapping mapping) { |
| 139 | + ResourceMapping defaultMapping = new ResourceMapping(findRel(method), findPath(method), findExported(method)); |
| 140 | + if (null != mapping) { |
| 141 | + return new ResourceMapping((null != mapping.getRel() ? mapping.getRel() : defaultMapping.getRel()), |
| 142 | + (null != mapping.getPath() ? mapping.getPath() : defaultMapping.getPath()), |
| 143 | + (mapping.isExported() != defaultMapping.isExported() ? mapping.isExported() : defaultMapping.isExported())); |
| 144 | + } |
| 145 | + return defaultMapping; |
| 146 | + } |
| 147 | + |
| 148 | + public static ResourceMapping merge(Class<?> type, ResourceMapping mapping) { |
| 149 | + ResourceMapping defaultMapping = new ResourceMapping(findRel(type), findPath(type), findExported(type)); |
| 150 | + if (null != mapping) { |
| 151 | + return new ResourceMapping((null != mapping.getRel() ? mapping.getRel() : defaultMapping.getRel()), |
| 152 | + (null != mapping.getPath() ? mapping.getPath() : defaultMapping.getPath()), |
| 153 | + (mapping.isExported() != defaultMapping.isExported() ? mapping.isExported() : defaultMapping.isExported())) |
| 154 | + .addResourceMappings(mapping.getResourceMappings()); |
| 155 | + } |
| 156 | + return defaultMapping; |
| 157 | + } |
148 | 158 |
|
149 | 159 | }
|
0 commit comments