Search in sources :

Example 6 with XmlAgentException

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;
}
Also used : DocumentElement(com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) DocumentContainerElement(com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentContainerElement) JAXBElement(javax.xml.bind.JAXBElement)

Example 7 with XmlAgentException

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;
}
Also used : IndicatorsElement(com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorsElement) DataFailureException(com.hack23.cia.service.external.worldbank.api.DataFailureException) ArrayList(java.util.ArrayList) IndicatorElement(com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorElement) XmlAgentException(com.hack23.cia.service.external.common.api.XmlAgentException)

Example 8 with XmlAgentException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) XmlAgentException(com.hack23.cia.service.external.common.api.XmlAgentException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JDOMSource(org.jdom2.transform.JDOMSource)

Example 9 with XmlAgentException

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) XmlAgentException(com.hack23.cia.service.external.common.api.XmlAgentException) URL(java.net.URL)

Aggregations

XmlAgentException (com.hack23.cia.service.external.common.api.XmlAgentException)4 SimpleXml (com.hack23.cia.service.external.common.impl.test.SimpleXml)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)3 DocumentContainerElement (com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentContainerElement)2 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 JAXBElement (javax.xml.bind.JAXBElement)2 DocumentElement (com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement)1 BallotContainer (com.hack23.cia.model.external.riksdagen.votering.impl.BallotContainer)1 VoteData (com.hack23.cia.model.external.riksdagen.votering.impl.VoteData)1 VoteDataDto (com.hack23.cia.model.external.riksdagen.votering.impl.VoteDataDto)1 VoteDataEmbeddedId (com.hack23.cia.model.external.riksdagen.votering.impl.VoteDataEmbeddedId)1 IndicatorElement (com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorElement)1 IndicatorsElement (com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorsElement)1 DataFailureException (com.hack23.cia.service.external.riksdagen.api.DataFailureException)1 DataFailureException (com.hack23.cia.service.external.worldbank.api.DataFailureException)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1