You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by gc...@apache.org on 2014/09/27 00:05:36 UTC

svn commit: r1627899 - in /lucene/dev/branches/branch_5x/solr: ./ solrj/src/java/org/apache/solr/client/solrj/ solrj/src/java/org/apache/solr/client/solrj/impl/ solrj/src/test/org/apache/solr/client/solrj/impl/

Author: gchanan
Date: Fri Sep 26 22:05:35 2014
New Revision: 1627899

URL: http://svn.apache.org/r1627899
Log:
SOLR-6565: SolrRequest support for query params

Modified:
    lucene/dev/branches/branch_5x/solr/CHANGES.txt
    lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
    lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
    lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java

Modified: lucene/dev/branches/branch_5x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/CHANGES.txt?rev=1627899&r1=1627898&r2=1627899&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/solr/CHANGES.txt Fri Sep 26 22:05:35 2014
@@ -118,6 +118,8 @@ New Features
 * SOLR-5986: Don't allow runaway queries from harming Solr cluster health or search 
   performance (Anshum Gupta, Steve Rowe, Robert Muir)
 
+* SOLR-6565: SolrRequest support for query params (Gregory Chanan)
+
 Bug Fixes
 ----------------------
 

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java?rev=1627899&r1=1627898&r2=1627899&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java Fri Sep 26 22:05:35 2014
@@ -20,6 +20,7 @@ package org.apache.solr.client.solrj;
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.Collection;
+import java.util.Set;
 
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.ContentStream;
@@ -42,6 +43,7 @@ public abstract class SolrRequest implem
 
   private ResponseParser responseParser;
   private StreamingResponseCallback callback;
+  private Set<String> queryParams;
   
   //---------------------------------------------------------
   //---------------------------------------------------------
@@ -93,7 +95,18 @@ public abstract class SolrRequest implem
   public void setStreamingResponseCallback(StreamingResponseCallback callback) {
     this.callback = callback;
   }
-  
+
+  /**
+   * Parameter keys that are sent via the query string
+   */
+  public Set<String> getQueryParams() {
+    return this.queryParams;
+  }
+
+  public void setQueryParams(Set<String> queryParams) {
+    this.queryParams = queryParams;
+  }
+
   public abstract SolrParams getParams();
   public abstract Collection<ContentStream> getContentStreams() throws IOException;
   public abstract SolrResponse process( SolrServer server ) throws SolrServerException, IOException;

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java?rev=1627899&r1=1627898&r2=1627899&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java Fri Sep 26 22:05:35 2014
@@ -178,8 +178,11 @@ public class HttpSolrServer extends Solr
   }
 
   /**
-   * Expert Method.
+   * Expert Method
    * @param queryParams set of param keys to only send via the query string
+   * Note that the param will be sent as a query string if the key is part
+   * of this Set or the SolrRequest's query params.
+   * @see org.apache.solr.client.solrj.SolrRequest#getQueryParams
    */
   public void setQueryParams(Set<String> queryParams) {
     this.queryParams = queryParams;
@@ -254,7 +257,24 @@ public class HttpSolrServer extends Solr
     mrr.httpUriRequest = method;
     return mrr;
   }
-  
+
+  protected ModifiableSolrParams calculateQueryParams(Set<String> queryParamNames,
+      ModifiableSolrParams wparams) {
+    ModifiableSolrParams queryModParams = new ModifiableSolrParams();
+    if (queryParamNames != null) {
+      for (String param : queryParamNames) {
+        String[] value = wparams.getParams(param) ;
+        if (value != null) {
+          for (String v : value) {
+            queryModParams.add(param, v);
+          }
+          wparams.remove(param);
+        }
+      }
+    }
+    return queryModParams;
+  }
+
   protected HttpRequestBase createMethod(final SolrRequest request) throws IOException, SolrServerException {
     HttpRequestBase method = null;
     InputStream is = null;
@@ -309,19 +329,11 @@ public class HttpSolrServer extends Solr
             }
             boolean isMultipart = ((this.useMultiPartPost && SolrRequest.METHOD.POST == request.getMethod())
               || ( streams != null && streams.size() > 1 )) && !hasNullStreamName;
