This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOCIAL-293: Added support for Java configuration of Facebook connecti…
…on factory and API binding beans.
- Loading branch information
Showing
3 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...k/src/main/java/org/springframework/social/facebook/config/annotation/EnableFacebook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.social.facebook.config.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.context.annotation.Import; | ||
import org.springframework.social.connect.ConnectionFactoryLocator; | ||
import org.springframework.social.connect.ConnectionRepository; | ||
import org.springframework.social.facebook.api.Facebook; | ||
import org.springframework.social.facebook.connect.FacebookConnectionFactory; | ||
|
||
/** | ||
* Annotation to enable Facebook in a Spring Social application. | ||
* Configures a {@link FacebookConnectionFactory} bean (and a {@link ConnectionFactoryLocator} bean if one isn't already registered). | ||
* Also configures a request-scoped {@link Facebook} bean fetched from the current user's {@link ConnectionRepository}. | ||
* @author Craig Walls | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@Import(FacebookProviderConfigRegistrar.class) | ||
public @interface EnableFacebook { | ||
|
||
/** | ||
* The application's App ID as issued by Facebook. | ||
*/ | ||
String appId(); | ||
|
||
/** | ||
* The application's App Secret as issued by Facebook. | ||
*/ | ||
String appSecret(); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...rg/springframework/social/facebook/config/annotation/FacebookProviderConfigRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.social.facebook.config.annotation; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.social.config.annotation.ProviderConfigRegistrarSupport; | ||
import org.springframework.social.config.xml.ApiHelper; | ||
import org.springframework.social.config.xml.UserIdSource; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.UsersConnectionRepository; | ||
import org.springframework.social.facebook.api.Facebook; | ||
import org.springframework.social.facebook.api.impl.FacebookTemplate; | ||
import org.springframework.social.facebook.connect.FacebookConnectionFactory; | ||
|
||
/** | ||
* {@link ImportBeanDefinitionRegistrar} for configuring a {@link FacebookConnectionFactory} bean and a request-scoped {@link Facebook} bean. | ||
* @author Craig Walls | ||
*/ | ||
public class FacebookProviderConfigRegistrar extends ProviderConfigRegistrarSupport { | ||
|
||
public FacebookProviderConfigRegistrar() { | ||
super(EnableFacebook.class, FacebookConnectionFactory.class, FacebookApiHelper.class); | ||
} | ||
|
||
static class FacebookApiHelper implements ApiHelper<Facebook> { | ||
|
||
private final UsersConnectionRepository usersConnectionRepository; | ||
|
||
private final UserIdSource userIdSource; | ||
|
||
private FacebookApiHelper(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) { | ||
this.usersConnectionRepository = usersConnectionRepository; | ||
this.userIdSource = userIdSource; | ||
} | ||
|
||
public Facebook getApi() { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Getting API binding instance for Facebook provider"); | ||
} | ||
|
||
Connection<Facebook> connection = usersConnectionRepository.createConnectionRepository(userIdSource.getUserId()).findPrimaryConnection(Facebook.class); | ||
if (logger.isDebugEnabled() && connection == null) { | ||
logger.debug("No current connection; Returning default FacebookTemplate instance."); | ||
} | ||
return connection != null ? connection.getApi() : new FacebookTemplate(); | ||
} | ||
|
||
private final static Log logger = LogFactory.getLog(FacebookApiHelper.class); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters