mirror of https://github.com/apache/lucene.git
SOLR-11380: XML request writer also stream request
This commit is contained in:
parent
2e4b6929d2
commit
1b8747ec4f
|
@ -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<ContentStream> 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());
|
||||
|
|
|
@ -53,7 +53,7 @@ public class BinaryRequestWriter extends RequestWriter {
|
|||
}
|
||||
};
|
||||
} else {
|
||||
return super.getContentWriter(req);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<ContentStream> streams, ModifiableSolrParams wparams, boolean isMultipart, LinkedList<NameValuePair> postOrPutParams, String fullQueryUrl) throws IOException {
|
||||
|
|
|
@ -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<ContentStream> getContentStreams(SolrRequest req) throws IOException {
|
||||
if (req instanceof UpdateRequest) {
|
||||
UpdateRequest updateRequest = (UpdateRequest) req;
|
||||
if (isEmpty(updateRequest)) return null;
|
||||
List<ContentStream> 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();
|
||||
}
|
||||
|
|
|
@ -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 <code>ContentStream</code> from a <code>File</code>
|
||||
|
|
Loading…
Reference in New Issue