/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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 org.apache.james.smtpserver.integration;

import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.api.user.UsersRepository;
import org.apache.james.api.vut.ErrorMappingException;
import org.apache.james.api.vut.VirtualUserTable;
import org.apache.james.api.vut.VirtualUserTableStore;
import org.apache.james.lifecycle.Configurable;
import org.apache.james.services.MailServer;
import org.apache.james.smtpserver.protocol.SMTPSession;
import org.apache.james.smtpserver.protocol.core.fastfail.AbstractValidRcptHandler;
import org.apache.mailet.MailAddress;

/**
 * Handler which reject invalid recipients
 */
public class ValidRcptHandler extends AbstractValidRcptHandler implements
		Configurable {

	private UsersRepository users;

	private VirtualUserTableStore tableStore;
	private VirtualUserTable table;
	private String tableName = null;

    private boolean vut = true;

    private MailServer mailServer;

	/**
	 * Gets the users repository.
	 * 
	 * @return the users
	 */
	public final UsersRepository getUsers() {
		return users;
	}

	/**
	 * Sets the users repository.
	 * 
	 * @param users
	 *            the users to set
	 */
	@Resource(name = "localusersrepository")
	public final void setUsers(UsersRepository users) {
		this.users = users;
	}

	/**
	 * Gets the virtual user table store.
	 * 
	 * @return the tableStore
	 */
	public final VirtualUserTableStore getTableStore() {
		return tableStore;
	}

	/**
	 * Sets the virtual user table store.
	 * 
	 * @param tableStore
	 *            the tableStore to set
	 */
	@Resource(name = "virtualusertable-store")
	public final void setTableStore(VirtualUserTableStore tableStore) {
		this.tableStore = tableStore;
	}
	
	@Resource(name = "mailServer")
	public void setMailServer(MailServer mailServer) {
	    this.mailServer = mailServer;
	}
	
	@PostConstruct
	public void init() throws Exception{
		loadTable();
	}

	/**
	 * @see org.apache.james.lifecycle.Configurable#configure(org.apache.commons.configuration.Configuration)
	 */
	public void configure(HierarchicalConfiguration config) throws ConfigurationException {
		setVirtualUserTableSupport(config.getBoolean("enableVirtualUserTable",
				true));
		setTableName(config.getString("table", null));
	}

	public void setVirtualUserTableSupport(boolean vut) {
		this.vut  = vut;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	private void loadTable() throws Exception {
        table = tableStore.getTable(this.tableName);	
	}


	@Override
	protected boolean isValidRecipient(SMTPSession session,
			MailAddress recipient) {

	    if (mailServer.isLocalServer(recipient.getDomain()) == false) {
	        return false;
	    }
	    
		if (users.contains(recipient.getLocalPart()) == true) {
			return true;
		} else {
			if (vut == true) {
				try {
					Collection<String> targetString = table.getMappings(
							recipient.getLocalPart(), recipient.getDomain());

					if (targetString != null && targetString.isEmpty() == false) {
						return true;
					}
				} catch (ErrorMappingException e) {
					return false;
				}
			}

			return false;
		}
	}
}

@@ -3096,26 +3096,21 @@
 name = %22
-mailServer
+James
 %22)%0A%09publ
@@ -3679,17 +3679,16 @@
 his.vut 
- 
 = vut;%0A%09
@@ -4044,89 +4044,430 @@
 ) %7B%0A
-%09        return false;%0A%09    %7D%0A%09    %0A%09%09if (users.contains(recipient.getLocalPart()
+            session.getLogger().debug(%22Unknown domain %22 + recipient.getDomain() + %22 so reject it%22);%0A%0A%09        return false;%0A%09    %7D%0A%09    %0A%09    String username = recipient.toString();%0A%09    %0A%09    // check if the server use virtualhosting, if not use only the localpart as username%0A%09    if (mailServer.supportVirtualHosting() == false) %7B%0A%09        username = recipient.getLocalPart();%0A%09    %7D%0A%09    %0A%09%09if (users.contains(username
 ) ==
@@ -4502,16 +4502,17 @@
  else %7B%0A
+%0A
 %09%09%09if (v
@@ -4521,24 +4521,120 @@
  == true) %7B%0A
+%09            session.getLogger().debug(%22Unknown user %22 + username + %22 check if its an alias%22);%0A%0A
 %09%09%09%09try %7B%0A%09%09

fix @Resource name