Skip to content

Apache Lucene open-source search software

SOLR-4515: CurrencyField's OpenExchangeRatesOrgProvider now requires a ratesFileLocation init param, since the previous global default no longer works

Chris M. Hostetterdaa66dd5d0d2
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 2e81f1fac9c..8a9727b68a6 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -195,6 +195,10 @@ Bug Fixes
well as when nodes may switch master/slave roles.
(Mark Miller, Raúl Grande)
+* SOLR-4515: CurrencyField's OpenExchangeRatesOrgProvider now requires
+ a ratesFileLocation init param, since the previous global default
+ no longer works (hossman)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
index a270299bf37..2d473804a54 100644
--- a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
+++ b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
@@ -130,7 +130,7 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa
provider = c.newInstance();
provider.init(args);
} catch (Exception e) {
- throw new SolrException(ErrorCode.BAD_REQUEST, "Error instansiating exhange rate provider "+exchangeRateProviderClass+". Please check your FieldType configuration", e);
+ throw new SolrException(ErrorCode.BAD_REQUEST, "Error instantiating exhange rate provider "+exchangeRateProviderClass+": " + e.getMessage(), e);
}
}
diff --git a/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java b/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
index d108b15b904..bccc4934808 100644
--- a/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
+++ b/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
@@ -33,19 +33,29 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Exchange Rates Provider for {@link CurrencyField} implementing the freely available
- * exchange rates from openexchangerates.org
+ * <p>
+ * Exchange Rates Provider for {@link CurrencyField} capable of fetching &amp
+ * parsing the freely available exchange rates from openexchangerates.org
+ * </p>
+ * <p>
+ * Configuration Options:
+ * </p>
+ * <ul>
+ * <li><code>ratesFileLocation</code> - A file path or absolute URL specifying the JSON data to load (mandatory)</li>
+ * <li><coderefreshInterval></code> - How frequently (in minutes) to reload the exchange rate data (default: 1440)</li>
+ * </ul>
* <p>
* <b>Disclaimer:</b> This data is collected from various providers and provided free of charge
* for informational purposes only, with no guarantee whatsoever of accuracy, validity,
* availability or fitness for any purpose; use at your own risk. Other than that - have
* fun, and please share/watch/fork if you think data like this should be free!
+ * </p>
+ * @see <a href="https://openexchangerates.org/documentation">openexchangerates.org JSON Data Format</a>
*/
public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
public static Logger log = LoggerFactory.getLogger(OpenExchangeRatesOrgProvider.class);
protected static final String PARAM_RATES_FILE_LOCATION = "ratesFileLocation";
protected static final String PARAM_REFRESH_INTERVAL = "refreshInterval";
- protected static final String DEFAULT_RATES_FILE_LOCATION = "http://openexchangerates.org/latest.json";
protected static final String DEFAULT_REFRESH_INTERVAL = "1440";
protected String ratesFileLocation;
@@ -145,7 +155,10 @@ public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
@Override
public void init(Map<String,String> params) throws SolrException {
try {
- ratesFileLocation = getParam(params.get(PARAM_RATES_FILE_LOCATION), DEFAULT_RATES_FILE_LOCATION);
+ ratesFileLocation = params.get(PARAM_RATES_FILE_LOCATION);
+ if (null == ratesFileLocation) {
+ throw new SolrException(ErrorCode.SERVER_ERROR, "Init param must be specified: " + PARAM_RATES_FILE_LOCATION);
+ }
refreshInterval = Integer.parseInt(getParam(params.get(PARAM_REFRESH_INTERVAL), DEFAULT_REFRESH_INTERVAL));
// Force a refresh interval of minimum one hour, since the API does not offer better resolution
if (refreshInterval < 60) {
@@ -153,8 +166,11 @@ public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
log.warn("Specified refreshInterval was too small. Setting to 60 minutes which is the update rate of openexchangerates.org");
}
log.info("Initialized with rates="+ratesFileLocation+", refreshInterval="+refreshInterval+".");
- } catch (Exception e) {
- throw new SolrException(ErrorCode.BAD_REQUEST, "Error initializing", e);
+ } catch (SolrException e1) {
+ throw e1;
+ } catch (Exception e2) {
+ throw new SolrException(ErrorCode.SERVER_ERROR, "Error initializing: " +
+ e2.getMessage(), e2);
} finally {
// Removing config params custom to us
params.remove(PARAM_RATES_FILE_LOCATION);
diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-oer-norates.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-oer-norates.xml
new file mode 100644
index 00000000000..bd23933b270
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-oer-norates.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<schema name="bad-schema-currency-ft-oer-norates" version="1.4">
+ <types>
+ <fieldType name="string" class="solr.StrField" multiValued="true"/>
+ <!-- BEGIN BAD STUFF: multiValued -->
+ <fieldType name="currency"
+ class="solr.CurrencyField"
+ providerClass="solr.OpenExchangeRatesOrgProvider"
+ multiValued="false" />
+ </types>
+
+ <fields>
+ <field name="id" type="string" indexed="true" stored="true" multiValued="false"/>
+ <field name="money" type="currency" indexed="true" stored="true" />
+ </fields>
+
+ <defaultSearchField>id</defaultSearchField>
+ <uniqueKey>id</uniqueKey>
+
+</schema>
diff --git a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
index 63f43651205..f8ef4ca1a7a 100644
--- a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
@@ -73,6 +73,11 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase {
"Fields can not be multiValued: *_c");
}
+ public void testCurrencyOERNoRates() throws Exception {
+ doTest("bad-schema-currency-ft-oer-norates.xml",
+ "ratesFileLocation");
+ }
+
public void testPerFieldtypeSimButNoSchemaSimFactory() throws Exception {
doTest("bad-schema-sim-global-vs-ft-mismatch.xml", "global similarity does not support it");
}
diff --git a/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java b/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
index 4167099fb7c..5e1015eaa3f 100644
--- a/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
@@ -32,8 +32,7 @@ import java.util.Map;
public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
OpenExchangeRatesOrgProvider oerp;
ResourceLoader loader;
- private final Map<String,String> emptyParams = new HashMap<String,String>();
- private Map<String,String> mockParams;
+ private final Map<String,String> mockParams = new HashMap<String,String>();
@Override
@Before
@@ -42,24 +41,27 @@ public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
("USD", "EUR", "MXN", "GBP", "JPY");
super.setUp();
- mockParams = new HashMap<String,String>();;
- mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "open-exchange-rates.json");
+ mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION,
+ "open-exchange-rates.json");
oerp = new OpenExchangeRatesOrgProvider();
loader = new SolrResourceLoader("solr/collection1");
}
@Test
public void testInit() throws Exception {
- oerp.init(emptyParams);
- assertTrue("Wrong default url", oerp.ratesFileLocation.toString().equals("http://openexchangerates.org/latest.json"));
- assertTrue("Wrong default interval", oerp.refreshInterval == 1440);
+ oerp.init(mockParams);
+ assertEquals("Wrong url",
+ "open-exchange-rates.json", oerp.ratesFileLocation);
+ assertEquals("Wrong default interval", 1440, oerp.refreshInterval);
Map<String,String> params = new HashMap<String,String>();
- params.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "http://foo.bar/baz");
+ params.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION,
+ "http://foo.bar/baz");
params.put(OpenExchangeRatesOrgProvider.PARAM_REFRESH_INTERVAL, "100");
oerp.init(params);
- assertTrue("Wrong param set url", oerp.ratesFileLocation.equals("http://foo.bar/baz"));
- assertTrue("Wrong param interval", oerp.refreshInterval == 100);
+ assertEquals("Wrong param set url",
+ "http://foo.bar/baz", oerp.ratesFileLocation);
+ assertEquals("Wrong param interval", 100, oerp.refreshInterval);
}
@Test