Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

SOCIAL-293: Added support for Java configuration of Twitter connectio…

…n factory and API binding beans.
  • Loading branch information...
commit b496d7e40bec2e8683f0d550a819fa964e0f18f3 1 parent 3c55a76
Craig Walls authored
View
54 ...ter/src/main/java/org/springframework/social/twitter/config/annotation/EnableTwitter.java
@@ -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.twitter.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.twitter.api.Twitter;
+import org.springframework.social.twitter.connect.TwitterConnectionFactory;
+
+/**
+ * Annotation to enable Twitter in a Spring Social application.
+ * Configures a {@link TwitterConnectionFactory} bean (and a {@link ConnectionFactoryLocator} bean if one isn't already registered).
+ * Also configures a request-scoped {@link Twitter} bean fetched from the current user's {@link ConnectionRepository}.
+ * @author Craig Walls
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+@Import(TwitterProviderConfigRegistrar.class)
+public @interface EnableTwitter {
+
+ /**
+ * The application's consumer key as issued by Twitter.
+ */
+ String appId();
+
+ /**
+ * The application's consumer secret as issued by Twitter.
+ */
+ String appSecret();
+
+}
View
67 .../org/springframework/social/twitter/config/annotation/TwitterProviderConfigRegistrar.java
@@ -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.twitter.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.twitter.api.Twitter;
+import org.springframework.social.twitter.api.impl.TwitterTemplate;
+import org.springframework.social.twitter.connect.TwitterConnectionFactory;
+
+/**
+ * {@link ImportBeanDefinitionRegistrar} for configuring a {@link TwitterConnectionFactory} bean and a request-scoped {@link Twitter} bean.
+ * @author Craig Walls
+ */
+public class TwitterProviderConfigRegistrar extends ProviderConfigRegistrarSupport {
+
+ public TwitterProviderConfigRegistrar() {
+ super(EnableTwitter.class, TwitterConnectionFactory.class, TwitterApiHelper.class);
+ }
+
+ static class TwitterApiHelper implements ApiHelper<Twitter> {
+
+ private final UsersConnectionRepository usersConnectionRepository;
+
+ private final UserIdSource userIdSource;
+
+ private TwitterApiHelper(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
+ this.usersConnectionRepository = usersConnectionRepository;
+ this.userIdSource = userIdSource;
+ }
+
+ public Twitter getApi() {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Getting API binding instance for Twitter provider");
+ }
+
+ Connection<Twitter> connection = usersConnectionRepository.createConnectionRepository(userIdSource.getUserId()).findPrimaryConnection(Twitter.class);
+ if (logger.isDebugEnabled() && connection == null) {
+ logger.debug("No current connection; Returning default TwitterTemplate instance.");
+ }
+ return connection != null ? connection.getApi() : new TwitterTemplate();
+ }
+
+ private final static Log logger = LogFactory.getLog(TwitterApiHelper.class);
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.