Skip to content

Apache Lucene open-source search software

SOLR-2230: fix CommonsHttpSolrServer so that multiple addFile calls work

Chris M. Hostetterdd55ad439780
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index dfb81fa8a5f..e0da92c0478 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -306,6 +306,10 @@ Bug Fixes
bigger then the max int/float were used.
(Erbi Hanka, hossman)
+* SOLR-2230: CommonsHttpSolrServer.addFile could not be used to send
+ multiple files in a single request.
+ (Stephan Günther, hossman)
+
Other Changes
----------------------
diff --git a/solr/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java b/solr/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java
index 2f1c19a6c5b..5a928857f5f 100644
--- a/solr/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java
+++ b/solr/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java
@@ -32,11 +32,11 @@ import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.Part;
-import org.apache.commons.httpclient.methods.multipart.PartBase;
+import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
-import org.apache.commons.io.IOUtils;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
@@ -324,25 +324,24 @@ public class CommonsHttpSolrServer extends SolrServer
final ContentStream c = content;
String charSet = null;
- String transferEncoding = null;
- parts.add(new PartBase(c.getName(), c.getContentType(),
- charSet, transferEncoding) {
+ PartSource source = new PartSource() {
@Override
- protected long lengthOfData() throws IOException {
+ public long getLength() {
return c.getSize();
}
+
+ public String getFileName() {
+ return c.getName();
+ }
@Override
- protected void sendData(OutputStream out)
- throws IOException {
- InputStream in = c.getStream();
- try {
- IOUtils.copy(in, out);
- } finally {
- in.close();
- }
+ public InputStream createInputStream() throws IOException {
+ return c.getStream();
}
- });
+ };
+
+ parts.add(new FilePart(c.getName(), source,
+ c.getContentType(), charSet));
}
}
if (parts.size() > 0) {
diff --git a/solr/src/test-files/docs1.xml b/solr/src/test-files/docs1.xml
new file mode 100644
index 00000000000..3c5448d9dcf
--- /dev/null
+++ b/solr/src/test-files/docs1.xml
@@ -0,0 +1,56 @@
+<!--
+ 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.
+-->
+
+<add>
+<doc>
+ <field name="id">SP2514N</field>
+ <field name="name">Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133</field>
+ <field name="manu">Samsung Electronics Co. Ltd.</field>
+ <!-- Join -->
+ <field name="manu_id_s">samsung</field>
+ <field name="cat">electronics</field>
+ <field name="cat">hard drive</field>
+ <field name="features">7200RPM, 8MB cache, IDE Ultra ATA-133</field>
+ <field name="features">NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor</field>
+ <field name="price">92</field>
+ <field name="popularity">6</field>
+ <field name="inStock">true</field>
+ <field name="manufacturedate_dt">2006-02-13T15:26:37Z</field>
+ <!-- Near Oklahoma city -->
+ <field name="store">35.0752,-97.032</field>
+</doc>
+
+<doc>
+ <field name="id">6H500F0</field>
+ <field name="name">Maxtor DiamondMax 11 - hard drive - 500 GB - SATA-300</field>
+ <field name="manu">Maxtor Corp.</field>
+ <!-- Join -->
+ <field name="manu_id_s">maxtor</field>
+ <field name="cat">electronics</field>
+ <field name="cat">hard drive</field>
+ <field name="features">SATA 3.0Gb/s, NCQ</field>
+ <field name="features">8.5ms seek</field>
+ <field name="features">16MB cache</field>
+ <field name="price">350</field>
+ <field name="popularity">6</field>
+ <field name="inStock">true</field>
+ <!-- Buffalo store -->
+ <field name="store">45.17614,-93.87341</field>
+ <field name="manufacturedate_dt">2006-02-13T15:26:37Z</field>
+</doc>
+</add>
+
diff --git a/solr/src/test-files/docs2.xml b/solr/src/test-files/docs2.xml
new file mode 100644
index 00000000000..0b89d6785c2
--- /dev/null
+++ b/solr/src/test-files/docs2.xml
@@ -0,0 +1,77 @@
+<!--
+ 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.
+-->
+
+<add>
+<doc>
+ <field name="id">TWINX2048-3200PRO</field>
+ <field name="name">CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</field>
+ <field name="manu">Corsair Microsystems Inc.</field>
+ <!-- Join -->
+ <field name="manu_id_s">corsair</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="features">CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader</field>
+ <field name="price">185</field>
+ <field name="popularity">5</field>
+ <field name="inStock">true</field>
+ <!-- San Francisco store -->
+ <field name="store">37.7752,-122.4232</field>
+ <field name="manufacturedate_dt">2006-02-13T15:26:37Z</field>
+
+ <!-- a field for testing payload tagged text via DelimitedPayloadTokenFilter -->
+ <field name="payloads">electronics|6.0 memory|3.0</field>
+</doc>
+
+<doc>
+ <field name="id">VS1GB400C3</field>
+ <field name="name">CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail</field>
+ <field name="manu">Corsair Microsystems Inc.</field>
+ <!-- Join -->
+ <field name="manu_id_s">corsair</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="price">74.99</field>
+ <field name="popularity">7</field>
+ <field name="inStock">true</field>
+ <!-- Dodge City store -->
+ <field name="store">37.7752,-100.0232</field>
+ <field name="manufacturedate_dt">2006-02-13T15:26:37Z</field>
+
+ <field name="payloads">electronics|4.0 memory|2.0</field>
+</doc>
+
+<doc>
+ <field name="id">VDBDB1A16</field>
+ <field name="name">A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM</field>
+ <field name="manu">A-DATA Technology Inc.</field>
+ <!-- Join -->
+ <field name="manu_id_s">corsair</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="features">CAS latency 3, 2.7v</field>
+ <!-- note: price & popularity is missing on this one -->
+ <field name="popularity">0</field>
+ <field name="inStock">true</field>
+ <!-- Buffalo store -->
+ <field name="store">45.18414,-93.88141</field>
+ <field name="manufacturedate_dt">2006-02-13T15:26:37Z</field>
+
+ <field name="payloads">electronics|0.9 memory|0.1</field>
+</doc>
+
+</add>
+
diff --git a/solr/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
index e19992a4a21..1f28fa120d5 100644
--- a/solr/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
+++ b/solr/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
@@ -446,6 +446,24 @@ abstract public class SolrExampleTests extends SolrJettyTestBase
assertNotNull("Couldn't upload books.csv", result);
rsp = server.query( new SolrQuery( "*:*") );
Assert.assertEquals( 10, rsp.getResults().getNumFound() );
+ }
+
+ @Test
+ public void testMultiContentStreamRequest() throws Exception {
+ SolrServer server = getSolrServer();
+ server.deleteByQuery( "*:*" );// delete everything!
+ server.commit();
+ QueryResponse rsp = server.query( new SolrQuery( "*:*") );
+ Assert.assertEquals( 0, rsp.getResults().getNumFound() );
+
+ ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
+ up.addFile(getFile("docs1.xml")); // 2
+ up.addFile(getFile("docs2.xml")); // 3
+ up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
+ NamedList<Object> result = server.request(up);
+ assertNotNull("Couldn't upload xml files", result);
+ rsp = server.query( new SolrQuery( "*:*") );
+ Assert.assertEquals( 5 , rsp.getResults().getNumFound() );
}