From 1b8747ec4f3dfd0c1572c8e34857a8d12a71b7b4 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Wed, 1 Nov 2017 19:25:35 +1030 Subject: [PATCH] SOLR-11380: XML request writer also stream request --- .../SignatureUpdateProcessorFactoryTest.java | 7 +- .../solrj/impl/BinaryRequestWriter.java | 2 +- .../client/solrj/impl/HttpSolrClient.java | 37 +++----- .../client/solrj/request/RequestWriter.java | 92 ++++--------------- .../solr/common/util/ContentStreamBase.java | 10 ++ 5 files changed, 45 insertions(+), 103 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java b/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java index 012b8ce69b8..8f9d2b72dd4 100644 --- a/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java +++ b/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java @@ -17,6 +17,7 @@ package org.apache.solr.update.processor; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -27,7 +28,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.MultiMapSolrParams; import org.apache.solr.common.params.UpdateParams; -import org.apache.solr.common.util.ContentStream; +import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrCore; import org.apache.solr.handler.UpdateRequestHandler; @@ -323,11 +324,9 @@ public class SignatureUpdateProcessorFactoryTest extends SolrTestCaseJ4 { } - ArrayList streams = new ArrayList<>(2); - streams.add(new BinaryRequestWriter().getContentStream(ureq)); LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), mmparams); try { - req.setContentStreams(streams); + req.setContentStreams(Collections.singletonList(ContentStreamBase.create(new BinaryRequestWriter(), ureq))); UpdateRequestHandler h = new UpdateRequestHandler(); h.init(new NamedList()); h.handleRequestBody(req, new SolrQueryResponse()); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java index ee961ef553a..4687fe36304 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java @@ -53,7 +53,7 @@ public class BinaryRequestWriter extends RequestWriter { } }; } else { - return super.getContentWriter(req); + return null; } } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java index c292534dac9..2b60e337e4a 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java @@ -443,34 +443,19 @@ public class HttpSolrClient extends SolrClient { contentStream[0] = content; break; } - if (contentStream[0] instanceof RequestWriter.LazyContentStream) { - Long size = contentStream[0].getSize(); - postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) { - @Override - public Header getContentType() { - return new BasicHeader("Content-Type", contentStream[0].getContentType()); - } + Long size = contentStream[0].getSize(); + postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) { + @Override + public Header getContentType() { + return new BasicHeader("Content-Type", contentStream[0].getContentType()); + } - @Override - public boolean isRepeatable() { - return false; - } + @Override + public boolean isRepeatable() { + return false; + } + }); - }); - } else { - Long size = contentStream[0].getSize(); - postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) { - @Override - public Header getContentType() { - return new BasicHeader("Content-Type", contentStream[0].getContentType()); - } - - @Override - public boolean isRepeatable() { - return false; - } - }); - } } private HttpEntityEnclosingRequestBase fillContentStream(SolrRequest request, Collection streams, ModifiableSolrParams wparams, boolean isMultipart, LinkedList postOrPutParams, String fullQueryUrl) throws IOException { diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java index d83ca911ee2..ef7f14d30b7 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java @@ -18,13 +18,9 @@ package org.apache.solr.client.solrj.request; import java.io.BufferedWriter; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.Reader; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -32,7 +28,6 @@ import java.util.Map; import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.client.solrj.util.ClientUtils; import org.apache.solr.common.util.ContentStream; -import org.apache.solr.common.util.ContentStreamBase; /** * A RequestWriter is used to write requests to Solr. @@ -43,7 +38,6 @@ import org.apache.solr.common.util.ContentStreamBase; * @since solr 1.4 */ public class RequestWriter { - public static final Charset UTF_8 = StandardCharsets.UTF_8; public interface ContentWriter { @@ -55,20 +49,33 @@ public class RequestWriter { /** * Use this to do a push writing instead of pull. If this method returns null - * {@link org.apache.solr.client.solrj.request.RequestWriter#getContentStream(UpdateRequest)} is + * {@link org.apache.solr.client.solrj.request.RequestWriter#getContentStreams(SolrRequest)} is * invoked to do a pull write. */ public ContentWriter getContentWriter(SolrRequest req) { + if (req instanceof UpdateRequest) { + UpdateRequest updateRequest = (UpdateRequest) req; + if (isEmpty(updateRequest)) return null; + return new ContentWriter() { + @Override + public void write(OutputStream os) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8); + updateRequest.writeXML(writer); + writer.flush(); + } + + @Override + public String getContentType() { + return ClientUtils.TEXT_XML; + } + }; + } return null; } public Collection getContentStreams(SolrRequest req) throws IOException { if (req instanceof UpdateRequest) { - UpdateRequest updateRequest = (UpdateRequest) req; - if (isEmpty(updateRequest)) return null; - List l = new ArrayList<>(); - l.add(new LazyContentStream(updateRequest)); - return l; + return null; } return req.getContentStreams(); } @@ -84,14 +91,10 @@ public class RequestWriter { return req.getPath(); } - public ContentStream getContentStream(UpdateRequest req) throws IOException { - return new ContentStreamBase.StringStream(req.getXML()); - } - public void write(SolrRequest request, OutputStream os) throws IOException { if (request instanceof UpdateRequest) { UpdateRequest updateRequest = (UpdateRequest) request; - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, UTF_8)); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); updateRequest.writeXML(writer); writer.flush(); } @@ -102,61 +105,6 @@ public class RequestWriter { } - public class LazyContentStream implements ContentStream { - ContentStream contentStream = null; - UpdateRequest req = null; - - public LazyContentStream(UpdateRequest req) { - this.req = req; - } - - private ContentStream getDelegate() { - if (contentStream == null) { - try { - contentStream = getContentStream(req); - } catch (IOException e) { - throw new RuntimeException("Unable to write xml into a stream", e); - } - } - return contentStream; - } - - @Override - public String getName() { - return getDelegate().getName(); - } - - @Override - public String getSourceInfo() { - return getDelegate().getSourceInfo(); - } - - @Override - public String getContentType() { - return getUpdateContentType(); - } - - @Override - public Long getSize() { - return getDelegate().getSize(); - } - - @Override - public InputStream getStream() throws IOException { - return getDelegate().getStream(); - } - - @Override - public Reader getReader() throws IOException { - return getDelegate().getReader(); - } - - public void writeTo(OutputStream os) throws IOException { - write(req, os); - - } - } - protected boolean isNull(List l) { return l == null || l.isEmpty(); } diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java index f9db69c5e18..33860ff2c76 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java @@ -17,6 +17,7 @@ package org.apache.solr.common.util; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -30,6 +31,9 @@ import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.Locale; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.request.RequestWriter; + /** * Three concrete implementations for ContentStream - one for File/URL/String * @@ -251,6 +255,12 @@ public abstract class ContentStreamBase implements ContentStream public void setSourceInfo(String sourceInfo) { this.sourceInfo = sourceInfo; } + public static ContentStream create(RequestWriter requestWriter, SolrRequest req) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + RequestWriter.ContentWriter contentWriter = requestWriter.getContentWriter(req); + contentWriter.write(baos); + return new ByteArrayStream(baos.toByteArray(), null,contentWriter.getContentType() ); + } /** * Construct a ContentStream from a File