Custom css

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.
                    httpPost.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    try {
                        instream.close();
                    } catch (Exception ignore) {
                    }
                }
            }

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }


    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 = reader.read(buffer)) != -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);
                        callBack.eventOccured(event);
                        builder = new StringBuilder(builder.substring(indexOfDelimiter + delimiter.length()));
                    }
                }
            } finally {
                is.close();
            }
        }
    }

Ingen kommentarer:

Send en kommentar

Install AWS VPN Client for openSUSE Tumbleweed

Code: curl https://d20adtppz83p9s.cloudfront.net/GTK/latest/awsvpnclient_amd64.deb -o awsvpnclient_amd64.deb sudo zypper in alien alien --t...