JavaBlog.fr / Java.lu DEVELOPMENT,DFC DCTM,Documentum Documentum : Principal-Mode Authentication : Use of IDfPrincipalSupport

Documentum : Principal-Mode Authentication : Use of IDfPrincipalSupport

Hi,

After the post concerning the Documentum : Ticketed Authentication, Generation Of DM_TICKET, I would expose in this post the principal-mode authentication via the use of IDfPrincipalSupport interface.

The DFC javadoc describes this interface: An interface supported by classes that can establish sessions using principal-mode authentication. Principal-mode authentication is a form of authentication in which the caller has already established the validity of the user and therefore an explicit password verification is not needed.
 
 

IDfPrincipalSupport Interface

In order to use principal support, the IDfPrincipalSupport object must be a custom class that implements IDfPrincipalSupport and overrides its IDfSession IDfPrincipalSupport.getSession(String docbaseName, String principalName) method. More, this class must could have a constructor with Constructor(IDfTrustManager trustManager) parameter for an established authentication within the admin-user.
 
A getSession method with String docbaseName, String principalName parameters corresponding to the docbase and login of user for which a session must be created. First a session is created for the admin-user with the established dfSessionManager

1synchronized (dfSessionManager) {
2    if (!dfSessionManager.hasIdentity(docbaseName)) {
3        dfSessionManager.setIdentity(docbaseName, dfTrustManager.getTrustCredential(docbaseName));
4    }
5}
6IDfSession dfSession = dfSessionManager.getSession(docbaseName);

 
….and second, a LoginTicket is created for principalName user via the previously created session:

1IDfClientX dfClientX = new DfClientX();
2IDfClient dfClient = dfClientX.getLocalClient();
3result = dfClient.newSession(docbaseName, new DfLoginInfo(principalName, dfSession.getLoginTicketForUser(principalName)));

 
…. and last, the first created session of admin-user is released:

1dfSessionManager.release(dfSession);

 
Here the full code of MyPrincipalSupport class:

01private static class MyPrincipalSupport implements IDfPrincipalSupport {
02 
03    private final IDfTrustManager dfTrustManager;
04    private final IDfSessionManager dfSessionManager;
05 
06    public MyPrincipalSupport(IDfTrustManager trustManager) throws DfException {
07        if (trustManager == null) {
08            throw new IllegalArgumentException("trustManager cannot be null");
09        }
10        this.dfTrustManager = trustManager;
11        IDfClientX dfClientX = new DfClientX();
12        IDfClient dfClient = dfClientX.getLocalClient();
13        this.dfSessionManager = dfClient.newSessionManager();
14    }
15 
16    @Override
17    public IDfSession getSession(String docbaseName, String principalName) throws DfPrincipalException {
18        try {
19            if (!dfSessionManager.hasIdentity(docbaseName)) {
20                synchronized (dfSessionManager) {
21                    if (!dfSessionManager.hasIdentity(docbaseName)) {
22                        dfSessionManager.setIdentity(docbaseName, dfTrustManager.getTrustCredential(docbaseName));
23                    }
24                }
25            }
26            IDfSession result;
27            IDfSession dfSession = dfSessionManager.getSession(docbaseName);
28            try {
29                IDfClientX dfClientX = new DfClientX();
30                IDfClient dfClient = dfClientX.getLocalClient();
31                result = dfClient.newSession(docbaseName, new DfLoginInfo(principalName, dfSession.getLoginTicketForUser(principalName)));
32            } finally {
33                dfSessionManager.release(dfSession);
34            }
35            return result;
36        } catch (DfPrincipalException e) {
37            throw e;
38        } catch (Exception e) {
39            throw new DfPrincipalException(MessageFormat.format("Unable to retrieve IdfSession for user \"{0}\" and docbase {1}", principalName, docbaseName), e);
40        }
41    }
42}

 
 

Identity Mode VS Principal Support Mode

The Identity Mode is the classic authentication method via the parameters String userAdmin, String passwdAdmin, String docbase:

01IDfLoginInfo login = new DfLoginInfo();
02login.setUser(userAdmin);
03login.setPassword(passwdAdmin);
04IDfClientX clientx = new DfClientX();
05IDfClient client = clientx.getLocalClient();
06 
07this.sessMgr = client.newSessionManager();
08this.sessMgr.setIdentity(docbase, login);
09 
10this.idfSession = sessMgr.getSession(docbase);
11if (this.idfSession != null)
12    System.out.println("Session created successfully");

 
The Principal Support Mode is a proxy authentication method using the class implementing IDfPrincipalSupport with the parameters String userAdmin, String passwdAdmin, String docbase, String principalName. The void setPrincipalSupport(IDfPrincipalSupport support) method changes the session manager mode from “Identity” mode to “Principal Support” mode in order to support single sign in. This method allows a client to define a handler that creates sessions on behalf of principal users.

01IDfLoginInfo login = new DfLoginInfo();
02login.setUser(userAdmin);
03login.setPassword(passwdAdmin);
04IDfClientX clientx = new DfClientX();
05IDfClient client = clientx.getLocalClient();
06 
07client.setPrincipalSupport(new MyPrincipalSupport(new DfSimpleTrustManager(new DfLoginInfo(userAdmin, passwdAdmin))));
08this.sessMgr = client.newSessionManager();
09this.sessMgr.setPrincipalName(principalName);
10 
11this.idfSession = sessMgr.getSession(docbase);
12if (this.idfSession != null)
13    System.out.println("Session created successfully");

 
 

TESTS : Identity Mode VS Principal Support Mode

Here, a test class creating a document using these 2 modes:

001package com.huo.test.ecm.test5;
002 
003import java.io.ByteArrayOutputStream;
004import java.io.File;
005import java.io.FileInputStream;
006import java.io.InputStream;
007import java.text.MessageFormat;
008 
009import org.apache.commons.io.IOUtils;
010 
011import com.documentum.com.DfClientX;
012import com.documentum.com.IDfClientX;
013import com.documentum.fc.client.DfPrincipalException;
014import com.documentum.fc.client.DfSimpleTrustManager;
015import com.documentum.fc.client.IDfClient;
016import com.documentum.fc.client.IDfDocument;
017import com.documentum.fc.client.IDfPrincipalSupport;
018import com.documentum.fc.client.IDfSession;
019import com.documentum.fc.client.IDfSessionManager;
020import com.documentum.fc.client.IDfTrustManager;
021import com.documentum.fc.common.DfException;
022import com.documentum.fc.common.DfLoginInfo;
023import com.documentum.fc.common.IDfLoginInfo;
024import com.documentum.fc.common.impl.MessageHelper;
025 
026 
027 
028/**
029 * Documentum DFC - Principal-Mode Authentication : Use of IDfPrincipalSupport
030 *
031 * Use of "IDfPrincipalSupport" interface:
032 *          An interface supported by classes that can establish sessions using principal-mode authentication.
033 *          Principal-mode authentication is a form of authentication in which the caller has already established the validity of the user and therefore an explicit
034 *          password verification is not needed.
035 *
036 */
037public class DfcPrincipalSupportLoginTest {
038 
039 
040    IDfSession idfSession = null;
041    IDfSessionManager sessMgr = null;
042 
043    private static class MyPrincipalSupport implements IDfPrincipalSupport {
044 
045        private final IDfTrustManager dfTrustManager;
046        private final IDfSessionManager dfSessionManager;
047 
048        public MyPrincipalSupport(IDfTrustManager trustManager) throws DfException {
049            if (trustManager == null) {
050                throw new IllegalArgumentException("trustManager cannot be null");
051            }
052            this.dfTrustManager = trustManager;
053            IDfClientX dfClientX = new DfClientX();
054            IDfClient dfClient = dfClientX.getLocalClient();
055            this.dfSessionManager = dfClient.newSessionManager();
056        }
057 
058        @Override
059        public IDfSession getSession(String docbaseName, String principalName) throws DfPrincipalException {
060            try {
061                if (!dfSessionManager.hasIdentity(docbaseName)) {
062                    synchronized (dfSessionManager) {
063                        if (!dfSessionManager.hasIdentity(docbaseName)) {
064                            dfSessionManager.setIdentity(docbaseName, dfTrustManager.getTrustCredential(docbaseName));
065                        }
066                    }
067                }
068                IDfSession result;
069                IDfSession dfSession = dfSessionManager.getSession(docbaseName);
070                try {
071                    IDfClientX dfClientX = new DfClientX();
072                    IDfClient dfClient = dfClientX.getLocalClient();
073                    result = dfClient.newSession(docbaseName, new DfLoginInfo(principalName, dfSession.getLoginTicketForUser(principalName)));
074                } finally {
075                    dfSessionManager.release(dfSession);
076                }
077                return result;
078            } catch (DfPrincipalException e) {
079                throw e;
080            } catch (Exception e) {
081                throw new DfPrincipalException(MessageFormat.format("Unable to retrieve IdfSession for user \"{0}\" and docbase {1}", principalName, docbaseName), e);
082            }
083        }
084    }
085     
086 
087 
088 
089    /**
090     * Create a Session in "Identity Mode" OR "Principal Support Mode"
091     * @param userAdmin
092     * @param passwdAdmin
093     * @param docbase
094     * @param principalName
095     * @throws Exception
096     */
097    public DfcPrincipalSupportLoginTest(String userAdmin, String passwdAdmin, String docbase, String principalName) throws Exception {
098 
099        IDfLoginInfo login = new DfLoginInfo();
100        login.setUser(userAdmin);
101        login.setPassword(passwdAdmin);
102        IDfClientX clientx = new DfClientX();
103        IDfClient client = clientx.getLocalClient();
104 
105        // Principal Support Mode
106        if(principalName!=null){
107            client.setPrincipalSupport(new MyPrincipalSupport(new DfSimpleTrustManager(new DfLoginInfo(userAdmin, passwdAdmin))));
108            this.sessMgr = client.newSessionManager();
109            this.sessMgr.setPrincipalName(principalName);
110             
111        // Identity Mode
112        }else{
113            this.sessMgr = client.newSessionManager();
114            this.sessMgr.setIdentity(docbase, login);
115        }
116         
117        this.idfSession = sessMgr.getSession(docbase);
118        if (this.idfSession != null)
119            System.out.println("Session created successfully");
120    }  
121     
122 
123 
124    public void releaseSession() throws Exception {
125        if(sessMgr!=null && idfSession!=null){
126            sessMgr.release(idfSession);
127        }
128    }
129     
130 
131 
132 
133 
134    public static void main(String[] args) throws Exception {
135        testWithSession();
136         
137        testWithSessionOfPrincipalNameViaPrincipalSupport();
138    }
139     
140    public static void testWithSession() throws Exception {
141        long startTime = 0;
142        long stopTime = 0;
143         
144        String userAdmin = "adminuser";
145        String passwdAdmin = "pass_4adminuser";
146        String docbase = "MY_DOCBASE";
147         
148        DfcPrincipalSupportLoginTest object = new DfcPrincipalSupportLoginTest(userAdmin, passwdAdmin, docbase, null);
149         
150        boolean isTransactionalSession = false;
151        boolean noErrorWithCurrentDocument = false;
152        try {
153            if (!object.idfSession.isTransactionActive()) {
154                object.idfSession.beginTrans();
155                isTransactionalSession = true;
156            }
157 
158            startTime = System.currentTimeMillis();
159             
160            // --- MetaData
161            IDfDocument dfDocument = (IDfDocument) object.idfSession.newObject("my_huo_document");
162            dfDocument.setObjectName("Object's name");
163            dfDocument.setTitle("Object's title");
164            dfDocument.setString("owner_name", userAdmin);
165            dfDocument.setString("year", "2018");
166            dfDocument.setString("status_label", "DRAFT");
167            dfDocument.setContentType("excel12book");
168 
169            // --- Content
170            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
171            InputStream inputStream = null;
172            try {
173                File contentFile = new File("C:\\Users\\principalName\\Desktop\\temp.pdf");
174                inputStream = new FileInputStream(contentFile);
175                IOUtils.copy(inputStream, byteArrayOutputStream);
176                dfDocument.setContent(byteArrayOutputStream);
177                dfDocument.save();
178            } finally {
179                if(inputStream!=null){
180                    inputStream.close();
181                }
182 
183                byteArrayOutputStream.close();
184            }
185            System.out.println("New document created successfully : " + dfDocument.getObjectId().getId());
186            System.out.println("---------------------- ");
187            System.out.println("object_name : " + dfDocument.getString("object_name"));
188            System.out.println("title : " + dfDocument.getString("title"));
189            System.out.println("owner_name : " + dfDocument.getString("owner_name"));
190            System.out.println("r_modifier : " + dfDocument.getString("r_modifier"));
191            System.out.println("r_creator_name : " + dfDocument.getString("r_creator_name"));
192            for(int i=0 ; i<dfDocument.getVersionLabelCount(); i++){
193                System.out.println("r_version_label ["+i+"]: " + dfDocument.getVersionLabel(i));
194            }
195 
196            stopTime = System.currentTimeMillis();
197             
198            noErrorWithCurrentDocument = true;
199             
200        } catch (Throwable e) {
201            StringBuilder sb = new StringBuilder(MessageFormat.format("ERROR : {0}", "java.lu"));
202            sb.append(IOUtils.LINE_SEPARATOR);
203            sb.append(MessageHelper.getStackTraceAsString(e));
204            System.out.println(sb.toString());
205             
206        } finally {
207            if (isTransactionalSession) {
208                if (noErrorWithCurrentDocument) {
209                    object.idfSession.commitTrans();
210                } else {
211                    object.idfSession.abortTrans();
212                }
213            }
214            // to release a docbase session
215            object.releaseSession();
216             
217            long elapsedTime = stopTime - startTime;
218            System.out.println(MessageFormat.format("Execute() total execution time : {0} ms ", elapsedTime));
219        }
220    }
221     
222 
223 
224 
225    public static void testWithSessionOfPrincipalNameViaPrincipalSupport() throws Exception {
226        long startTime = 0;
227        long stopTime = 0;
228         
229        String userAdmin = "adminuser";
230        String passwdAdmin = "pass_4adminuser";
231        String docbase = "MY_DOCBASE";
232        String principalName = "principalName";
233         
234        DfcPrincipalSupportLoginTest object = new DfcPrincipalSupportLoginTest(userAdmin, passwdAdmin, docbase, principalName);
235         
236        boolean isTransactionalSession = false;
237        boolean noErrorWithCurrentDocument = false;
238        try {
239            if (!object.idfSession.isTransactionActive()) {
240                object.idfSession.beginTrans();
241                isTransactionalSession = true;
242            }
243 
244            startTime = System.currentTimeMillis();
245             
246            // --- MetaData
247            IDfDocument dfDocument = (IDfDocument) object.idfSession.newObject("my_huo_document");
248            dfDocument.setObjectName("Object's name");
249            dfDocument.setTitle("Object's title");
250            dfDocument.setString("owner_name", principalName);
251            dfDocument.setString("year", "2018");
252            dfDocument.setString("status_label", "DRAFT");
253            dfDocument.setContentType("excel12book");
254 
255            // --- Content
256            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
257            InputStream inputStream = null;
258            try {
259                File contentFile = new File("C:\\Users\\principalName\\Desktop\\temp.pdf");
260                inputStream = new FileInputStream(contentFile);
261                IOUtils.copy(inputStream, byteArrayOutputStream);
262                dfDocument.setContent(byteArrayOutputStream);
263                dfDocument.save();
264            } finally {
265                if(inputStream!=null){
266                    inputStream.close();
267                }
268 
269                byteArrayOutputStream.close();
270            }
271             
272            System.out.println("New document created successfully : " + dfDocument.getObjectId().getId());
273 
274            System.out.println("---------------------- ");
275            System.out.println("object_name : " + dfDocument.getString("object_name"));
276            System.out.println("title : " + dfDocument.getString("title"));
277            System.out.println("owner_name : " + dfDocument.getString("owner_name"));
278            System.out.println("r_modifier : " + dfDocument.getString("r_modifier"));
279            System.out.println("r_creator_name : " + dfDocument.getString("r_creator_name"));
280            for(int i=0 ; i<dfDocument.getVersionLabelCount(); i++){
281                System.out.println("r_version_label ["+i+"]: " + dfDocument.getVersionLabel(i));
282            }
283 
284            stopTime = System.currentTimeMillis();
285             
286            noErrorWithCurrentDocument = true;
287             
288        } catch (Throwable e) {
289            StringBuilder sb = new StringBuilder(MessageFormat.format("ERROR : {0}", "java.lu"));
290            sb.append(IOUtils.LINE_SEPARATOR);
291            sb.append(MessageHelper.getStackTraceAsString(e));
292            System.out.println(sb.toString());
293             
294        } finally {
295            if (isTransactionalSession) {
296                if (noErrorWithCurrentDocument) {
297                    object.idfSession.commitTrans();
298                } else {
299                    object.idfSession.abortTrans();
300                }
301            }
302            // to release a docbase session
303            object.releaseSession();
304             
305            long elapsedTime = stopTime - startTime;
306            System.out.println(MessageFormat.format("Execute() total execution time : {0} ms ", elapsedTime));
307        }
308    }
309     
310}

 
 
… the outputs are:

01New document created successfully : 090xxxxxxxxxxxff4
02----------------------
03object_name : Object's name
04title : Object's title
05owner_name : adminuser
06r_modifier : adminuser
07r_creator_name : adminuser
08r_version_label [0]: 1.0
09r_version_label [1]: CURRENT
10Execute() total execution time : 8,327 ms
11 
12 
13 
14 
15New document created successfully : 090xxxxxxxxxxxff5
16----------------------
17object_name : Object's name
18title : Object's title
19owner_name : principalName
20r_modifier : principalName
21r_creator_name : principalName
22r_version_label [0]: 1.0
23r_version_label [1]: CURRENT
24Execute() total execution time : 600 ms

Best regards,

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post