View Javadoc
1   /*
2    * Copyright 2008-2009 Web Cohesion
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  
14  package org.springframework.security.oauth2.config.xml;
15  
16  import org.springframework.beans.factory.support.AbstractBeanDefinition;
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
19  import org.springframework.beans.factory.xml.ParserContext;
20  import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
21  import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
22  import org.springframework.util.StringUtils;
23  import org.w3c.dom.Element;
24  
25  /**
26   * Parser for the OAuth "provider" element.
27   *
28   * @author Ryan Heaton
29   * @author Dave Syer
30   */
31  public abstract class ProviderBeanDefinitionParser extends AbstractBeanDefinitionParser {
32  
33  	@Override
34  	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
35  
36  		String tokenServicesRef = element.getAttribute("token-services-ref");
37  		String serializerRef = element.getAttribute("serialization-service-ref");
38  
39  		if (!StringUtils.hasText(tokenServicesRef)) {
40  			tokenServicesRef = "oauth2TokenServices";
41  			BeanDefinitionBuilder tokenServices = BeanDefinitionBuilder.rootBeanDefinition(DefaultTokenServices.class);
42  			AbstractBeanDefinition tokenStore = BeanDefinitionBuilder.rootBeanDefinition(InMemoryTokenStore.class).getBeanDefinition();
43  			tokenServices.addPropertyValue("tokenStore", tokenStore);
44  			parserContext.getRegistry().registerBeanDefinition(tokenServicesRef, tokenServices.getBeanDefinition());
45  		}
46  
47  		return parseEndpointAndReturnFilter(element, parserContext, tokenServicesRef, serializerRef);
48  	}
49  
50  	protected abstract AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, ParserContext parserContext,
51  			String tokenServicesRef, String serializerRef);
52  
53  }