VMware

Developing a Complete VI Client Plug-in with Tomcat and VI Java API

VERSION 3 Published

Created on: Jun 13, 2008 12:16 AM by Steve Jin - Last Modified:  Feb 22, 2009 3:57 PM by Steve Jin

In this technote, I will show you step by step how to create a complete VI client with Tomcat and the VI Java API (http://vijava.sf.net). The Java servlet sample code demos how to parse the URL sent from the VI client and connect back to the VC server to retrieve information and more.

Average User Rating
(0 ratings)




Comments

Aug 21, 2008 3:43 PM bruce.downs  says:

Great guide. Do you have or know of a version for DotNet? I'm looking for code to 1) login with the given SessionId and 2) MorUtil.createExactManagedEntity.

Aug 21, 2008 3:53 PM Steve Jin  VMware  says: in response to: bruce.downs

Andrew is leading a DotNet version of API. I will have him to post the link here. -Steve

Aug 21, 2008 4:14 PM bruce.downs  says: in response to: Steve Jin

The relevant VIJ code that needs porting is in the ServiceInstance constructor.

public ServiceInstance(URL url, String sessionStr, boolean ignoreCert)
throws ServiceException, RemoteException

You need to set the HEADER_COOKIE property to the following:
vmware_soap_session="GUID"

What I'm unsure of is which object's property is set and what is apache's string value for HEADER_COOKIE.

        • from ServiceInstance.java ****
// sessionStr format: "vmware_soap_session=\"B3240D15-34DF-4BB8-B902-A844FDF42E85\""
public ServiceInstance(URL url, String sessionStr, boolean ignoreCert)
throws ServiceException, RemoteException
{
if(url == null || sessionStr ==null)
{
throw new NullPointerException("None of url, session string can be null.");
}

if(ignoreCert==true)
{
this.ignoreCertificate();
}

setMOR(SERVICE_INSTANCE_MOR);

VimServiceLocator serviceLocator = new VimServiceLocator();
serviceLocator.setMaintainSession(true);

VimPortType vimService = serviceLocator.getVimPort(url);
((org.apache.axis.client.Stub)vimService).setTimeout(1200000); //optional
((VimBindingStub) vimService).setMaintainSession(true);

VimBindingStub vimStub = (VimBindingStub) vimService;
vimStub._setProperty(org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE, sessionStr );

serviceContent = vimService.retrieveServiceContent(SERVICE_INSTANCE_MOR);
setServerConnection(new ServerConnection(url, vimService, this));
UserSession userSession = (UserSession) getSessionManager().getCurrentProperty("currentSession");
getServerConnection().setUserSession(userSession);
}

Aug 21, 2008 4:23 PM Steve Jin  VMware  says: in response to: bruce.downs

The definition of the HEADER_COOKIE is:
public static final java.lang.String HEADER_COOKIE = "Cookie";

Aug 21, 2008 4:32 PM akutz  says:

Check out the VI Toolkit for .NET at http://vitfordotnet.sourceforge.net/

Aug 22, 2008 9:04 AM bruce.downs  says: in response to: akutz

Cool. I'm trying it out.

I'm not sure where to post bugs, but I ran into one minor problem. When reading in the password from the console you are considering the EOL part of the password, so the string becomes password\r and the connection fails.

I fixed temporarily by adding an if check before appending the key char.

i.e.
CommandLineParser::ValidateOptions()
...
if ( !HasOption( "sessionfile" ) &&
GetOptionValue( "password" ) == null )
{
Console.Write( "Password: " );
ConsoleKeyInfo cki;
var sb = new StringBuilder();
do
{
cki = Console.ReadKey( true );

if (cki.Key != ConsoleKey.Enter)
sb.Append( cki.KeyChar );
} while ( cki.Key != ConsoleKey.Enter );
VI_PASSWORD = sb.ToString();
Console.WriteLine();
}

Aug 22, 2008 9:36 AM Steve Jin  VMware  says: in response to: bruce.downs

Thanks a lot Bruce. I filed a bug for you:
https://sourceforge.net/tracker/?func=detail&atid=1073396&aid=2067804&group_id=228007

In the future, you can use this following link to file more bugs and feature requests:
http://sourceforge.net/tracker/?group_id=228007

Aug 22, 2008 9:44 AM Steve Jin  VMware  says: in response to: Steve Jin

VI Java API does not have this issue. So forwarding it to Andrew to take a look. -Steve

Aug 22, 2008 11:36 AM bruce.downs  says: in response to: Steve Jin

I logged this and another bug for vitfordotnet on sourgeforge.

The other bug is relevant to the VI Client plugin as logging in via plainTextCookie does not work. I need this function in order to login with the VI client's sessionId.

As you see in my write up, I fixed the problem temporarily by creating the cookie without a path and calling CookieContainer.Add() with a new Uri of the service Url.

i.e.
UserSession Login( Cookie cookie )
{
ServiceInstance.Service.CookieContainer.Add( new Uri( ServiceInstance.Service.Url ), cookie );
return ( CurrentSession );
}

Login( string plainTextCookie )
{
var c = new Cookie("vmware_soap_session", plainTextCookie);
return Login(c);
}

Feb 21, 2009 6:11 PM Steve Jin  VMware  says:

VI Java API 2.0 was released. http://communities.vmware.com//thread/192910

Steve JIN, VMware Engineering
Creator of VMware Infrastructure Java API: http://vijava.sf.net
VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly freedom to redistribute your applications.
Download, Samples, DocWiki, RSS Feed

Jul 16, 2009 5:28 AM acnsys  says: in response to: Steve Jin

Please help, i am trying my best to get it work but each time i deploy the war project i get:
HTTP Status 404 - /ViClientWebApp/TestServlet


type Status report
message /ViClientWebApp/TestServlet
description The requested resource (/ViClientWebApp/TestServlet) is not available

I see the tab in the VICLient and i have cut and paste your code (Web.xml and TestServlet.java) from the PDF but it wont work...

I also get that error with XML Spy:
Tomcat 6.0\webapps\ViClientWebApp\WEB-INF\Web.xml is not valid.
The element declaration was not found for root element <Web-app>.
Error location: Web-app
Details
cvc-elt.1: The element declaration was not found for root element <Web-app>.

The web.xml i used is the one you provide in the tuto:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>VI Client Plugin Demo App</display-name>
<description> VI Client Plugin Demo App with Tomcat and VI Java
API</description>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</Web-app>

I get this warning too in Eclipse:
Description Resource Path Location Type
The serializable class TestServlet does not declare a static final serialVersionUID field of type long TestServlet.java /ViClientWebApp/WEB-INF/src line 16 Java Problem

Jul 16, 2009 6:51 AM acnsys  says: in response to: acnsys

Nevermind, found the answer myself.

The Web.xml file should be web.xml (no capital) and the web.xml should contain <web-app... and not <Web-app...

Jul 29, 2009 12:30 PM gilsharon  says:

Can you please add the xml file to use when registering this extension?
Thanks!

Aug 11, 2009 12:46 PM ejohannes  says:

There are a couple of capitalization problems in the document related to the web.xml file that caused me some difficulties.
The problems are on page 8. First is the name of the file itself. In the document it calls it Web.xml instead of the correct web.xml. Tomcat wouldn't find and load it with the capital W.
The other problem is in the display of the web.xml file on the tag line <Web-app ...>. The tag should be <web-app ...>. Tomcat won't process things correctly and doesn't really notify you of the issues. The consequence of these problems is that the servlet example doesn't work.

 

Actions

Developer Social Media

Communities