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.
                    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();
            }
        }
    }

When the Android device appears offline while trying to debug

I struggled a bit with my Android device appearing offline while trying to deploy an app I was working on to my phone. I was trying running adb devices to figure out if the system could communicate with the phone.

./adb devices
List of devices attached
<serial_number> offline 

At least since version 4.2.2, there is a security question that appears on the phone requesting RSA verification with the computer. Be sure your tools are updated AND you allow the PC access by verifying the security question on the devices in question. This fixed it for me. And as always, verify you have debugging enabled in the developer options ;)

Using hardware devices for Android development on openSUSE

I started doing android development.

Debugging on my Galaxy Nexus on linux did not work out of the box like it does on OS X.

Google only documented the procedure for Ubuntu based system.

On openSUSE you can put the udev rule here:
/usr/lib/udev/rules.d/51-android.rules

For a Galaxy Nexus you would add the line:
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="users"

For other devices change "04e8" to the relevant USB vendor id

Edit (2nd January 2014): Most devices are now automatically included in the above mentioned file. (Running openSUSE 12.3)