VMware
3 Replies Last post: Aug 7, 2009 2:08 AM by thomasjulian  

!! Session management with Cookies !! posted: May 19, 2008 10:04 AM

Click to view akutz's profile Hot Shot 317 posts since
Nov 9, 2005
Just thought I'd share this bit 'o knowledge with the community. When building the VI Toolkits for .NET and Java I created methods for connecting to VI with an existing session cookie (like Perl and PowerShell). In .NET that looks like this:

// The next line is self-explanatory.
m_vim_svc_content =
m_vim_svc.RetrieveServiceContent( vim_svc_ref );

if ( GetOptionValue( "sessionfile" ) != null )
{
Trace.TraceInformation( "Connecting with session file" );

BinaryFormatter bf = new BinaryFormatter();
Stream s = File.Open(
GetOptionValue( "sessionfile" ), FileMode.Open );
Cookie c = bf.Deserialize( s ) as Cookie;
s.Close();
m_vim_svc.CookieContainer.Add( c );
}

In the above example the session content is retrieved and then the Cookie object is deserialized from a text file where it was stored (you can store just the cookie value and create a new Cookie object as well, but storing the object by serializing it to a file means that the VI Toolkit for Windows (PowerShell) can use it as well).

In Java however, the same logic does not work. You have to do this:

m_vim_svc = vim_svc_loc.getVimPort( new java.net.URL( getVIUrl() ) );
VimBindingStub vbs = ( VimBindingStub ) m_vim_svc;
vbs.setMaintainSession( true );

if ( getOptionValue( "sessionfile" ) != null )
{
/*
* Load the session cookie. It is important that when using Axis,
* the session cookie is loaded BEFORE the service content is
* retrieved, otherwise the session will not be authenticated.
*/
String session_file = getOptionValue( "sessionfile" );
BufferedReader br =
new BufferedReader( new FileReader( session_file ) );
String session_cookie = br.readLine();
br.close();
vbs._setProperty(
org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE,
session_cookie );
m_vim_svc_content = m_vim_svc.retrieveServiceContent( vim_svc_ref );
}
else
{
m_vim_svc_content = m_vim_svc.retrieveServiceContent( vim_svc_ref );
m_vim_svc.login( m_vim_svc_content.getSessionManager(),
getOptionValue( "username" ), getOptionValue( "password" ),
null );
}

In Java you must set the cookie BEFORE you retrieve the service content. However, you still must retrieve the service content before you login via username and password.

I am putting this out there just in case one or two of you trip over the same issue.

Many thanks to Steve Jin @ VMware for pointing me in the right direction on this.

Re: !! Session management with Cookies !!

1. Aug 26, 2008 6:14 PM in response to: akutz
Click to view v_potnis2001's profile Enthusiast 53 posts since
Jan 17, 2007
Let's say there large number of concurrent client requests to ESX hostd coming from a mgmt application.
The app is running inside a VM and connects to the same ESX server that hosts the VM.

Each request makes similar VI SDK java api calls.

Is there a limit on the max number of concurrent sessions that are allowed? We see messages:
2008-08-26 14:45:36.833 'SoapAdapter' 22461360 error SOAP session count limit reached

Another question, is if we allow one client thread to establish a session cookie and other threads to reuse the same session cookie, would this give a substantial performance gain on the ESX server? (session pooling)

Is connection pooling possible?

Re: !! Session management with Cookies !!

2. Jun 17, 2009 5:08 PM in response to: v_potnis2001
Click to view jjp's profile Enthusiast 26 posts since
Apr 6, 2005
The documentation states that the session limit is set to 100.

Re: !! Session management with Cookies !!

3. Aug 7, 2009 2:08 AM in response to: jjp
Click to view thomasjulian's profile Novice 10 posts since
Feb 20, 2009

Hi,

Could you please post the full code.I want to know how to get the cookie from the session and how to store it and how to use the same session id for further communication with the server.In the above code we are using a previously stored cookie right.What we need to do during the first time we connect with the server.

Thanks.


Developer Social Media

Communities