Skip to content
This repository

Initial cut of Java-config support for Twitter connection factory and API binding. #25

Merged
merged 1 commit into from over 1 year ago

1 participant

Craig Walls
Craig Walls
Collaborator

No description provided.

Craig Walls habuma merged commit 6b5d76a into from September 14, 2012
Craig Walls habuma closed this September 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 14, 2012
SOCIAL-293: Added support for Java configuration of Twitter connectio…
…n factory and API binding beans.
b496d7e
This page is out of date. Refresh to see the latest.
54  spring-social-twitter/src/main/java/org/springframework/social/twitter/config/annotation/EnableTwitter.java
... ...
@@ -0,0 +1,54 @@
  1
+/*
  2
+ * Copyright 2012 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
+ *      http://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.social.twitter.config.annotation;
  17
+
  18
+import java.lang.annotation.Documented;
  19
+import java.lang.annotation.ElementType;
  20
+import java.lang.annotation.Inherited;
  21
+import java.lang.annotation.Retention;
  22
+import java.lang.annotation.RetentionPolicy;
  23
+import java.lang.annotation.Target;
  24
+
  25
+import org.springframework.context.annotation.Import;
  26
+import org.springframework.social.connect.ConnectionFactoryLocator;
  27
+import org.springframework.social.connect.ConnectionRepository;
  28
+import org.springframework.social.twitter.api.Twitter;
  29
+import org.springframework.social.twitter.connect.TwitterConnectionFactory;
  30
+
  31
+/**
  32
+ * Annotation to enable Twitter in a Spring Social application.
  33
+ * Configures a {@link TwitterConnectionFactory} bean (and a {@link ConnectionFactoryLocator} bean if one isn't already registered).
  34
+ * Also configures a request-scoped {@link Twitter} bean fetched from the current user's {@link ConnectionRepository}. 
  35
+ * @author Craig Walls
  36
+ */
  37
+@Target(ElementType.TYPE)
  38
+@Retention(RetentionPolicy.RUNTIME)
  39
+@Documented
  40
+@Inherited
  41
+@Import(TwitterProviderConfigRegistrar.class)
  42
+public @interface EnableTwitter {
  43
+
  44
+	/**
  45
+	 * The application's consumer key as issued by Twitter.
  46
+	 */
  47
+	String appId();
  48
+	
  49
+	/**
  50
+	 * The application's consumer secret as issued by Twitter.
  51
+	 */
  52
+	String appSecret();
  53
+	
  54
+}
67  ...r/src/main/java/org/springframework/social/twitter/config/annotation/TwitterProviderConfigRegistrar.java
... ...
@@ -0,0 +1,67 @@
  1
+/*
  2
+ * Copyright 2012 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
+ *      http://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.social.twitter.config.annotation;
  17
+
  18
+import org.apache.commons.logging.Log;
  19
+import org.apache.commons.logging.LogFactory;
  20
+import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
  21
+import org.springframework.social.config.annotation.ProviderConfigRegistrarSupport;
  22
+import org.springframework.social.config.xml.ApiHelper;
  23
+import org.springframework.social.config.xml.UserIdSource;
  24
+import org.springframework.social.connect.Connection;
  25
+import org.springframework.social.connect.UsersConnectionRepository;
  26
+import org.springframework.social.twitter.api.Twitter;
  27
+import org.springframework.social.twitter.api.impl.TwitterTemplate;
  28
+import org.springframework.social.twitter.connect.TwitterConnectionFactory;
  29
+
  30
+/**
  31
+ * {@link ImportBeanDefinitionRegistrar} for configuring a {@link TwitterConnectionFactory} bean and a request-scoped {@link Twitter} bean.
  32
+ * @author Craig Walls
  33
+ */
  34
+public class TwitterProviderConfigRegistrar extends ProviderConfigRegistrarSupport {
  35
+
  36
+	public TwitterProviderConfigRegistrar() {
  37
+		super(EnableTwitter.class, TwitterConnectionFactory.class, TwitterApiHelper.class);
  38
+	}
  39
+	
  40
+	static class TwitterApiHelper implements ApiHelper<Twitter> {
  41
+		
  42
+		private final UsersConnectionRepository usersConnectionRepository;
  43
+
  44
+		private final UserIdSource userIdSource;
  45
+
  46
+		private TwitterApiHelper(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
  47
+			this.usersConnectionRepository = usersConnectionRepository;
  48
+			this.userIdSource = userIdSource;		
  49
+		}
  50
+
  51
+		public Twitter getApi() {
  52
+			if (logger.isDebugEnabled()) {
  53
+				logger.debug("Getting API binding instance for Twitter provider");
  54
+			}
  55
+					
  56
+			Connection<Twitter> connection = usersConnectionRepository.createConnectionRepository(userIdSource.getUserId()).findPrimaryConnection(Twitter.class);
  57
+			if (logger.isDebugEnabled() && connection == null) {
  58
+				logger.debug("No current connection; Returning default TwitterTemplate instance.");
  59
+			}
  60
+			return connection != null ? connection.getApi() : new TwitterTemplate();
  61
+		}
  62
+		
  63
+		private final static Log logger = LogFactory.getLog(TwitterApiHelper.class);
  64
+
  65
+	}
  66
+
  67
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.