use of org.qi4j.library.rest.client.spi.ResponseReader in project qi4j-sdk by Qi4j.
the class RssReaderTest method testReadRssFeed.
@Test
public void testReadRssFeed() {
Client client = new Client(Protocol.HTTPS);
Reference ref = new Reference("https://github.com/Qi4j/qi4j-sdk/commits/develop.atom");
ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
contextResourceClientFactory.registerResponseReader(new ResponseReader() {
@Override
public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
if (resultType.equals(Document.class)) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(false);
return documentBuilderFactory.newDocumentBuilder().parse(response.getEntity().getStream());
} catch (Exception e) {
throw new ResourceException(e);
}
}
return null;
}
});
contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {
@Override
public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
System.out.println(">> REFRESH on recoverable error: " + response.getStatus());
return refresh();
}
}));
crc = contextResourceClientFactory.newClient(ref);
crc.onResource(new ResultHandler<Document>() {
Iterator<Node> itemNodes;
@Override
public HandlerCommand handleResult(Document result, ContextResourceClient client) {
try {
final XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("== " + xPath.evaluate("feed/title", result) + " ==");
final NodeList nodes = (NodeList) xPath.evaluate("feed/entry", result, XPathConstants.NODESET);
List<Node> items = new ArrayList<Node>();
for (int i = 0; i < nodes.getLength(); i++) {
items.add(nodes.item(i));
}
itemNodes = items.iterator();
return processEntry(xPath);
} catch (XPathExpressionException e) {
throw new ResourceException(e);
}
}
private HandlerCommand processEntry(final XPath xPath) throws XPathExpressionException {
if (!itemNodes.hasNext()) {
return null;
}
Node item = itemNodes.next();
String title = xPath.evaluate("title", item);
String detailUrl = xPath.evaluate("link/@href", item);
System.out.println("-- " + title + " --");
System.out.println("-- " + detailUrl + " --");
return processEntry(xPath);
}
});
crc.start();
}
use of org.qi4j.library.rest.client.spi.ResponseReader in project qi4j-sdk by Qi4j.
the class ResponseReaderDelegator method initialize.
@Override
public void initialize() throws InitializationException {
Logger logger = LoggerFactory.getLogger(getClass());
ResourceBundle defaultResponseReaders = ResourceBundle.getBundle("org.qi4j.library.rest.client.rest-client");
String responseReaderClasses = defaultResponseReaders.getString("responsereaders");
logger.info("Using responsereaders:" + responseReaderClasses);
for (String className : responseReaderClasses.split(",")) {
try {
Class readerClass = module.classLoader().loadClass(className.trim());
ResponseReader reader = (ResponseReader) module.newObject(readerClass);
registerResponseReader(reader);
} catch (ClassNotFoundException e) {
logger.warn("Could not register response reader " + className, e);
}
}
}
Aggregations