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/25 08:04:05 UTC

svn commit: r1627460 - in /lucene/dev/trunk/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/ test-framework/src/java/org/apache/solr/util/

Author: gchanan
Date: Thu Sep 25 06:04:05 2014
New Revision: 1627460

URL: http://svn.apache.org/r1627460
Log:
SOLR-6543: Give HttpSolrServer the ability to send PUT requests

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java
    lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1627460&r1=1627459&r2=1627460&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Thu Sep 25 06:04:05 2014
@@ -138,6 +138,8 @@ New Features
 * SOLR-6485: ReplicationHandler should have an option to throttle the speed of
   replication (Varun Thacker, NOble Paul)
 
+* SOLR-6543: Give HttpSolrServer the ability to send PUT requests (Gregory Chanan)
+
 Bug Fixes
 ----------------------
 

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java?rev=1627460&r1=1627459&r2=1627460&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java Thu Sep 25 06:04:05 2014
@@ -33,7 +33,8 @@ public abstract class SolrRequest implem
 {
   public enum METHOD {
     GET,
-    POST
+    POST,
+    PUT
   };
 
   private METHOD method = METHOD.GET;

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java?rev=1627460&r1=1627459&r2=1627460&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java Thu Sep 25 06:04:05 2014
@@ -42,8 +42,10 @@ import org.apache.http.NameValuePair;
 import org.apache.http.NoHttpResponseException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.params.ClientPNames;
@@ -293,7 +295,7 @@ public class HttpSolrServer extends Solr
             }
             method = new HttpGet( baseUrl + path + ClientUtils.toQueryString( wparams, false ) );
           }
-          else if( SolrRequest.METHOD.POST == request.getMethod() ) {
+          else if( SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod() ) {
 
             String url = baseUrl + path;
             boolean hasNullStreamName = false;
@@ -305,7 +307,8 @@ public class HttpSolrServer extends Solr
                 }
               }
             }
-            boolean isMultipart = (this.useMultiPartPost || ( streams != null && streams.size() > 1 )) && !hasNullStreamName;
+            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();
@@ -319,11 +322,13 @@ public class HttpSolrServer extends Solr
               }
             }
             
-            LinkedList<NameValuePair> postParams = new LinkedList<>();
+            LinkedList<NameValuePair> postOrPutParams = new LinkedList<>();
             if (streams == null || isMultipart) {
-              HttpPost post = new HttpPost(url + ClientUtils.toQueryString( queryParams, false ));
+              String fullQueryUrl = url + ClientUtils.toQueryString( queryParams, false );
+              HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ?
+                new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
               if (!isMultipart) {
-                post.addHeader("Content-Type",
+                postOrPut.addHeader("Content-Type",
                     "application/x-www-form-urlencoded; charset=UTF-8");
               }
 
@@ -337,7 +342,7 @@ public class HttpSolrServer extends Solr
                     if (isMultipart) {
                       parts.add(new FormBodyPart(p, new StringBody(v, StandardCharsets.UTF_8)));
                     } else {
-                      postParams.add(new BasicNameValuePair(p, v));
+                      postOrPutParams.add(new BasicNameValuePair(p, v));
                     }
                   }
                 }
@@ -366,18 +371,19 @@ public class HttpSolrServer extends Solr
                 for(FormBodyPart p: parts) {
                   entity.addPart(p);
                 }
-                post.setEntity(entity);
+                postOrPut.setEntity(entity);
               } else {
                 //not using multipart
-                post.setEntity(new UrlEncodedFormEntity(postParams, StandardCharsets.UTF_8));
+                postOrPut.setEntity(new UrlEncodedFormEntity(postOrPutParams, StandardCharsets.UTF_8));
               }
 
-              method = post;
+              method = postOrPut;
             }
             // It is has one stream, it is the post body, put the params in the URL
             else {
               String pstr = ClientUtils.toQueryString(wparams, false);
-              HttpPost post = new HttpPost(url + pstr);
+              HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ?
+                new HttpPost(url + pstr) : new HttpPut(url + pstr);
 
               // Single stream as body
               // Using a loop just to get the first one
@@ -387,7 +393,7 @@ public class HttpSolrServer extends Solr
                 break;
               }
               if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
-                post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
+                postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                   @Override
                   public Header getContentType() {
                     return new BasicHeader("Content-Type", contentStream[0].getContentType());
@@ -400,7 +406,7 @@ public class HttpSolrServer extends Solr
                   
                 });
               } else {
-                post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
+                postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                   @Override
                   public Header getContentType() {
                     return new BasicHeader("Content-Type", contentStream[0].getContentType());
@@ -412,7 +418,7 @@ public class HttpSolrServer extends Solr
                   }
                 });
               }
-              method = post;
+              method = postOrPut;
             }
           }
           else {

Modified: lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java?rev=1627460&r1=1627459&r2=1627460&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java (original)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java Thu Sep 25 06:04:05 2014
@@ -116,6 +116,13 @@ public class BasicHttpSolrServerTest ext
       lastMethod = "post";
       recordRequest(req, resp);
     }
+
+    @Override
+    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
+        throws ServletException, IOException {
+      lastMethod = "put";
+      recordRequest(req, resp);
+    }
     
     private void recordRequest(HttpServletRequest req, HttpServletResponse resp) {
       setHeaders(req);
@@ -232,6 +239,23 @@ public class BasicHttpSolrServerTest ext
     assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
     assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
 
+    //PUT
+    DebugServlet.clear();
+    try {
+      server.query(q, METHOD.PUT);
+    } catch (Throwable t) {}
+    assertEquals("put", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+    assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+
     //XML/GET
     server.setParser(new XMLResponseParser());
     DebugServlet.clear();
@@ -266,6 +290,23 @@ public class BasicHttpSolrServerTest ext
     assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
     assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
     assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+
+    server.setParser(new XMLResponseParser());
+    DebugServlet.clear();
+    try {
+      server.query(q, METHOD.PUT);
+    } catch (Throwable t) {}
+    assertEquals("put", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+    assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
     server.shutdown();
   }
 

Modified: lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java?rev=1627460&r1=1627459&r2=1627460&view=diff
==============================================================================
--- lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java (original)
+++ lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java Thu Sep 25 06:04:05 2014
@@ -130,7 +130,7 @@ public class RestTestHarness extends Bas
    *
    * @param request The URL path and optional query params
    * @param content The content to include with the POST request
-   * @return The response to the PUT request
+   * @return The response to the POST request
    */
   public String post(String request, String content) throws IOException {
     HttpPost httpPost = new HttpPost(getBaseURL() + request);