-            
-            // only send this list of params as query string params
-            ModifiableSolrParams queryParams = new ModifiableSolrParams();
-            for (String param : this.queryParams) {
-              String[] value = wparams.getParams(param) ;
-              if (value != null) {
-                for (String v : value) {
-                  queryParams.add(param, v);
-                }
-                wparams.remove(param);
-              }
-            }
-            
+
+            // send server list and request list as query string params
+            ModifiableSolrParams queryParams = calculateQueryParams(this.queryParams, wparams);
+            queryParams.add(calculateQueryParams(request.getQueryParams(), wparams));
+
             LinkedList<NameValuePair> postOrPutParams = new LinkedList<>();
             if (streams == null || isMultipart) {
               String fullQueryUrl = url + ClientUtils.toQueryString( queryParams, false );

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java?rev=1627899&r1=1627898&r2=1627899&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java Fri Sep 26 22:05:35 2014
@@ -23,7 +23,10 @@ import java.net.MalformedURLException;
 import java.net.Socket;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -37,6 +40,7 @@ import org.apache.http.client.HttpClient
 import org.apache.http.client.methods.HttpGet;
 import org.apache.solr.SolrJettyTestBase;
 import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrRequest.METHOD;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.request.QueryRequest;
@@ -46,6 +50,7 @@ import org.apache.solr.common.SolrInputD
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrException.ErrorCode;
 import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.util.ExternalPaths;
 import org.apache.solr.util.SSLTestConfig;
@@ -78,12 +83,14 @@ public class BasicHttpSolrServerTest ext
       headers = null;
       parameters = null;
       errorCode = null;
+      queryString = null;
     }
     
     public static Integer errorCode = null;
     public static String lastMethod = null;
     public static HashMap<String,String> headers = null;
     public static Map<String,String[]> parameters = null;
+    public static String queryString = null;
     
     public static void setErrorCode(Integer code) {
       errorCode = code;
@@ -110,6 +117,10 @@ public class BasicHttpSolrServerTest ext
       parameters = req.getParameterMap();
     }
 
+    private void setQueryString(HttpServletRequest req) {
+      queryString = req.getQueryString();
+    }
+
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
@@ -127,6 +138,7 @@ public class BasicHttpSolrServerTest ext
     private void recordRequest(HttpServletRequest req, HttpServletResponse resp) {
       setHeaders(req);
       setParameters(req);
+      setQueryString(req);
       if (null != errorCode) {
         try { 
           resp.sendError(errorCode); 
@@ -553,5 +565,80 @@ public class BasicHttpSolrServerTest ext
     }
     throw new RuntimeException("Could not find unused TCP port.");
   }
-  
+
+  private Set<String> setOf(String... keys) {
+    Set<String> set = new TreeSet<String>();
+    if (keys != null) {
+      for (String k : keys) {
+        set.add(k);
+      }
+    }
+    return set;
+  }
+
+  private void setReqParamsOf(UpdateRequest req, String... keys) {
+    if (keys != null) {
+      for (String k : keys) {
+        req.setParam(k, k+"Value");
+      }
+    }
+  }
+
+  private void verifyServletState(HttpSolrServer server, SolrRequest request) {
+    // check query String
+    Iterator<String> paramNames = request.getParams().getParameterNamesIterator();
+    while (paramNames.hasNext()) {
+      String name = paramNames.next();
+      String [] values = request.getParams().getParams(name);
+      if (values != null) {
+        for (String value : values) {
+          boolean shouldBeInQueryString = server.getQueryParams().contains(name)
+            || (request.getQueryParams() != null && request.getQueryParams().contains(name));
+          assertEquals(shouldBeInQueryString, DebugServlet.queryString.contains(name + "=" + value));
+          // in either case, it should be in the parameters
+          assertNotNull(DebugServlet.parameters.get(name));
+          assertEquals(1, DebugServlet.parameters.get(name).length);
+          assertEquals(value, DebugServlet.parameters.get(name)[0]);
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testQueryString() throws Exception {
+    HttpSolrServer server = new HttpSolrServer(jetty.getBaseUrl().toString() +
+                                               "/debug/foo");
+
+    // test without request query params
+    DebugServlet.clear();
+    server.setQueryParams(setOf("serverOnly"));
+    UpdateRequest req = new UpdateRequest();
+    setReqParamsOf(req, "serverOnly", "notServer");
+    try {
+      server.request(req);
+    } catch (Throwable t) {}
+    verifyServletState(server, req);
+
+    // test without server query params
+    DebugServlet.clear();
+    server.setQueryParams(setOf());
+    req = new UpdateRequest();
+    req.setQueryParams(setOf("requestOnly"));
+    setReqParamsOf(req, "requestOnly", "notRequest");
+    try {
+      server.request(req);
+    } catch (Throwable t) {}
+    verifyServletState(server, req);
+
+    // test with both request and server query params
+    DebugServlet.clear();
+    req = new UpdateRequest();
+    server.setQueryParams(setOf("serverOnly", "both"));
+    req.setQueryParams(setOf("requestOnly", "both"));
+    setReqParamsOf(req, "serverOnly", "requestOnly", "both", "neither");
+     try {
+      server.request(req);
+    } catch (Throwable t) {}
+    verifyServletState(server, req);
+  }
 }