Skip to content

Apache Lucene open-source search software

SOLR-4358: HttpSolrServer now supports forcing multipart requests

Ryan McKinley8795ba0c431c
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
index 676029f4d0e..888be90fd05 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java
@@ -225,13 +225,22 @@ public class HttpSolrServer extends SolrServer {
else if( SolrRequest.METHOD.POST == request.getMethod() ) {
String url = baseUrl + path;
- boolean isMultipart = ( streams != null && streams.size() > 1 );
+ boolean hasNullStreamName = false;
+ if (streams != null) {
+ for (ContentStream cs : streams) {
+ if (cs.getName() == null) {
+ hasNullStreamName = true;
+ break;
+ }
+ }
+ }
+ boolean isMultipart = (this.useMultiPartPost || ( streams != null && streams.size() > 1 )) && !hasNullStreamName;
LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
if (streams == null || isMultipart) {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Charset", "UTF-8");
- if (!this.useMultiPartPost && !isMultipart) {
+ if (!isMultipart) {
post.addHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
}
@@ -243,7 +252,7 @@ public class HttpSolrServer extends SolrServer {
String[] vals = params.getParams(p);
if (vals != null) {
for (String v : vals) {
- if (this.useMultiPartPost || isMultipart) {
+ if (isMultipart) {
parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8"))));
} else {
postParams.add(new BasicNameValuePair(p, v));
@@ -252,13 +261,17 @@ public class HttpSolrServer extends SolrServer {
}
}
- if (isMultipart) {
+ if (isMultipart && streams != null) {
for (ContentStream content : streams) {
String contentType = content.getContentType();
if(contentType==null) {
contentType = "application/octet-stream"; // default
}
- parts.add(new FormBodyPart(content.getName(),
+ String name = content.getName();
+ if(name==null) {
+ name = "";
+ }
+ parts.add(new FormBodyPart(name,
new InputStreamBody(
content.getStream(),
contentType,
@@ -381,6 +394,15 @@ public class HttpSolrServer extends SolrServer {
shouldClose = false;
return rsp;
}
+
+// if(true) {
+// ByteArrayOutputStream copy = new ByteArrayOutputStream();
+// IOUtils.copy(respBody, copy);
+// String val = new String(copy.toByteArray());
+// System.out.println(">RESPONSE>"+val+"<"+val.length());
+// respBody = new ByteArrayInputStream(copy.toByteArray());
+// }
+
String charset = EntityUtils.getContentCharSet(response.getEntity());
NamedList<Object> rsp = processor.processResponse(respBody, charset);
if (httpStatus != HttpStatus.SC_OK) {
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleBinaryTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleBinaryTest.java
index fc8f751bbba..3c846b20c52 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleBinaryTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleBinaryTest.java
@@ -46,6 +46,7 @@ public class SolrExampleBinaryTest extends SolrExampleTests {
s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
s.setDefaultMaxConnectionsPerHost(100);
s.setMaxTotalConnections(100);
+ s.setUseMultiPartPost(random().nextBoolean());
// where the magic happens
s.setParser(new BinaryResponseParser());
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleXMLTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleXMLTest.java
index 2283e3ecb25..eb81b6d27e6 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleXMLTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleXMLTest.java
@@ -38,6 +38,7 @@ public class SolrExampleXMLTest extends SolrExampleTests {
try {
String url = jetty.getBaseUrl().toString();
HttpSolrServer s = new HttpSolrServer(url);
+ s.setUseMultiPartPost(random().nextBoolean());
s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
s.setDefaultMaxConnectionsPerHost(100);
s.setMaxTotalConnections(100);