SOLR-12142: EmbeddedSolrServer should use req.getContentWriter

This commit is contained in:
Noble Paul 2018-06-08 02:38:50 +10:00
parent 8cb6e3d3c8
commit 9b65d7e1a2
5 changed files with 95 additions and 25 deletions

View File

@ -35,7 +35,9 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.SpecProvider; import org.apache.solr.common.SpecProvider;
import org.apache.solr.common.util.CommandOperation; import org.apache.solr.common.util.CommandOperation;
import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.JsonSchemaValidator;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.PathTrie;
import org.apache.solr.common.util.Utils; import org.apache.solr.common.util.Utils;
import org.apache.solr.common.util.ValidatingJsonMap; import org.apache.solr.common.util.ValidatingJsonMap;
import org.apache.solr.core.PluginBag; import org.apache.solr.core.PluginBag;
@ -45,8 +47,6 @@ import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.security.AuthorizationContext; import org.apache.solr.security.AuthorizationContext;
import org.apache.solr.security.PermissionNameProvider; import org.apache.solr.security.PermissionNameProvider;
import org.apache.solr.common.util.JsonSchemaValidator;
import org.apache.solr.common.util.PathTrie;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -288,7 +288,7 @@ public class ApiBag {
try { try {
parsedCommands = CommandOperation.readCommands(Collections.singleton(stream), new NamedList()); parsedCommands = CommandOperation.readCommands(Collections.singleton(stream), new NamedList());
} catch (IOException e) { } catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to parse commands"); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to parse commands",e);
} }
if (validators == null || !validate) { // no validation possible because we do not have a spec if (validators == null || !validate) { // no validation possible because we do not have a spec

View File

@ -20,7 +20,10 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.io.output.ByteArrayOutputStream;
@ -30,12 +33,15 @@ import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.StreamingResponseCallback; import org.apache.solr.client.solrj.StreamingResponseCallback;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter; import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter.BAOS; import org.apache.solr.client.solrj.impl.BinaryRequestWriter.BAOS;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.JavaBinCodec; import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
@ -130,19 +136,8 @@ public class EmbeddedSolrServer extends SolrClient {
SolrRequestHandler handler = coreContainer.getRequestHandler(path); SolrRequestHandler handler = coreContainer.getRequestHandler(path);
if (handler != null) { if (handler != null) {
try { try {
SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), Collections.singleton(new ContentStreamBase() { SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), getContentStreams(request));
@Override req.getContext().put("httpMethod", request.getMethod().name());
public InputStream getStream() throws IOException {
BAOS baos = new BAOS();
new BinaryRequestWriter().write(request, baos);
return new ByteArrayInputStream(baos.getbuf());
}
@Override
public String getContentType() {
return CommonParams.JAVABIN_MIME;
}
}));
req.getContext().put(PATH, path); req.getContext().put(PATH, path);
SolrQueryResponse resp = new SolrQueryResponse(); SolrQueryResponse resp = new SolrQueryResponse();
handler.handleRequest(req, resp); handler.handleRequest(req, resp);
@ -187,7 +182,7 @@ public class EmbeddedSolrServer extends SolrClient {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
} }
req = _parser.buildRequestFrom(core, params, request.getContentStreams()); req = _parser.buildRequestFrom(core, params, getContentStreams(request));
req.getContext().put(PATH, path); req.getContext().put(PATH, path);
req.getContext().put("httpMethod", request.getMethod().name()); req.getContext().put("httpMethod", request.getMethod().name());
SolrQueryResponse rsp = new SolrQueryResponse(); SolrQueryResponse rsp = new SolrQueryResponse();
@ -242,6 +237,37 @@ public class EmbeddedSolrServer extends SolrClient {
} }
} }
private Set<ContentStream> getContentStreams(SolrRequest request) throws IOException {
if (request.getMethod() == SolrRequest.METHOD.GET) return null;
if (request instanceof ContentStreamUpdateRequest) {
ContentStreamUpdateRequest csur = (ContentStreamUpdateRequest) request;
Collection<ContentStream> cs = csur.getContentStreams();
if (cs != null) return new HashSet<>(cs);
}
RequestWriter.ContentWriter contentWriter = request.getContentWriter(CommonParams.JAVABIN_MIME);
final String cType = contentWriter == null ? CommonParams.JAVABIN_MIME : contentWriter.getContentType();
return Collections.singleton(new ContentStreamBase() {
@Override
public InputStream getStream() throws IOException {
BAOS baos = new BAOS();
if (contentWriter != null) {
contentWriter.write(baos);
} else {
new BinaryRequestWriter().write(request, baos);
}
return new ByteArrayInputStream(baos.toByteArray());
}
@Override
public String getContentType() {
return cType;
}
});
}
private JavaBinCodec createJavaBinCodec(final StreamingResponseCallback callback, final BinaryResponseWriter.Resolver resolver) { private JavaBinCodec createJavaBinCodec(final StreamingResponseCallback callback, final BinaryResponseWriter.Resolver resolver) {
return new JavaBinCodec(resolver) { return new JavaBinCodec(resolver) {

View File

@ -34,6 +34,7 @@ import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.StreamingResponseCallback; import org.apache.solr.client.solrj.StreamingResponseCallback;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.request.QueryRequest; import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
@ -100,10 +101,10 @@ public class EmbeddedSolrNoSerializeTest extends SolrTestCaseJ4 {
} }
// As of 7.2. But won't work until: https://issues.apache.org/jira/browse/SOLR-12142 // As of 7.2. But won't work until: https://issues.apache.org/jira/browse/SOLR-12142
// @Override @Override
// public RequestWriter.ContentWriter getContentWriter(String expectedType) { public RequestWriter.ContentWriter getContentWriter(String expectedType) {
// return new RequestWriter.StringPayloadContentWriter(input, "text/plain; charset=UTF8"); return new RequestWriter.StringPayloadContentWriter(input, "text/plain; charset=UTF8");
// } }
} }
@Test @Test

View File

@ -15,15 +15,18 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.solr.client.solrj.request; package org.apache.solr.client.solrj.request;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import java.io.IOException;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
/** /**
* Basic functionality to upload a File or {@link org.apache.solr.common.util.ContentStream} to a Solr Cell or some * Basic functionality to upload a File or {@link org.apache.solr.common.util.ContentStream} to a Solr Cell or some
@ -51,6 +54,23 @@ public class ContentStreamUpdateRequest extends AbstractUpdateRequest {
return contentStreams; return contentStreams;
} }
@Override
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
if (contentStreams == null || contentStreams.isEmpty() || contentStreams.size() > 1) return null;
ContentStream stream = contentStreams.get(0);
return new RequestWriter.ContentWriter() {
@Override
public void write(OutputStream os) throws IOException {
IOUtils.copy(stream.getStream(), os);
}
@Override
public String getContentType() {
return stream.getContentType();
}
};
}
/** /**
* Add a File to the {@link org.apache.solr.common.util.ContentStream}s. * Add a File to the {@link org.apache.solr.common.util.ContentStream}s.
* @param file The File to add. * @param file The File to add.

View File

@ -17,6 +17,8 @@
package org.apache.solr.client.solrj.request.schema; package org.apache.solr.client.solrj.request.schema;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
@ -25,7 +27,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.response.schema.SchemaResponse; import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.ContentStreamBase;
@ -706,12 +710,31 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
protected abstract NamedList<Object> getRequestParameters(); protected abstract NamedList<Object> getRequestParameters();
@Override @Override
@Deprecated
public Collection<ContentStream> getContentStreams() throws IOException { public Collection<ContentStream> getContentStreams() throws IOException {
CharArr json = new CharArr(); CharArr json = new CharArr();
new SchemaRequestJSONWriter(json).write(getRequestParameters()); new SchemaRequestJSONWriter(json).write(getRequestParameters());
return Collections.singletonList(new ContentStreamBase.StringStream(json.toString())); return Collections.singletonList(new ContentStreamBase.StringStream(json.toString()));
} }
@Override
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
return new RequestWriter.ContentWriter() {
@Override
public void write(OutputStream os) throws IOException {
//TODO : find a way to do streaming write
CharArr json = new CharArr();
new SchemaRequestJSONWriter(json).write(getRequestParameters());
os.write(json.toString().getBytes(StandardCharsets.UTF_8));
}
@Override
public String getContentType() {
return CommonParams.JSON_MIME;
}
};
}
@Override @Override
protected SchemaResponse.UpdateResponse createResponse(SolrClient client) { protected SchemaResponse.UpdateResponse createResponse(SolrClient client) {
return new SchemaResponse.UpdateResponse(); return new SchemaResponse.UpdateResponse();