더 쉬운 방법을 찾아서 다시 글 올립니다..
썬에서 제공하는 ldap service provider 1.2.2 를 설치하시면,
~/doc/providers/jndi-ldap.html 에 관련글이 나옵니다
attribute value 를 위한 형식으로 String 과 byte[] 를 지원한다고..
다만 주의할 것은 binary syntax 를 사용할 attributeType 을
다음과 같이 명시적으로 알려주어야 합니다
(binary 를 add 할때는 문제가 없지만 retrieve 할때는 처리해주지 않으면
ClassCastException 이 나더군요)
...
env.put("java.naming.ldap.attributes.binary", "file")
...
"file" 은 제가 미리만들어 놓은 attributeType 입니다
(SYNTAX : binary)
제가 테스트한 소스를 첨부합니다..
(주석이 필요하시다면 허접한 주석이지만 달아서 자료실에 올려드리겠습니다 ^^)
=======================================================================
// 'file' attributeType 과 'fileContainer' object class 를 schema에 추가
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
import com.javaholic.tools.PropertiesReader;
public class AddFileSchema
{
public static void main(String[] javaholic)
throws Exception
{
DirContext initCtx = new InitialDirContext(PropertiesReader.readFromFile("../widefool.properties"));
DirContext schema = initCtx.getSchema("");
DirContext attributeDef = (DirContext)schema.lookup("AttributeDefinition");
DirContext classDef = (DirContext)schema.lookup("ClassDefinition");
Attributes attrs = new BasicAttributes(true);
attrs.put("NUMERICOID", "file-oid");
attrs.put("NAME", "file");
attrs.put("DESC", "attribute for test");
attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.5");
attributeDef.createSubcontext("file", attrs);
attrs = new BasicAttributes(true);
attrs.put("NUMERICOID", "filecontainer-oid");
attrs.put("NAME", "fileContainer");
attrs.put("DESC", "object class for test");
attrs.put("SUP", "top");
attrs.put("STRUCTURAL", "true");
attrs.put("MUST", "cn");
attrs.put("MAY", "file");
classDef.createSubcontext("fileContainer", attrs);
}
}
==========================================================================
// source.gif 화일 저장
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
import java.io.*;
import com.javaholic.tools.PropertiesReader;
public class AddFileTest
{
public static void main(String[] javaholic)
throws Exception
{
RandomAccessFile raf = new RandomAccessFile("./source.gif", "r");
byte[] b = new byte[(int)raf.length()];
raf.readFully(b);
Properties env = PropertiesReader.readFromFile("../widefool.properties");
//env.put("java.naming.ldap.attributes.binary", "file");
DirContext initCtx = new InitialDirContext(env);
Attributes attrs = new BasicAttributes(true);
attrs.put("objectClass", "fileContainer");
attrs.put("cn", "testfile");
attrs.put("file", b);
initCtx.createSubcontext("cn=testfile,o=widefool", attrs);
initCtx.close();
}
}
============================================================================
// cn=testfile,o=widefool 로 부터 화일을 받아옴
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
import java.io.*;
import com.javaholic.tools.PropertiesReader;
public class GetFileTest
{
public static void main(String[] javaholic)
throws Exception
{
Properties env = PropertiesReader.readFromFile("../widefool.properties");
env.put("java.naming.ldap.attributes.binary", "file");
DirContext initCtx = new InitialDirContext(env);
Attribute attr = (initCtx.getAttributes("cn=testfile,o=widefool", new String[] {"file"})).get("file");
byte[] b = (byte[])attr.get();
initCtx.close();
FileOutputStream fo = new FileOutputStream("./copy.gif");
fo.write(b);
fo.close();
}
}
|