View Javadoc
1   /*
2    * Copyright 2008-2009 Web Cohesion
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    *   https://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  
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   * @author Ryan Heaton
33   * @author Andrew McCall
34   * @author Dave Syer
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 }