Skip to content

Apache Lucene open-source search software

SOLR-1265: Add variable resolving for URLDataSource properties like baseUrl

Erik Hatcher237bfa313798
diff --git a/contrib/dataimporthandler/CHANGES.txt b/contrib/dataimporthandler/CHANGES.txt
index f2926ec000b..e4594f7a6ca 100644
--- a/contrib/dataimporthandler/CHANGES.txt
+++ b/contrib/dataimporthandler/CHANGES.txt
@@ -246,6 +246,8 @@ Bug Fixes
26.SOLR-1146: ConcurrentModificationException in DataImporter.getStatusMessages
(Walter Ferrara, Noble Paul via shalin)
+
+
Documentation
----------------------
@@ -289,6 +291,8 @@ Other
Introduced a DocWrapper which takes care of maintaining document level session variables.
(Noble Paul, shalin)
+10.SOLR-1265: Add variable resolving for URLDataSource properties like baseUrl. (Chris Eldredge via ehatcher)
+
================== Release 1.3.0 20080915 ==================
Status
diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/URLDataSource.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/URLDataSource.java
index 7c89b9ddb37..9aec3295bdb 100644
--- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/URLDataSource.java
+++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/URLDataSource.java
@@ -49,15 +49,22 @@ public class URLDataSource extends DataSource<Reader> {
private int readTimeout = READ_TIMEOUT;
+ private Context context;
+
+ private Properties initProps;
+
public URLDataSource() {
}
public void init(Context context, Properties initProps) {
- baseUrl = initProps.getProperty(BASE_URL);
- if (initProps.get(ENCODING) != null)
- encoding = initProps.getProperty(ENCODING);
- String cTimeout = initProps.getProperty(CONNECTION_TIMEOUT_FIELD_NAME);
- String rTimeout = initProps.getProperty(READ_TIMEOUT_FIELD_NAME);
+ this.context = context;
+ this.initProps = initProps;
+
+ baseUrl = getInitPropWithReplacements(BASE_URL);
+ if (getInitPropWithReplacements(ENCODING) != null)
+ encoding = getInitPropWithReplacements(ENCODING);
+ String cTimeout = getInitPropWithReplacements(CONNECTION_TIMEOUT_FIELD_NAME);
+ String rTimeout = getInitPropWithReplacements(READ_TIMEOUT_FIELD_NAME);
if (cTimeout != null) {
try {
connectionTimeout = Integer.parseInt(cTimeout);
@@ -72,7 +79,6 @@ public class URLDataSource extends DataSource<Reader> {
LOG.warn("Invalid read timeout: " + rTimeout);
}
}
-
}
public Reader getData(String query) {
@@ -111,6 +117,18 @@ public class URLDataSource extends DataSource<Reader> {
public void close() {
}
+ public String getBaseUrl() {
+ return baseUrl;
+ }
+
+ private String getInitPropWithReplacements(String propertyName) {
+ final String expr = initProps.getProperty(propertyName);
+ if (expr == null) {
+ return null;
+ }
+ return context.getVariableResolver().replaceTokens(expr);
+ }
+
private static final Pattern URIMETHOD = Pattern.compile("\\w{3,}:/");
private static final Pattern CHARSET_PATTERN = Pattern.compile(".*?charset=(.*)$", Pattern.CASE_INSENSITIVE);
diff --git a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestURLDataSource.java b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestURLDataSource.java
new file mode 100644
index 00000000000..e76648969b8
--- /dev/null
+++ b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestURLDataSource.java
@@ -0,0 +1,47 @@
+/**
+ * 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.
+ */
+package org.apache.solr.handler.dataimport;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class TestURLDataSource {
+ private List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
+ private URLDataSource dataSource = new URLDataSource();
+ private VariableResolverImpl variableResolver = new VariableResolverImpl();
+ private Context context = AbstractDataImportHandlerTest.getContext(null, variableResolver,
+ dataSource, Context.FULL_DUMP, fields, null);
+ private Properties initProps = new Properties();
+
+ @Test
+ public void substitutionsOnBaseUrl() throws Exception {
+ String url = "http://example.com/";
+
+ variableResolver.addNamespace("dataimporter.request", Collections.<String,Object>singletonMap("baseurl", url));
+
+ initProps.setProperty(URLDataSource.BASE_URL, "${dataimporter.request.baseurl}");
+ dataSource.init(context, initProps);
+ Assert.assertEquals(url, dataSource.getBaseUrl());
+ }
+}