[Seasar-user:9182] [S2Directory]ユーザ名の半角スペースが""に置き換えられる

Taro Unno [E-MAIL ADDRESS DELETED]
2007年 7月 17日 (火) 15:46:21 JST


海野です。
いつもお世話になっております。

S2Directory 0.5で、directory.diconのuserの
接続ユーザ名に半角スペースを指定した場合、
接続時に""に置き換えられてしまいます。

例)
<property name="user">"cn=Sub Manager"</property>
と定義しても、実行時に"cn=SubManager"と置き換えられる。

調べてみたところ、
org.seasar.directory.util.DirectoryDataSourceUtilsクラスの
setupDirectoryControlProperty(DirectoryControlProperty property)メソッドで
   property.setUser(property.getUser().replaceAll("[ \t]", ""));
としているため、半角スペースと水平タブは意図的に""に置き換えている
のだと思います。(何か理由があるのだと思っています。)

しかし、こちらの案件で既に運用されている接続ユーザ名に半角スペースが含まれる為、
この置き換えを行わないようにする必要があります。

そこで下記のように改修案を考えてみました。
やりたいことは、
 「カンマとイコールの直前直後の、半角スペースおよびタブを取り除く」
です。

例)
"uid= uiduser 1 ,	ou=Users,dc=ju"
を
"uid=uiduser 1,ou=Users,dc=ju"
と置き換える。

正規表現にはあまり自信がないので、他に良い方法があれば
アドバイスをお願いします。

また、もしもこの改修が適切であれば、S2Directoryに反映してくださるとありがたいです。

	/**
	 * ディレクトリサーバ接続情報をセットアップします。
	 *
	 * @param property ディレクトリサーバ接続情報
	 */
	public static void setupDirectoryControlProperty(
			DirectoryControlProperty property) {
		if (property.getUser() != null) {
			property.setUser(replaseUserDn(property.getUser()));
			property.setBaseDn(replaseUserDn(property.getBaseDn()));
			property.setUser(getFullUserDn(property));
		}
	}

	/**
	 * 不要な空白、タブを取り除きます。
	 * カンマの直前直後、イコールの直前直後の半角スペースおよびタブを取り除いて返します。
	 *
	 * @param str 文字列
	 * @return 引数の文字列から不要な空白、タブを取り除いた文字列
	 */
	private static String replaseUserDn(String str){
		// 前後の空白を取り除く
		str = str.trim();
		// カンマの直後の空白もしくはタブを取り除く
		str = str.replaceAll(",[ \t]*(?!$)", ",");
		// カンマの直前の空白もしくはタブを取り除く
		str = str.replaceAll("[ \t]*(?!$),", ",");
		// イコールの直後の空白もしくはタブを取り除く
		str = str.replaceAll("=[ \t]*(?!$)", "=");
		// イコールの直前の空白もしくはタブを取り除く
		str = str.replaceAll("[ \t]*(?!$)=", "=");
		return str;
	}


以上、宜しくお願い致します。
-------------- next part --------------
/*
 * Copyright 2005-2006 the Seasar Foundation and the Others.
 *
 * Licensed 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.seasar.directory.util;

import javax.naming.AuthenticationException;
import javax.naming.CommunicationException;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import org.seasar.directory.DirectoryControlProperty;
import org.seasar.directory.DirectoryDataSource;
import org.seasar.directory.exception.DirectoryAuthenticationRuntimeException;
import org.seasar.directory.exception.DirectoryCommunicationRuntimeException;
import org.seasar.directory.exception.DirectoryRuntimeException;

/**
 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
 * 
 * @author Jun Futagawa (Integsystem Corporation)
 * @version $Date:: 2006-12-05 07:33:21 +0900#$
 */
public final class DirectoryDataSourceUtils {
	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓LDAP〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param directoryDataSource 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * @return 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * @throws DirectoryRuntimeException 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 */
	public static DirContext getConnection(
			DirectoryDataSource directoryDataSource) {
		try {
			return directoryDataSource.getConnection();
		} catch (AuthenticationException ae) {
			throw new DirectoryAuthenticationRuntimeException(
					directoryDataSource.getDirectoryControlProperty());
		} catch (CommunicationException ce) {
			throw new DirectoryCommunicationRuntimeException(
					directoryDataSource.getDirectoryControlProperty());
		} catch (NamingException ex) {
			throw new DirectoryRuntimeException(ex);
		}
	}

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param directoryDataSource 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * @return 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 true, 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 false
	 */
	public static boolean authenticate(DirectoryDataSource directoryDataSource) {
		DirContext context = null;
		try {
			context = directoryDataSource.getConnection();
			if (context != null) {
				return true;
			} else {
				return false;
			}
		} catch (NamingException e) {
			return false;
		} finally {
			DirectoryDataSourceUtils.close(context);
		}
	}

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param property 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 */
	public static void setupDirectoryControlProperty(
			DirectoryControlProperty property) {
		if (property.getUser() != null) {
			property.setUser(replaseUserDn(property.getUser()));
			property.setBaseDn(replaseUserDn(property.getBaseDn()));
			property.setUser(getFullUserDn(property));
		}
	}

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param str 〓〓〓〓〓〓〓〓〓
	 * @return 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 */
	private static String replaseUserDn(String str){
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		str = str.trim();
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		str = str.replaceAll(",[ \t]*(?!$)", ",");
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		str = str.replaceAll("[ \t]*(?!$),", ",");
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		str = str.replaceAll("=[ \t]*(?!$)", "=");
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		str = str.replaceAll("[ \t]*(?!$)=", "=");
		return str;
	}

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param context 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 */
	public static void close(DirContext context) {
		if (context == null) {
			return;
		}
		try {
			context.close();
		} catch (NamingException e) {
			throw new DirectoryRuntimeException(e);
		}
	}

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓Dn〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * 
	 * @param property 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 * @return 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓Dn
	 */
	private static String getFullUserDn(DirectoryControlProperty property) {
		String user = property.getUser();
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		String userFirstAttribute = user;
		int index = user.indexOf(",");
		if (index >= 0) {
			userFirstAttribute = user.substring(0, index);
		}
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 "〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓=" 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		if (userFirstAttribute.indexOf("=") == -1) {
			String userAttributeName = property.getUserAttributeName();
			index = userFirstAttribute.indexOf(userAttributeName + "=");
			if (index == -1) {
				user = userAttributeName + "=" + user;
			}
		}
		// 〓〓〓〓〓〓〓〓〓DN〓〓〓 "〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓=" 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		if (user.startsWith(property.getUserAttributeName())) {
			String userSuffix = property.getUserSuffix();
			index = user.indexOf(userSuffix);
			if (index == -1) {
				user = user + "," + userSuffix;
			}
		}
		// 〓〓〓〓〓〓〓〓〓DN〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		String baseDn = property.getBaseDn();
		index = user.indexOf(baseDn);
		if (index == -1) {
			user = user + "," + baseDn;
		}
		return user;
	}
}
-------------- next part --------------
/*
 * Copyright 2005-2006 the Seasar Foundation and the Others.
 *
 * Licensed 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.seasar.directory.util;

import junit.framework.TestCase;
import org.seasar.directory.DirectoryControlProperty;
import org.seasar.directory.impl.DirectoryControlPropertyImpl;
import org.seasar.directory.impl.DirectoryDataSourceImpl;
import org.seasar.directory.util.DirectoryDataSourceUtils;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

/**
 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
 * 
 * @author Jun Futagawa
 * @version $Revision: 53 $ $Date: 2006-08-20 01:46:29 +0900 (〓〓〓, 20 8 2006) $
 */
public class DirectoryDataSourceUtilTest extends TestCase {
	private static final String PATH = "directory.dicon";
	/** Directory〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 */
	private DirectoryDataSourceImpl directoryDataSource;

	/**
	 * 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
	 */
	public void setUp() {
		S2Container container = S2ContainerFactory.create(PATH);
		DirectoryControlProperty defaultProperty = (DirectoryControlProperty)container
				.getComponent(DirectoryControlPropertyImpl.class);
		directoryDataSource = new DirectoryDataSourceImpl(defaultProperty);
	}

	public void testGetFullUserDn() {
		DirectoryControlProperty property = directoryDataSource
				.getDirectoryControlProperty();
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("user1 ");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=user1,ou=Users,dc=seasar,dc=org", property.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("uid=user1  ");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=user1,ou=Users,dc=seasar,dc=org", property.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(uid)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("uiduser1,  ou=Users");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=uiduser1,ou=Users,dc=seasar,dc=org", property
				.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(uid)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("uid= uiduser1 ,	ou=Users,dc=ju");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=uiduser1,ou=Users,dc=ju,dc=seasar,dc=org", property
				.getUser());

		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓)
		property.setUser("user 1 ");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=user 1,ou=Users,dc=seasar,dc=org", property.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓)
		property.setUser("uid=user 1  ");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=user 1,ou=Users,dc=seasar,dc=org", property.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(uid)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("uiduser 1,  ou=Users");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=uiduser 1,ou=Users,dc=seasar,dc=org", property
				.getUser());
		// 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓(uid)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
		property.setUser("uid= uiduser 1 ,	ou=Users,dc=ju");
		DirectoryDataSourceUtils.setupDirectoryControlProperty(property);
		assertEquals("uid=uiduser 1,ou=Users,dc=ju,dc=seasar,dc=org", property
				.getUser());
	}
}


Seasar-user メーリングリストの案内