/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2011 The eXist Project
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */
package org.exist.security.realm.oauth;

import javax.servlet.http.HttpServletRequest;

import org.exist.config.Configurable;
import org.exist.config.Configuration;
import org.exist.config.Configurator;
import org.exist.config.annotation.ConfigurationClass;
import org.exist.config.annotation.ConfigurationFieldAsAttribute;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.Api;
import org.scribe.builder.api.FacebookApi;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

/**
 * <service name="app" key="APP_ID" secret="APP_SECRET" />
 * 
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 *
 */
@ConfigurationClass("service")
public class Service implements Configurable {
	
	private Configuration configuration = null;
	
	@ConfigurationFieldAsAttribute("name")
	String name;

	@ConfigurationFieldAsAttribute("key")
	String apiKey;
    
    @ConfigurationFieldAsAttribute("secret")
    String apiSecret;

    @ConfigurationFieldAsAttribute("provider")
    String provider;

    public Service(OAuthRealm realm, Configuration config) {

		configuration = Configurator.configure(this, config);
	}

	public String getName() {
		return name;
	}

	public ServiceBuilder getServiceBuilder() {
        return new ServiceBuilder()
        		.provider(getProviderClass())
        		.apiKey(apiKey)
        		.apiSecret(apiSecret);
	}
	
	private Class<? extends Api> getProviderClass() {
		if (provider.equalsIgnoreCase("facebook"))
			return FacebookApi.class;
		else if (provider.equalsIgnoreCase("google"))
			return Google2Api.class;
		
		throw new IllegalArgumentException("Unknown provider '"+provider+"'");
	}

	public void saveAccessToken(HttpServletRequest request, OAuthService service, Token accessToken) throws Exception {
		if (provider.equalsIgnoreCase("facebook"))
			ServiceFacebook.saveAccessToken(request, service, accessToken);
		else if (provider.equalsIgnoreCase("google"))
			ServiceGoogle.saveAccessToken(request, service, accessToken);
		
		throw new IllegalArgumentException("Unknown provider '"+provider+"'");
	}
	
	public String getApiKey() {
		return apiKey;
	}

	public String getApiSecret() {
		return apiSecret;
	}

	public boolean isConfigured() {
		return configuration != null;
	}

	public Configuration getConfiguration() {
		return configuration;
	}
}
@@ -2328,24 +2328,178 @@
 t);%0D%0A%09%7D%0D%0A%09%0D%0A
+%09private String getProvider() %7B%0D%0A%09%09if (provider == null)%0D%0A%09%09%09throw new IllegalArgumentException(%22Provider was not set.%22);%0D%0A%09%09%0D%0A%09%09return provider; %0D%0A%09%7D%0D%0A%0D%0A
 %09private Cla
@@ -2534,24 +2534,62 @@
 rClass() %7B%0D%0A
+%09%09String provider = getProvider();%0D%0A%0D%0A
 %09%09if (provid
@@ -2928,24 +2928,62 @@
 xception %7B%0D%0A
+%09%09String provider = getProvider();%0D%0A%0D%0A
 %09%09if (provid

make exception clear on NPE