com.seajas.search.codex.social.interceptor.FacebookConnectInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.codex.social.interceptor.FacebookConnectInterceptor.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.codex.social.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.ConnectInterceptor;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.request.WebRequest;

/**
 * Simple Facebook protocol interceptor, which stores the connection data.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class FacebookConnectInterceptor extends AbstractConnectInterceptor implements ConnectInterceptor<Facebook> {
    /**
     * The current connection repository.
     */
    private ConnectionRepository currentConnectionRepository;

    /**
     * {@inheritDoc}
     */
    @Override
    public void preConnect(final ConnectionFactory<Facebook> connectionFactory,
            final MultiValueMap<String, String> parameters, final WebRequest request) {
        addState(parameters, request);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void postConnect(final Connection<Facebook> connection, final WebRequest request) {
        try {
            storeConnectionData(connection.createData(), getState(request));
        } finally {
            // Remove this connection, as we don't keep state

            removeConnection(currentConnectionRepository, connection.getKey());
        }
    }

    /**
     * Set the currentConnectionRepository on a per-request basis.
     * 
     * @param currentConnectionRepository
     */
    @Autowired
    @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public void setCurrentConnectionRepository(final ConnectionRepository currentConnectionRepository) {
        this.currentConnectionRepository = currentConnectionRepository;
    }
}