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);
}
}
Aggregations