You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bo...@apache.org on 2012/03/08 20:23:31 UTC

svn commit: r1298525 - in /camel/trunk/components/camel-solr: ./ src/main/java/org/apache/camel/component/solr/ src/test/java/org/apache/camel/component/solr/ src/test/resources/

Author: boday
Date: Thu Mar  8 19:23:31 2012
New Revision: 1298525

URL: http://svn.apache.org/viewvc?rev=1298525&view=rev
Log:
CAMEL-5071 added SolrJ DirectXMLRequest support via the Exchange Body

Modified:
    camel/trunk/components/camel-solr/pom.xml
    camel/trunk/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java
    camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrSpringTest.java
    camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrUpdateTest.java
    camel/trunk/components/camel-solr/src/test/resources/SolrSpringTest-context.xml

Modified: camel/trunk/components/camel-solr/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-solr/pom.xml?rev=1298525&r1=1298524&r2=1298525&view=diff
==============================================================================
--- camel/trunk/components/camel-solr/pom.xml (original)
+++ camel/trunk/components/camel-solr/pom.xml Thu Mar  8 19:23:31 2012
@@ -58,6 +58,12 @@
     <!-- testing -->
     <dependency>
       <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core-xml</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
       <artifactId>camel-test-spring</artifactId>
       <scope>test</scope>
     </dependency>

Modified: camel/trunk/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java?rev=1298525&r1=1298524&r2=1298525&view=diff
==============================================================================
--- camel/trunk/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java (original)
+++ camel/trunk/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java Thu Mar  8 19:23:31 2012
@@ -22,7 +22,9 @@ import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.solr.client.solrj.SolrServer;
 import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
