SOLR-6543: Give HttpSolrServer the ability to send PUT requests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1627460 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gregory Chanan 2014-09-25 06:04:05 +00:00
parent 3adfad8cf5
commit fe0c5d68f5
5 changed files with 65 additions and 15 deletions

View File

@ -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
----------------------

View File

@ -33,7 +33,8 @@ public abstract class SolrRequest implements Serializable
{
public enum METHOD {
GET,
POST
POST,
PUT
};
private METHOD method = METHOD.GET;

View File

@ -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 SolrServer {
}
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 SolrServer {
}
}
}
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 SolrServer {
}
}
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 SolrServer {
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 SolrServer {
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 SolrServer {
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 SolrServer {
});
} 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 SolrServer {
}
});
}
method = post;
method = postOrPut;
}
}
else {

View File

@ -116,6 +116,13 @@ public class BasicHttpSolrServerTest extends SolrJettyTestBase {
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 extends SolrJettyTestBase {
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 extends SolrJettyTestBase {
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();
}

View File

@ -130,7 +130,7 @@ public class RestTestHarness extends BaseTestHarness {
*
* @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);