Announcement Announcement Module
Collapse
No announcement yet.
Newb Problem - ConnectInterceptor pre/postConnect not being called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newb Problem - ConnectInterceptor pre/postConnect not being called

    I've got the follow class, pretty much chapter and book from the Spring Social how-to's. Anyhow, I create a Facebook connection - all goes well, the db records are created, etc. However, the connect interceptor pre and post connect methods don't seem to fire as indicated either by print/logging statements or by debug breakpoints. Our spring core is 3.1.

    Any help on this would be appreciated. It is such a simple scenario, I'm completely baffled at the moment.

    Code:
    @Configuration
    public class SocialConfig {
    	@Value("${facebook.clientId}")
    	private String facebookClientId;
    	
    	@Value("${facebook.clientSecret}")
    	private String facebookClientSecret;
    
        @Inject
        private DataSource dataSource;
    
        @Inject
        private TextEncryptor textEncryptor;
    	
    	@Bean
        public ConnectionFactoryLocator connectionFactoryLocator() {
            ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
            registry.addConnectionFactory(new FacebookConnectionFactory(facebookClientId,facebookClientSecret));
            return registry;
        }
    	
        @Bean
        @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
        public ConnectionRepository connectionRepository(){
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication == null) {
                throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
            }
            return usersConnectionRepository().createConnectionRepository(authentication.getName());
        }
    
        @Bean
        public UsersConnectionRepository usersConnectionRepository() {
            return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(), 
                textEncryptor);
        }
    
        @Bean
        public ConnectController connectController() {
        	System.out.println("creating connectController bean ");
            ConnectController controller = new ConnectController(connectionFactoryLocator(), 
                connectionRepository());
            //controller.setApplicationUrl("http://localhost:8080/sigkat/");
            controller.addInterceptor(new FacebookConnectInterceptor());
            return controller;
        }
        
        protected static class FacebookConnectInterceptor implements ConnectInterceptor<Facebook>{
    
    		@Override
    		public void postConnect(Connection<Facebook> arg0, WebRequest arg1) {
    			System.out.println("FacebookConnectInterceptor.postConnect webrequest="+arg1);			
    		}
    
    		@Override
    		public void preConnect(ConnectionFactory<Facebook> arg0,
    				MultiValueMap<String, String> arg1, WebRequest arg2) {
    			System.out.println("FacebookConnectInterceptor.preConnect MultiValueMap="+arg1);	
    		}	
        }
    }

  • #2
    So I've been debugging through this. The addInterceptor method seems to be satisfactorily adding the FacebookConnectionInterceptor to the ConnectController, but the preConnect method on ConnectController fails to find the interceptor in this block of code from ConnectController:

    Code:
    	private List<ConnectInterceptor<?>> interceptingConnectionsTo(ConnectionFactory<?> connectionFactory) {
    		Class<?> serviceType = GenericTypeResolver.resolveTypeArgument(connectionFactory.getClass(), ConnectionFactory.class);
    		List<ConnectInterceptor<?>> typedInterceptors = interceptors.get(serviceType);
    		if (typedInterceptors == null) {
    			typedInterceptors = Collections.emptyList();
    		}
    		return typedInterceptors;
    	}
    The serviceType is correctly being found as org.springframework.social.facebook.api.Facebook and on the addInterceptors, this is the key that is being used to put the interceptor into the ConnectController.interceptors map, so now I'm really stymied.

    Comment


    • #3
      I have been playing the showcase sample lately. The Facebook interceptor doesn't seem working while the twitter's one does.

      Comment


      • #4
        Well, I'm still stepping through code here. On starting the server, the FacebookConnectInterceptor gets added to the ConnectController.interceptors LinkedMultiValueMap map, but then when it comes to preConnect time, this map is empty.

        Comment


        • #5
          I did some quick testing with Spring Social Showcase this morning and confirmed that both the Twitter and Facebook interceptors are being called as they should be. This leaves me confused as to why they wouldn't work for you guys. I think you may be encountering separate issues here...

          vw729: You say it works for Twitter, but not for Facebook. That tells me that the connect interceptor code is working, but that there's probably an error of some sort while posting to Facebook. The interceptors in the showcase are designed to fail silently because it's more important that the connection be made successfully than for the message to be posted post-connect. My guess is that Facebook's giving an error of some sort and it's just being ignored. You can test this theory by putting some logging/System.out.println() messages in each of the interceptor's catch blocks.

          robatsu: Your problem seems more serious. Why would the map of interceptors be cleared out by the time you get to the preConnect()? I'll look closer at your config, but is there anything else you can tell me about your app that might be unique? (For example, are you deploying it to Google App Engine?)

          Comment


          • #6
            Sorry, the facebook interceptor is running. I messed up the usage.
            Last edited by vw729; Mar 15th, 2012, 12:33 PM.

            Comment


            • #7
              Hi,

              I have the same problem that the Interceptor#preConnect and postConnect method isn't invoked.
              My configuration:

              HTML Code:
              <bean class="org.springframework.social.connect.web.ConnectController">
                          <constructor-arg ref="connectionFactoryLocator" />
                          <constructor-arg ref="connectionRepository" />
                        <!--  <property name="applicationUrl" value="http://localhost:8080/JSPTest/home.htm" />-->
                          <property name="interceptors">
                              <list>
                                  <bean class="package.name.to.interceptor.MyTwitterConnectInterceptor" />
                              </list>
                          </property>
                      </bean>
              and my interceptor:

              Code:
              import org.springframework.social.connect.Connection;
              import org.springframework.social.connect.ConnectionFactory;
              import org.springframework.social.connect.web.ConnectInterceptor;
              import org.springframework.util.MultiValueMap;
              import org.springframework.web.context.request.WebRequest;
              
              public class MyTwitterConnectInterceptor implements ConnectInterceptor {
              
                  public MyTwitterConnectInterceptor() {
                      System.out.println("لللللللللللللللللللللللللللل Object of TCI");
                      }
              
                  @Override
                  public void preConnect(ConnectionFactory cf, MultiValueMap mvm, WebRequest wr) {
                      System.out.println("----------------- preConnect");
                  }
              
                  @Override
                  public void postConnect(Connection cnctn, WebRequest wr) {
                      System.out.println("للللللللللللللللللللللللللللل postConnect");
                  }
              }
              The constructor of MyTwitterConnectInterceptor is invoked - therefore an interceptor- object exists but the both methods arn't invoked.
              Does anybody know what I'am doing wrong?

              Thanks a lot,
              Generic

              Comment


              • #8
                Hi

                Taking a look at the code you've posted, it doesn't look as if you are passing in the Twitter API type to the ConnectInterceptor interface - as I understand it you need to have:

                MyTwitterConnectInterceptor implements ConnectInterceptor<Twitter>

                That way the ConnectController can determine this is an interceptor for Twitter connections.

                Hope this helps,

                Michael

                Comment


                • #9
                  I was about to answer exactly what Michael said. A connection interceptor is associated with a given kind of connection (e.g., Twitter, Facebook, LinkedIn, etc) and so you must parameterize the interceptor when you implement ConnectInterceptor to let it know which kind of connection that the interceptor should be applied for.

                  Comment


                  • #10
                    I have a same problem. My preconnect and postconnect method isn't invoked. My interceptor is parameterized.
                    Code:
                    @Bean
                    	public ConnectController connectController() {
                    		ConnectController connectController = new ConnectController(
                    				connectionFactoryLocator(), connectionRepository());
                    		System.out.println("In Connect Controller");
                    		connectController.addInterceptor(new
                    		PostToWallAfterConnectInterceptor());
                    		return connectController;
                    	}
                    Code:
                    public class PostToWallAfterConnectInterceptor implements ConnectInterceptor<Facebook> {
                    	
                    	public PostToWallAfterConnectInterceptor(){
                    		System.out.println("In Interceptor constr");
                    	}
                    
                    	@Override
                    	public void postConnect(Connection<Facebook> arg0, WebRequest arg1) {
                    		// TODO Auto-generated method stub
                    		System.out.println("In Interceptor pre");
                    	}
                    
                    	@Override
                    	public void preConnect(ConnectionFactory<Facebook> arg0,
                    			MultiValueMap<String, String> arg1, WebRequest arg2) {
                    		// TODO Auto-generated method stub
                    		System.out.println("In Interceptor post");
                    	}
                    
                    	
                    }
                    But the constructor is being invoked. Please help

                    Comment


                    • #11
                      I see nothing out of sorts here. Sure, the constructor is being invoked, because you explicitly invoke it in the @Bean method for the ConnectController. Assuming that you're actually doing a Facebook connection, I see no apparent reason why the pre/postConnect methods wouldn't be invoked. I even matched up your code against the code in Spring Social Showcase (https://github.com/SpringSource/spri...ocial-showcase) and except for the details of each method, it looks the same.

                      A few starter questions: Did you try this with Spring Social Showcase? What versions of Spring Social (core/web/facebook) are you using? Are you certain that a connection is being created? What kind of server are you deploying this to (I ask, because I've seen trouble with interceptors working on Google App Engine)?

                      Comment


                      • #12
                        Thanks for the quick reply. Ya, I tried the spring social showcase and it works completely fine there. Then i tried a similar interceptor in my application and it fails to load. Ya the connection is being created and i used Tomact 6.0 for both the applications.

                        Comment


                        • #13
                          Well, this is puzzling. I'm not sure I can make a diagnosis on the info I have. Would you be able to share the social portion of your configuration? Not just the ConnectController, but the entire social configuration.

                          I also wonder if there may be some conflicting versions in play. ConnectController picks which interceptors to call based on the API binding interface that ConnectInterceptor is parameterized with. Sure, you've got it parameterized with Facebook, but is it the same Facebook? I've seen this kinda thing happen (not with Spring Social, but in other contexts) where there'd be 2 or more of a dependency in the classpath and one would be used for some things and another used for other things. If that's the case here, it may be looking for Facebook from dependency 1, but the interceptor is for Facebook from dependency 2.

                          That may not be the problem, but if it is, it's an easy thing to check for: Just look to see if you have more than one spring-social-facebook dependency in your WEB-INF/lib. If so, then that's gotta be it.

                          Comment


                          • #14
                            Maybe the Generics <Facebook> are not right. Actually this was my fault.
                            This weekend I will checkin my code at github than you can have a look at it (GitHub -> Atleticus or www.atleticus.net)

                            Comment


                            • #15
                              Thanks for your replies. Actually i was calling ProviderSignInController instead of ConnectController.
                              Can i make a popup based login using ProviderSignInController as i cannot add a interceptor to ProviderSignInController.

                              Comment

                              Working...
                              X