1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth.config;
18
19 import java.util.List;
20
21 import org.springframework.beans.BeanMetadataElement;
22 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23 import org.springframework.beans.factory.support.ManagedMap;
24 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
25 import org.springframework.beans.factory.xml.ParserContext;
26 import org.springframework.security.oauth.provider.InMemoryConsumerDetailsService;
27 import org.springframework.util.StringUtils;
28 import org.springframework.util.xml.DomUtils;
29 import org.w3c.dom.Element;
30
31
32
33
34
35
36 public class ConsumerServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
37
38 @Override
39 protected Class<?> getBeanClass(Element element) {
40 return InMemoryConsumerDetailsService.class;
41 }
42
43 @Override
44 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
45 List<Element> consumerElements = DomUtils.getChildElementsByTagName(element, "consumer");
46 ManagedMap<String, BeanMetadataElement> consumers = new ManagedMap<String, BeanMetadataElement>();
47 for (Object item : consumerElements) {
48
49 BeanDefinitionBuilder consumer = BeanDefinitionBuilder
50 .genericBeanDefinition(ConsumerDetailsFactoryBean.class);
51 Element consumerElement = (Element) item;
52 String key = consumerElement.getAttribute("key");
53 if (StringUtils.hasText(key)) {
54 consumer.addPropertyValue("consumerKey", key);
55 }
56 else {
57 parserContext.getReaderContext().error(
58 "A consumer key must be supplied with the definition of a consumer.", consumerElement);
59 }
60
61 String secret = consumerElement.getAttribute("secret");
62 if (StringUtils.hasText(secret)) {
63 consumer.addPropertyValue("secret", secret);
64 String typeOfSecret = consumerElement.getAttribute("typeOfSecret");
65 consumer.addPropertyValue("typeOfSecret", typeOfSecret);
66 }
67 else {
68 parserContext.getReaderContext().error(
69 "A consumer secret must be supplied with the definition of a consumer.", consumerElement);
70 }
71
72 String name = consumerElement.getAttribute("name");
73 if (StringUtils.hasText(name)) {
74 consumer.addPropertyValue("consumerName", name);
75 }
76
77 String authorities = consumerElement.getAttribute("authorities");
78 if (StringUtils.hasText(authorities)) {
79 consumer.addPropertyValue("authorities", authorities);
80 }
81
82 String resourceName = consumerElement.getAttribute("resourceName");
83 if (StringUtils.hasText(resourceName)) {
84 consumer.addPropertyValue("resourceName", resourceName);
85 }
86
87 String resourceDescription = consumerElement.getAttribute("resourceDescription");
88 if (StringUtils.hasText(resourceDescription)) {
89 consumer.addPropertyValue("resourceDescription", resourceDescription);
90 }
91
92 String requiredToObtainAuthenticatedToken = consumerElement
93 .getAttribute("requiredToObtainAuthenticatedToken");
94 if (StringUtils.hasText(requiredToObtainAuthenticatedToken)) {
95 consumer.addPropertyValue("requiredToObtainAuthenticatedToken", requiredToObtainAuthenticatedToken);
96 }
97
98 consumers.put(key, consumer.getBeanDefinition());
99 }
100
101 builder.addPropertyValue("consumerDetailsStore", consumers);
102 }
103 }