WorldbankCountryApiImpl.java

  1. /*
  2.  * Copyright 2010 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.service.external.worldbank.impl;

  20. import java.util.List;

  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25. import org.springframework.oxm.Unmarshaller;
  26. import org.springframework.stereotype.Component;

  27. import com.hack23.cia.model.external.worldbank.countries.impl.CountriesElement;
  28. import com.hack23.cia.model.external.worldbank.countries.impl.CountryElement;
  29. import com.hack23.cia.service.external.common.api.XmlAgent;
  30. import com.hack23.cia.service.external.common.api.XmlAgentException;
  31. import com.hack23.cia.service.external.worldbank.api.DataFailureException;
  32. import com.hack23.cia.service.external.worldbank.api.WorldBankCountryApi;

  33. /**
  34.  * The Class WorldbankCountryApiImpl.
  35.  */
  36. @Component
  37. final class WorldbankCountryApiImpl extends BaseWorldBankApiImpl implements WorldBankCountryApi {

  38.     /** The Constant LOGGER. */
  39.     private static final Logger LOGGER = LoggerFactory.getLogger(WorldbankCountryApiImpl.class);

  40.     /** The Constant COUNTRIES. */
  41.     private static final String COUNTRIES = "https://api.worldbank.org/country?per_page=300";

  42.     /** The Constant PROBLEM_GETTING_WORLDBANK_COUNTRY_LIST. */
  43.     private static final String PROBLEM_GETTING_WORLDBANK_COUNTRY_LIST = "Problem getting worldbank country list";

  44.     /**
  45.      * The Constant
  46.      * XMLNS_WB_HTTP_COUNTRIES_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
  47.      */
  48.     private static final String XMLNS_WB_HTTP_COUNTRIES_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "xmlns:wb=\"http://countries.worldbank.external.model.cia.hack23.com/impl\"";

  49.     /** The countries unmarshaller. */
  50.     @Autowired
  51.     @Qualifier("worldbankOrgCountriesMarshaller")
  52.     private Unmarshaller countriesUnmarshaller;

  53.     /**
  54.      * Instantiates a new worldbank country api impl.
  55.      */
  56.     @Autowired
  57.     public WorldbankCountryApiImpl(final XmlAgent xmlAgent) {
  58.         super(xmlAgent);
  59.     }

  60.     @Override
  61.     public List<CountryElement> getCountries() throws DataFailureException {
  62.         try {
  63.             return ((CountriesElement) getXmlAgent().unmarshallXml(countriesUnmarshaller, COUNTRIES, null,
  64.                     XMLNS_WB_HTTP_WWW_WORLDBANK_ORG,
  65.                     XMLNS_WB_HTTP_COUNTRIES_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL)).getCountry();
  66.         } catch (final XmlAgentException e) {
  67.             LOGGER.warn(PROBLEM_GETTING_WORLDBANK_COUNTRY_LIST);
  68.             throw new DataFailureException(e);
  69.         }
  70.     }

  71. }