mandag den 26. august 2013

Cisco UCS XML API Event from java

I've been working professionally on an integration with the Cisco UCS Manager. Their API is non-standard. It isn't SOAP or REST or any other well known protocol. But it seems that there are actually good reasons for this. It is easy to write scripts that uses the XML API.

I did not need to write scripts though. I had to implement a java client. There were limited resources available on the subject so I wanted to post what I came up with for subscribing for events.

    public void subscribe(String host, String port) throws InterruptedException, IOException, HttpException {
String subscribeRequest = "<eventSubscribe cookie=\"\"><inFilter></inFilter></eventSubscribe>";
        HttpClient httpclient = new DefaultHttpClient();

        try {
            HttpPost httpPost = new HttpPost(MessageFormat.format("http://{1}:{2}/nuova", host, port));

            httpPost.setEntity(new StringEntity(subscribeRequest));

            // Execute HTTP request
            HttpResponse response = httpclient.execute(httpPost);

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

            // If the response does not enclose an entity, there is no need
            // to bother about connection release
            if (entity != null) {
                InputStream instream = new BufferedInputStream(entity.getContent());
                try {
                    generateEventsFromStream(instream, new EventCallBackHandler());
                } catch (RuntimeException ex) {
                    // In case of an unexpected exception you may want to abort
                    // the HTTP request in order to shut down the underlying
                    // connection immediately.
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    try {
                    } catch (Exception ignore) {

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources

    public void generateEventsFromStream(InputStream is, EventCallBackHandler callBack) throws IOException {
        if (is != null) {
            // Writer writer = new StringWriter();

            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[1024];
            int inEid = -1;
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is));
                int n;
                while ((n = != -1 && !stopRequested) {
                    builder.append(new String(buffer, 0, n));
                    int indexOfDelimiter = builder.indexOf(delimiter);
                    if (indexOfDelimiter != -1) {
                        String event = builder.substring(0, indexOfDelimiter + delimiter.length());
                        int indexOfStart = event.indexOf("<methodVessel"); //$NON-NLS-1$
                        event = event.substring(indexOfStart);
                        builder = new StringBuilder(builder.substring(indexOfDelimiter + delimiter.length()));
            } finally {

Ingen kommentarer:

Send en kommentar