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.
// 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.