use of com.hack23.cia.service.external.common.api.XmlAgentException in project cia by Hack23.
the class RiksdagenDocumentApiImpl method loadDocumentList.
/**
* Load document list.
*
* @param url
* the url
* @param maxNumberPages
* the max number pages
* @return the list
* @throws Exception
* the exception
*/
private List<DocumentElement> loadDocumentList(final String url, final int maxNumberPages) throws XmlAgentException {
final List<DocumentElement> result = new ArrayList<>();
DocumentContainerElement dokumentLista = ((JAXBElement<DocumentContainerElement>) xmlAgent.unmarshallXml(riksdagenDocumentListMarshaller, url, HTTP_DOKUMENTLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
result.addAll(dokumentLista.getDokument());
final BigInteger pages = dokumentLista.getTotalPages();
for (int i = 1; i < pages.intValue() && i < maxNumberPages; i++) {
dokumentLista = ((JAXBElement<DocumentContainerElement>) xmlAgent.unmarshallXml(riksdagenDocumentListMarshaller, fixBrokenUrl(dokumentLista.getNextPage()), HTTP_DOKUMENTLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
result.addAll(dokumentLista.getDokument());
LOGGER.info(LOADING_DOCUMENTS, result.size(), dokumentLista.getHits());
}
return result;
}
use of com.hack23.cia.service.external.common.api.XmlAgentException in project cia by Hack23.
the class WorldbankIndicatorApiImpl method getIndicators.
/* (non-Javadoc)
* @see com.hack23.cia.service.external.worldbank.api.WorldBankIndicatorApi#getIndicators()
*/
@Override
public List<IndicatorElement> getIndicators() throws DataFailureException {
final List<IndicatorElement> result = new ArrayList<>();
try {
final IndicatorsElement firstPage = (IndicatorsElement) getXmlAgent().unmarshallXml(indicatorsUnmarshaller, INDICATORS, null, XMLNS_WB_HTTP_WWW_WORLDBANK_ORG, XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL);
result.addAll(firstPage.getIndicator());
for (int pageNumber = SECOND_PAGE; pageNumber < firstPage.getPages().intValue(); pageNumber++) {
final IndicatorsElement otherPageResult = (IndicatorsElement) getXmlAgent().unmarshallXml(indicatorsUnmarshaller, INDICATORS + PAGE_NUMBER + pageNumber, null, XMLNS_WB_HTTP_WWW_WORLDBANK_ORG, XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL);
result.addAll(otherPageResult.getIndicator());
}
} catch (final XmlAgentException e) {
LOGGER.warn(PROBLEM_GETTING_WORLDBANK_INDICATOR_LIST);
throw new DataFailureException(e);
}
return result;
}
use of com.hack23.cia.service.external.common.api.XmlAgentException in project cia by Hack23.
the class XmlAgentImpl method unmarshallXml.
@Override
public Object unmarshallXml(final Unmarshaller unmarshaller, final String accessUrl, final String nameSpace, final String replace, final String with) throws XmlAgentException {
try {
LOGGER.info("Calls {}", accessUrl);
final boolean isWeb = accessUrl.toLowerCase(Locale.ENGLISH).startsWith("http://") || accessUrl.toLowerCase(Locale.ENGLISH).startsWith("https://");
String xmlContent;
if (isWeb) {
xmlContent = Request.Get(accessUrl.replace(" ", "")).execute().returnContent().asString(StandardCharsets.UTF_8);
} else {
xmlContent = readInputStream(accessUrl.replace(" ", ""));
}
if (replace != null) {
xmlContent = xmlContent.replace(replace, with);
}
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8));
Source source;
if (nameSpace != null) {
source = setNameSpaceOnXmlStream(byteArrayInputStream, nameSpace);
} else {
source = new StreamSource(byteArrayInputStream);
}
return unmarshaller.unmarshal(source);
} catch (IOException | JDOMException e) {
throw new XmlAgentException(e);
}
}
use of com.hack23.cia.service.external.common.api.XmlAgentException in project cia by Hack23.
the class XmlAgentImpl method retriveContent.
@Override
public String retriveContent(final String accessUrl) throws XmlAgentException {
try {
final URL url = new URL(accessUrl.replace(" ", ""));
final BufferedReader inputStream = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
return readWithStringBuffer(inputStream);
} catch (IOException e) {
throw new XmlAgentException(e);
}
}
Aggregations