You are viewing a plain text version of this content. The canonical link for it is here.
Posted to svn@forrest.apache.org by th...@apache.org on 2007/01/13 23:05:11 UTC

svn commit: r495980 - in /forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr: input.xmap src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java

Author: thorsten
Date: Sat Jan 13 14:05:10 2007
New Revision: 495980

URL: http://svn.apache.org/viewvc?view=rev&rev=495980
Log:
Changing search against solr server to a new generator. Further adding a default query string to the generator so the crawler can build the search result page without addressing a query.

Added:
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java   (with props)
Modified:
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/input.xmap

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/input.xmap
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/input.xmap?view=diff&rev=495980&r1=495979&r2=495980
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/input.xmap (original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/input.xmap Sat Jan 13 14:05:10 2007
@@ -20,6 +20,9 @@
     <map:generators default="file">
       <map:generator name="request"
         src="org.apache.cocoon.generation.RequestGenerator"/>
+       <map:generator name="solr-search"
+        logger="sitemap.generator.searchgenerator"
+        src="org.apache.forrest.solr.client.SolrSearchGenerator" />
     </map:generators>
     <map:transformers default="xslt">
       <map:transformer logger="sitemap.transformer.solr" name="solr" pool-max="16" 
@@ -30,14 +33,11 @@
     <map:pipeline>
       <!-- Will dispatch a query against the solr server -->
       <map:match pattern="solr-search.xml">
-        <map:generate type="request"/>
-        <map:transform src="resources/stylesheets/query-to-cinclude-solr.xsl">
-          <map:parameter name="host" value="{properties:solr.select.url}"/>
-        </map:transform>
-        <map:transform type="cinclude"/>
+        <map:generate type="solr-search">
+          <map:parameter name="destinationUrl" value="{properties:solr.select.url}"/>
+        </map:generate>
         <map:transform type="solr"/>
         <map:transform src="resources/stylesheets/solrQueryResult-to-xdocs.xsl">
-          <map:parameter name="command" value="{1}"/>
           <map:parameter name="searchForm" value="{request:servletPath}"/>
         </map:transform>
         <map:serialize type="xml"/>

Added: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java?view=auto&rev=495980
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java (added)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java Sat Jan 13 14:05:10 2007
@@ -0,0 +1,91 @@
+package org.apache.forrest.solr.client;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.generation.ServiceableGenerator;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.excalibur.xml.sax.SAXParser;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+public class SolrSearchGenerator extends ServiceableGenerator {
+    private static final String QUERY_PARAM = "q";
+
+    public static final String AGENT = "forrest[" + SolrSearchGenerator.class.getName() + "]";
+    
+    private static final String DESTINATION_URL = "destinationUrl";
+
+    private static final String SOLR_UPDATE_URL = "http://localhost:8983/solr/select";
+
+    private SAXParser parser;
+
+    private String destination;
+
+    private HttpClient client;
+
+    private Request request;
+
+    private String query;
+
+    private Map map;
+
+    private int statusCode;
+
+
+    public void setup(SourceResolver resolver, Map objectModel, String src,
+            Parameters par) throws ProcessingException, SAXException, IOException {
+        super.setup(resolver, objectModel, src, par);
+        this.request = ObjectModelHelper.getRequest(objectModel);
+        destination = par.getParameter(DESTINATION_URL, SOLR_UPDATE_URL);
+        query = request.getParameter(QUERY_PARAM);
+        if (null== query){
+            query = "DEFAULT_QUERY_STRING";
+        }
+        map = request.getParameters();
+    }
+
+    public void generate() throws IOException, SAXException, ProcessingException {
+        client = new HttpClient();
+        PostMethod filePost = preparePost();
+        statusCode = client.executeMethod(filePost);
+        try {
+            parser = (SAXParser) manager.lookup(SAXParser.ROLE);
+            parser.parse(new InputSource(filePost.getResponseBodyAsStream()),contentHandler);
+        } catch (ServiceException e) {
+            throw new ProcessingException("parser lookup faild.");
+        }
+        
+    }
+
+    private PostMethod preparePost() {
+        PostMethod filePost = new PostMethod(destination);
+        filePost.addRequestHeader("User-Agent", AGENT);
+        Iterator keys = map.keySet().iterator();
+        HashSet set = new HashSet();
+        while (keys.hasNext()) {
+            String element = (String) keys.next();
+            if(!QUERY_PARAM.equals(element)){
+                String value = (String) map.get(element);
+                set.add(new NameValuePair(element,value));
+            }
+        }
+        //make sure we send the query (even if null) to get a response
+        set.add(new NameValuePair(QUERY_PARAM,query));
+        for (Iterator iter = set.iterator(); iter.hasNext();) {
+            filePost.addParameter((NameValuePair) iter.next());
+        }
+        return filePost;
+    }
+
+}

Propchange: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.output.solr/src/java/org/apache/forrest/solr/client/SolrSearchGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native