View Javadoc
1   /*
2    * Copyright 2002-2011 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    *      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  package org.springframework.security.oauth2.provider.endpoint;
17  
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.beans.factory.InitializingBean;
22  import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
23  import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
24  import org.springframework.security.oauth2.provider.ClientDetailsService;
25  import org.springframework.security.oauth2.provider.TokenGranter;
26  import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
27  import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
28  import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
29  import org.springframework.util.Assert;
30  
31  /**
32   * @author Dave Syer
33   * 
34   */
35  public class AbstractEndpoint implements InitializingBean {
36  
37  	protected final Log logger = LogFactory.getLog(getClass());
38  
39  	private WebResponseExceptionTranslator<OAuth2Exception> providerExceptionHandler = new DefaultWebResponseExceptionTranslator();
40  
41  	private TokenGranter tokenGranter;
42  
43  	private ClientDetailsService clientDetailsService;
44  
45  	private OAuth2RequestFactory oAuth2RequestFactory;
46  
47  	private OAuth2RequestFactory defaultOAuth2RequestFactory;
48  
49  	public void afterPropertiesSet() throws Exception {
50  		Assert.state(tokenGranter != null, "TokenGranter must be provided");
51  		Assert.state(clientDetailsService != null, "ClientDetailsService must be provided");
52  		defaultOAuth2RequestFactory = new DefaultOAuth2RequestFactory(getClientDetailsService());
53  		if (oAuth2RequestFactory == null) {
54  			oAuth2RequestFactory = defaultOAuth2RequestFactory;
55  		}
56  	}
57  
58  	public void setProviderExceptionHandler(WebResponseExceptionTranslator<OAuth2Exception> providerExceptionHandler) {
59  		this.providerExceptionHandler = providerExceptionHandler;
60  	}
61  
62  	public void setTokenGranter(TokenGranter tokenGranter) {
63  		this.tokenGranter = tokenGranter;
64  	}
65  
66  	protected TokenGranter getTokenGranter() {
67  		return tokenGranter;
68  	}
69  
70  	protected WebResponseExceptionTranslator<OAuth2Exception> getExceptionTranslator() {
71  		return providerExceptionHandler;
72  	}
73  
74  	protected OAuth2RequestFactory getOAuth2RequestFactory() {
75  		return oAuth2RequestFactory;
76  	}
77  
78  	protected OAuth2RequestFactory getDefaultOAuth2RequestFactory() {
79  		return defaultOAuth2RequestFactory;
80  	}
81  
82  	public void setOAuth2RequestFactory(OAuth2RequestFactory oAuth2RequestFactory) {
83  		this.oAuth2RequestFactory = oAuth2RequestFactory;
84  	}
85  
86  	protected ClientDetailsService getClientDetailsService() {
87  		return clientDetailsService;
88  	}
89  
90  	public void setClientDetailsService(ClientDetailsService clientDetailsService) {
91  		this.clientDetailsService = clientDetailsService;
92  	}
93  
94  }