/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth.service;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.log.Log;
import org.xdi.model.SmtpConfiguration;
import org.xdi.oxauth.crypto.random.RandomChallengeGenerator;
import org.xdi.oxauth.crypto.signature.SHA256withECDSASignatureVerification;
import org.xdi.oxauth.model.appliance.GluuAppliance;
import org.xdi.service.cache.MemcachedConfiguration;
import org.xdi.util.StringHelper;
import org.xdi.util.security.StringEncrypter.EncryptionException;

/**
 * Holds factoryt methods to create services
 *
 * @author Yuriy Movchan Date: 05/22/2015
 */
@Scope(ScopeType.APPLICATION)
@Name("applicationFactory")
@Startup
public class ApplicationFactory {
    
    @In
    private ApplianceService applianceService;

    @Logger
    private Log log;

    @Factory(value = "randomChallengeGenerator", scope = ScopeType.APPLICATION, autoCreate = true)
    public RandomChallengeGenerator createRandomChallengeGenerator() {
        return new RandomChallengeGenerator();
    }

    @Factory(value = "sha256withECDSASignatureVerification", scope = ScopeType.APPLICATION, autoCreate = true)
    public SHA256withECDSASignatureVerification createBouncyCastleSignatureVerification() {
        return new SHA256withECDSASignatureVerification();
    }

	@Factory(value = "memcachedConfiguration", scope = ScopeType.APPLICATION, autoCreate = true)
	public MemcachedConfiguration createMemcachedConfiguration() {
		return applianceService.getAppliance().getMemcachedConfiguration();
	}

	@Factory(value = "smtpConfiguration", scope = ScopeType.APPLICATION, autoCreate = true)
	public SmtpConfiguration createSmtpConfiguration() {
		GluuAppliance appliance = applianceService.getAppliance();
		SmtpConfiguration smtpConfiguration = appliance.getSmtpConfiguration();
		
		if (smtpConfiguration == null) {
			return null;
		}

		String password = smtpConfiguration.getPassword();
		if (StringHelper.isNotEmpty(password)) {
			try {
				EncryptionService securityService = EncryptionService.instance();
				smtpConfiguration.setPasswordDecrypted(securityService.decrypt(password));
			} catch (EncryptionException ex) {
				log.error("Failed to decript SMTP user password", ex);
			}
		}
		
		return smtpConfiguration;
	}

}
@@ -172,16 +172,60 @@
 rvice;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -245,24 +245,24 @@
 .ScopeType;%0A
-
 import org.j
@@ -933,17 +933,16 @@
  factory
-t
  methods
@@ -1859,22 +1859,63 @@
 n() %7B%0A%09%09
-return
+MemcachedConfiguration memcachedConfiguration =
  applian
@@ -1968,16 +1968,272 @@
 tion();%0A
+%09%09if (memcachedConfiguration == null %7C%7C StringUtils.isBlank(memcachedConfiguration.getServers())) %7B%0A%09%09%09throw new RuntimeException(%22Failed to load memcached configuration from ldap. Please check appliance ldap entry.%22);%0A%09%09%7D%0A%09%09return memcachedConfiguration;%0A
 %09%7D%0A%0A%09@Fa

throw exception if memcached configuration is not in ldap