<commit_before>/*
 *  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:<EMAIL>"><NAME></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 String getProvider() {
		if (provider == null)
			throw new IllegalArgumentException("Provider was not set.");
		
		return provider; 
	}

	private Class<? extends Api> getProviderClass() {
		String provider = getProvider();

		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 {
		String provider = getProvider();

		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;<commit_msg>[ignore] small optimization<commit_after>/*
 *  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:<EMAIL>"><NAME></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 String getProvider() {
		if (provider == null)
			throw new IllegalArgumentException("Provider was not set.");
		
		return provider; 
	}

	private Class<? extends Api> getProviderClass() {
		String provider = getProvider().toLowerCase();

		if (provider.equals("facebook"))
			return FacebookApi.class;
		else if (provider.equals("google"))
			return Google2Api.class;
		
		throw new IllegalArgumentException("Unknown provider '"+provider+"'");
	}

	public void saveAccessToken(HttpServletRequest request, OAuthService service, Token accessToken) throws Exception {
		String provider = getProvider().toLowerCase();

		if (provider.equals("facebook"))
			ServiceFacebook.saveAccessToken(request, service, accessToken);
		else if (provider.equals("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;
{'fraction_non_alphanumeric': 0.0978450786255096, 'fraction_numerical': 0.0049504950495049506, 'mean_word_length': 4.473306772908367, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090747', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1836, 'n_words': 585}