Search in sources :

Example 1 with CommonUnmarshallerListener

use of org.openhab.binding.homematic.internal.model.CommonUnmarshallerListener in project openhab1-addons by openhab.

the class CcuClient method sendScript.

/**
     * Main method for sending a TclRega script and parsing the XML result.
     */
@SuppressWarnings("unchecked")
private synchronized <T> T sendScript(String script, Class<T> clazz) throws HomematicClientException {
    PostMethod post = null;
    try {
        script = StringUtils.trim(script);
        if (StringUtils.isEmpty(script)) {
            throw new RuntimeException("Homematic TclRegaScript is empty!");
        }
        if (TRACE_ENABLED) {
            logger.trace("TclRegaScript: {}", script);
        }
        post = new PostMethod(context.getConfig().getTclRegaUrl());
        RequestEntity re = new ByteArrayRequestEntity(script.getBytes("ISO-8859-1"));
        post.setRequestEntity(re);
        httpClient.executeMethod(post);
        String result = post.getResponseBodyAsString();
        result = StringUtils.substringBeforeLast(result, "<xml><exec>");
        if (TRACE_ENABLED) {
            logger.trace("Result TclRegaScript: {}", result);
        }
        Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
        um.setListener(new CommonUnmarshallerListener());
        return (T) um.unmarshal(new StringReader(result));
    } catch (Exception ex) {
        throw new HomematicClientException(ex.getMessage(), ex);
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringReader(java.io.StringReader) CommonUnmarshallerListener(org.openhab.binding.homematic.internal.model.CommonUnmarshallerListener) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) Unmarshaller(javax.xml.bind.Unmarshaller) JAXBException(javax.xml.bind.JAXBException) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Aggregations

StringReader (java.io.StringReader)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)1 CommonUnmarshallerListener (org.openhab.binding.homematic.internal.model.CommonUnmarshallerListener)1