001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.user.lib;
021
022 import javax.annotation.Resource;
023
024 import org.apache.commons.configuration.ConfigurationException;
025 import org.apache.commons.configuration.HierarchicalConfiguration;
026 import org.apache.james.domainlist.api.DomainList;
027 import org.apache.james.domainlist.api.DomainListException;
028 import org.apache.james.lifecycle.api.Configurable;
029 import org.apache.james.lifecycle.api.LogEnabled;
030 import org.apache.james.user.api.UsersRepository;
031 import org.apache.james.user.api.UsersRepositoryException;
032 import org.slf4j.Logger;
033
034 public abstract class AbstractUsersRepository implements UsersRepository, LogEnabled, Configurable {
035
036 private DomainList domainList;
037 private boolean virtualHosting;
038 private Logger logger;
039
040 protected Logger getLogger() {
041 return logger;
042 }
043
044 /**
045 * @see org.apache.james.lifecycle.api.LogEnabled#setLog(org.slf4j.Logger)
046 */
047 public void setLog(Logger logger) {
048 this.logger = logger;
049 }
050
051 /**
052 * @see
053 * org.apache.james.lifecycle.api.Configurable#configure(org.apache.commons.configuration.HierarchicalConfiguration)
054 */
055 public void configure(HierarchicalConfiguration configuration) throws ConfigurationException {
056
057 virtualHosting = configuration.getBoolean("enableVirtualHosting", false);
058
059 doConfigure(configuration);
060 }
061
062 protected void doConfigure(HierarchicalConfiguration config) throws ConfigurationException {
063
064 }
065
066 public void setEnableVirtualHosting(boolean virtualHosting) {
067 this.virtualHosting = virtualHosting;
068 }
069
070 @Resource(name = "domainlist")
071 public void setDomainList(DomainList domainList) {
072 this.domainList = domainList;
073 }
074
075 protected void isValidUsername(String username) throws UsersRepositoryException {
076 int i = username.indexOf("@");
077 if (supportVirtualHosting()) {
078 // need a @ in the username
079 if (i == -1) {
080 throw new UsersRepositoryException("Given Username needs to contain a @domainpart");
081 } else {
082 String domain = username.substring(i + 1);
083 try {
084 if (domainList.containsDomain(domain) == false) {
085 throw new UsersRepositoryException("Domain does not exist in DomainList");
086 } else {
087 return;
088 }
089 } catch (DomainListException e) {
090 throw new UsersRepositoryException("Unable to query DomainList", e);
091 }
092 }
093 } else {
094 // @ only allowed when virtualhosting is supported
095 if (i != -1) {
096 throw new UsersRepositoryException("Given Username contains a @domainpart but virtualhosting support is disabled");
097 }
098 }
099 }
100
101 /**
102 * @see org.apache.james.user.api.UsersRepository#addUser(java.lang.String,
103 * java.lang.String)
104 */
105 public void addUser(String username, String password) throws UsersRepositoryException {
106
107 if (contains(username) == false) {
108 isValidUsername(username);
109 doAddUser(username, password);
110 } else {
111 throw new UsersRepositoryException("User with username " + username + " already exist!");
112 }
113
114 }
115
116 /**
117 * @see org.apache.james.user.api.UsersRepository#supportVirtualHosting()
118 */
119 public boolean supportVirtualHosting() throws UsersRepositoryException {
120 return virtualHosting;
121 }
122
123 /**
124 * Add the user with the given username and password
125 *
126 * @param username
127 * @param password
128 * @throws UsersRepositoryException
129 * If an error occurred
130 */
131 protected abstract void doAddUser(String username, String password) throws UsersRepositoryException;
132 }