mirror of https://github.com/apache/lucene.git
SOLR-1265: Add variable resolving for URLDataSource properties like baseUrl
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@792555 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
511ea76b32
commit
237bfa3137
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue