com.socklabs.whoisthat.twitter.CacheTwitterFriendsAfterConnectInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.socklabs.whoisthat.twitter.CacheTwitterFriendsAfterConnectInterceptor.java

Source

/*
 * Copyright 2011 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 com.socklabs.whoisthat.twitter;

import com.socklabs.whoisthat.mvc.AccountManager;
import com.socklabs.whoisthat.mvc.EntityManager;
import com.socklabs.whoisthat.mvc.model.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.DuplicateStatusException;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.web.ConnectInterceptor;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.social.twitter.api.CursoredList;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.WebRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * An interceptor specific to the spring-social connect features that is used both before and after a Twitter
 * connection is made for a user. This interceptor attempts to look up the user's Twitter profile and friends and
 * create account meta, entity and entity meta records for the connection.
 * @author ngerakines
 * @since 2/22/2012
 */
public class CacheTwitterFriendsAfterConnectInterceptor implements ConnectInterceptor<Twitter> {

    @Autowired
    private AccountManager accountManager;

    @Autowired
    private EntityManager entityManager;

    private final static Logger logger = LoggerFactory.getLogger(CacheTwitterFriendsAfterConnectInterceptor.class);

    public void preConnect(ConnectionFactory<Twitter> provider, MultiValueMap<String, String> parameters,
            WebRequest request) {
    }

    public void postConnect(Connection<Twitter> connection, WebRequest request) {
        logger.info("connection key {}", connection.getKey());

        Account account = accountManager.getByEmail(request.getUserPrincipal().getName());
        logger.info("account {}", account);

        TwitterProfile twitterProfile = connection.getApi().userOperations().getUserProfile();
        accountManager.addMeta(account.getEmail(), "twitter/id", String.valueOf(twitterProfile.getId()));
        accountManager.addMeta(account.getEmail(), "twitter/username", twitterProfile.getScreenName());
        accountManager.addMeta(account.getEmail(), "twitter/name", twitterProfile.getName());

        List<TwitterProfile> friends = connection.getApi().friendOperations().getFriends();

        logger.info("retrieved {} twitter profiles", friends.size());
        List<String> friendIds = new ArrayList<String>();
        for (TwitterProfile friend : friends) {
            friendIds.add(String.valueOf(friend.getId()));
        }
        String joinedFriendIds = join(friendIds, ",");
        accountManager.addMeta(account.getEmail(), "twitter/friends", joinedFriendIds);
        accountManager.addMeta(account.getEmail(), "twitter/count", String.valueOf(friendIds.size()));

        for (TwitterProfile friend : friends) {
            String entityId = entityManager.create(account.getEmail(), "twitter", String.valueOf(friend.getId()));
            entityManager.addMeta(entityId, "label", friend.getScreenName());
        }
    }

    // TODO: Move this to a utility class.
    static public String join(List<String> list, String conjunction) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String item : list) {
            if (first) {
                first = false;
            } else {
                sb.append(conjunction);
            }
            sb.append(item);
        }
        return sb.toString();
    }

}