+import org.apache.solr.client.solrj.request.DirectXmlRequest;
 import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrInputDocument;
 
 /**
@@ -72,7 +74,14 @@ public class SolrProducer extends Defaul
 
         Object body = exchange.getIn().getBody();
 
+        boolean hasSolrHeaders = false;
+        Map<String, Object> headers = exchange.getIn().getHeaders();
+        if (headers != null && headers.containsKey(SolrConstants.FIELD + "id")) {
+            hasSolrHeaders = true;
+        }
+
         if (body instanceof File) {
+
             ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
             updateRequest.addFile((File) body);
 
@@ -89,28 +98,54 @@ public class SolrProducer extends Defaul
                 updateRequest.process(solrServer);
             }
 
-        } else {
+        } else if (body instanceof SolrInputDocument) {
 
             UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
+            updateRequest.add((SolrInputDocument) body);
 
-            if (body instanceof SolrInputDocument) {
-                updateRequest.add((SolrInputDocument) body);
+            if (isStreaming) {
+                updateRequest.process(streamingSolrServer);
             } else {
-                SolrInputDocument doc = new SolrInputDocument();
-                for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
-                    if (entry.getKey().startsWith(SolrConstants.FIELD)) {
-                        String fieldName = entry.getKey().substring(SolrConstants.FIELD.length());
-                        doc.setField(fieldName, entry.getValue());
-                    }
+                updateRequest.process(solrServer);
+            }
+
+        } else if (hasSolrHeaders) {
+
+            UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
+
+            SolrInputDocument doc = new SolrInputDocument();
+            for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
+                if (entry.getKey().startsWith(SolrConstants.FIELD)) {
+                    String fieldName = entry.getKey().substring(SolrConstants.FIELD.length());
+                    doc.setField(fieldName, entry.getValue());
                 }
-                updateRequest.add(doc);
             }
+            updateRequest.add(doc);
 
             if (isStreaming) {
                 updateRequest.process(streamingSolrServer);
             } else {
                 updateRequest.process(solrServer);
             }
+
+        } else if (body instanceof String) {
+
+            String bodyAsString = (String) body;
+
+            if (!bodyAsString.startsWith("<add")) {
+                bodyAsString = "<add>" + bodyAsString + "</add>";
+            }
+
+            DirectXmlRequest xmlRequest = new DirectXmlRequest(getRequestHandler(), bodyAsString);
+
+            if (isStreaming) {
+                streamingSolrServer.request(xmlRequest);
+            } else {
+                solrServer.request(xmlRequest);
+            }
+
+        } else {
+            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unable to find data in Exchange to update Solr");
         }
     }
 

Modified: camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrSpringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrSpringTest.java?rev=1298525&r1=1298524&r2=1298525&view=diff
==============================================================================
--- camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrSpringTest.java (original)
+++ camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrSpringTest.java Thu Mar  8 19:23:31 2012
@@ -26,7 +26,9 @@ import org.apache.solr.client.solrj.Solr
 import org.apache.solr.client.solrj.embedded.JettySolrRunner;
 import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
 import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.client.solrj.util.ClientUtils;
 import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrInputDocument;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -45,6 +47,9 @@ public class SolrSpringTest extends Abst
     private static JettySolrRunner solrRunner;
     private static CommonsHttpSolrServer solrServer;
 
+    @Produce(uri = "direct:direct-xml-start")
+    protected ProducerTemplate directXmlRoute;
+
     @Produce(uri = "direct:xml-start")
     protected ProducerTemplate xmlRoute;
 
@@ -57,6 +62,20 @@ public class SolrSpringTest extends Abst
     @Produce(uri = "direct:pdf-start-streaming")
     protected ProducerTemplate pdfRouteStreaming;
 
+
+    @DirtiesContext
+    @Test
+    public void endToEndIndexDirectXML() throws Exception {
+        SolrInputDocument doc = new SolrInputDocument();
+        doc.addField("id", "MA147LL/A", 1.0f);
+        String docAsXml = ClientUtils.toXML(doc);
+        directXmlRoute.sendBody(docAsXml);
+
+        QueryResponse response = executeSolrQuery("id:MA147LL/A");
+        assertEquals(0, response.getStatus());
+        assertEquals(1, response.getResults().getNumFound());
+    }
+    
     @DirtiesContext
     @Test
     public void endToEndIndexXMLDocuments() throws Exception {

Modified: camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrUpdateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrUpdateTest.java?rev=1298525&r1=1298524&r2=1298525&view=diff
==============================================================================
--- camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrUpdateTest.java (original)
+++ camel/trunk/components/camel-solr/src/test/java/org/apache/camel/component/solr/SolrUpdateTest.java Thu Mar  8 19:23:31 2012
@@ -21,6 +21,7 @@ import java.util.Arrays;
 import java.util.List;
 import org.apache.camel.Exchange;
 import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.client.solrj.util.ClientUtils;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrInputDocument;
@@ -40,6 +41,34 @@ public class SolrUpdateTest extends Solr
     }
 
     @Test
+    public void testInsertSolrInputDocumentAsXMLWithoutAddRoot() throws Exception {
+
+        SolrInputDocument doc = new SolrInputDocument();
+        doc.addField("id", "MA147LL/A", 1.0f);
+        String docAsXml = ClientUtils.toXML(doc);
+        template.sendBodyAndHeader("direct:start", docAsXml, SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
+        solrCommit();
+
+        QueryResponse response = executeSolrQuery("id:MA147LL/A");
+        assertEquals(0, response.getStatus());
+        assertEquals(1, response.getResults().getNumFound());
+    }
+
+    @Test
+    public void testInsertSolrInputDocumentAsXMLWithAddRoot() throws Exception {
+
+        SolrInputDocument doc = new SolrInputDocument();
+        doc.addField("id", "MA147LL/A", 1.0f);
+        String docAsXml = "<add>" + ClientUtils.toXML(doc) + "</add>";
+        template.sendBodyAndHeader("direct:start", docAsXml, SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
+        solrCommit();
+
+        QueryResponse response = executeSolrQuery("id:MA147LL/A");
+        assertEquals(0, response.getStatus());
+        assertEquals(1, response.getResults().getNumFound());
+    }
+
+    @Test
     public void testInsertSolrInputDocument() throws Exception {
 
         SolrInputDocument doc = new SolrInputDocument();

Modified: camel/trunk/components/camel-solr/src/test/resources/SolrSpringTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-solr/src/test/resources/SolrSpringTest-context.xml?rev=1298525&r1=1298524&r2=1298525&view=diff
==============================================================================
--- camel/trunk/components/camel-solr/src/test/resources/SolrSpringTest-context.xml (original)
+++ camel/trunk/components/camel-solr/src/test/resources/SolrSpringTest-context.xml Thu Mar  8 19:23:31 2012
@@ -7,6 +7,18 @@
     <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"/>
 
     <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <route id="DirectXMLRoute">
+            <from uri="direct:direct-xml-start" />
+            <setHeader headerName="SolrOperation">
+                <constant>INSERT</constant>
+            </setHeader>
+            <to uri="solr://localhost:{{SolrServer.Port}}/solr"/>
+            <setHeader headerName="SolrOperation">
+                <constant>COMMIT</constant>
+            </setHeader>
+            <to uri="solr://localhost:{{SolrServer.Port}}/solr"/>
+        </route>
+
         <route id="XMLRoute">
             <from uri="direct:xml-start" />
             <split>