Obtaining a Login Ticket
| October 2005 | |
|
Software Environment
| Feature | Tested on |
| Operating System | Windows 2000 Server SP4 |
| Compiler | Sun JDK 1.4.2_08 |
| Runtime | Sun JRE 1.4.2_08 |
| DFC | 5.3 SP1 |
| Content Server | 5.3 |
Abstract
This code snippet demonstrates obtaining a login ticket and then using it to create a new session
Snippet
/*
* Created on Oct 10, 2005
*
* EMC Documentum Developer Program 2005
*/
package com.documentum.devprog.snippets.session;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
public class CreateSessionUsingLoginTicket
{
/**
* @param args
*/
public static void main(String[] args)
{
String repoName = "devprog";
String username = "dmadmin";
String password = "dmadmin";
try
{
String ticket = getTicket(username,password,repoName);
createSessionUsingTicket(username,ticket,repoName);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* Gets a login ticket
*
*/
private static String getTicket(String username, String password,
String repoName) throws DfException
{
IDfSessionManager sessMgr = createSessionManager();
addIdentity(sessMgr, username, password, repoName);
IDfSession sess = null;
try
{
sess = sessMgr.getSession(repoName);
String ticket = sess.getLoginTicket();
System.out.println("Got ticket: " + ticket);
return ticket;
}
finally
{
if ((sessMgr != null) && (sess != null))
{
sessMgr.release(sess);
}
}
}
/**
* Creates a session using supplied login ticket
*
*/
private static void createSessionUsingTicket(String username,String ticket,String repoName) throws DfException
{
IDfSessionManager sessMgr = createSessionManager();
addIdentity(sessMgr,username,ticket,repoName);
IDfSession sess = null;
try
{
sess = sessMgr.getSession(repoName);
System.out.println("Got session: " + sess.getSessionId());
}
finally
{
if((sessMgr != null) && (sess != null))
{
sessMgr.release(sess);
}
}
}
/**
* Creates a new session manager instance. The session manager does not have
* any identities associated with it.
*
* @return a new session manager object.
* @throws DfException
*/
private static IDfSessionManager createSessionManager() throws DfException
{
IDfClientX clientX = new DfClientX();
IDfClient localClient = clientX.getLocalClient();
IDfSessionManager sessMgr = localClient.newSessionManager();
return sessMgr;
}
/**
* Adds a new identity to the session manager.
*
*/
private static void addIdentity(IDfSessionManager sm, String username,
String password, String repoName) throws DfException
{
IDfClientX clientX = new DfClientX();
IDfLoginInfo li = clientX.getLoginInfo();
li.setUser(username);
li.setPassword(password);
// check if session manager already has an identity.
// if yes, remove it.
if (sm.hasIdentity(repoName))
{
sm.clearIdentity(repoName);
}
sm.setIdentity(repoName, li);
}
}
|
Using the code
The above code snippet comes with a main method and is thus a standalone program. To use it, copy-paste the entire code into a new Java class created in your IDE. Adjust the package and class name of the pasted code according to your preference. Change the username,password, repository name and any other necessary variables. Compile and run the code. For details on setting up your IDE for DFC development, refer to the article Setting up a DFC Development Environment
Note:The above code demonstrates a specific piece of functionality and is not meant to be a complete solution
|