Search in sources :

Example 1 with CachingMapper

use of com.thoughtworks.xstream.mapper.CachingMapper in project camel by apache.

the class XmlRestProcessor method processResponse.

@Override
protected void processResponse(Exchange exchange, InputStream responseEntity, SalesforceException exception, AsyncCallback callback) {
    final XStream localXStream = xStream.get();
    try {
        // do we need to un-marshal a response
        if (responseEntity != null) {
            final Class<?> responseClass = exchange.getProperty(RESPONSE_CLASS, Class.class);
            Object response;
            if (responseClass != null) {
                // its ok to call this multiple times, as xstream ignores duplicate calls
                localXStream.processAnnotations(responseClass);
                final String responseAlias = exchange.getProperty(RESPONSE_ALIAS, String.class);
                if (responseAlias != null) {
                    // extremely dirty, need to flush entire cache if its holding on to an old alias!!!
                    final CachingMapper mapper = (CachingMapper) localXStream.getMapper();
                    try {
                        if (mapper.realClass(responseAlias) != responseClass) {
                            mapper.flushCache();
                        }
                    } catch (CannotResolveClassException ignore) {
                        // recent XStream versions add a ClassNotFoundException to cache
                        mapper.flushCache();
                    }
                    localXStream.alias(responseAlias, responseClass);
                }
                response = responseClass.newInstance();
                localXStream.fromXML(responseEntity, response);
            } else {
                // return the response as a stream, for getBlobField
                response = responseEntity;
            }
            exchange.getOut().setBody(response);
        } else {
            exchange.setException(exception);
        }
        // copy headers and attachments
        exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
        exchange.getOut().getAttachmentObjects().putAll(exchange.getIn().getAttachmentObjects());
    } catch (XStreamException e) {
        String msg = "Error parsing XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } catch (Exception e) {
        String msg = "Error creating XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } finally {
        // cleanup temporary exchange headers
        exchange.removeProperty(RESPONSE_CLASS);
        exchange.removeProperty(RESPONSE_ALIAS);
        // consume response entity
        if (responseEntity != null) {
            try {
                responseEntity.close();
            } catch (IOException ignored) {
            }
        }
        // notify callback that exchange is done
        callback.done(false);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) CachingMapper(com.thoughtworks.xstream.mapper.CachingMapper) IOException(java.io.IOException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) XStreamException(com.thoughtworks.xstream.XStreamException) IOException(java.io.IOException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Aggregations

XStream (com.thoughtworks.xstream.XStream)1 XStreamException (com.thoughtworks.xstream.XStreamException)1 CachingMapper (com.thoughtworks.xstream.mapper.CachingMapper)1 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)1