mirror of https://github.com/apache/lucene.git
SOLR-12142: EmbeddedSolrServer should use req.getContentWriter
This commit is contained in:
parent
8cb6e3d3c8
commit
9b65d7e1a2
|
@ -35,7 +35,9 @@ import org.apache.solr.common.SolrException;
|
|||
import org.apache.solr.common.SpecProvider;
|
||||
import org.apache.solr.common.util.CommandOperation;
|
||||
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.PathTrie;
|
||||
import org.apache.solr.common.util.Utils;
|
||||
import org.apache.solr.common.util.ValidatingJsonMap;
|
||||
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.security.AuthorizationContext;
|
||||
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.LoggerFactory;
|
||||
|
||||
|
@ -288,7 +288,7 @@ public class ApiBag {
|
|||
try {
|
||||
parsedCommands = CommandOperation.readCommands(Collections.singleton(stream), new NamedList());
|
||||
} 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
|
||||
|
|
|
@ -20,7 +20,10 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
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.impl.BinaryRequestWriter;
|
||||
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.SolrDocumentList;
|
||||
import org.apache.solr.common.SolrException;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.params.ModifiableSolrParams;
|
||||
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.JavaBinCodec;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
|
@ -130,19 +136,8 @@ public class EmbeddedSolrServer extends SolrClient {
|
|||
SolrRequestHandler handler = coreContainer.getRequestHandler(path);
|
||||
if (handler != null) {
|
||||
try {
|
||||
SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), Collections.singleton(new ContentStreamBase() {
|
||||
@Override
|
||||
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;
|
||||
|
||||
}
|
||||
}));
|
||||
SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), getContentStreams(request));
|
||||
req.getContext().put("httpMethod", request.getMethod().name());
|
||||
req.getContext().put(PATH, path);
|
||||
SolrQueryResponse resp = new SolrQueryResponse();
|
||||
handler.handleRequest(req, resp);
|
||||
|
@ -187,7 +182,7 @@ public class EmbeddedSolrServer extends SolrClient {
|
|||
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("httpMethod", request.getMethod().name());
|
||||
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) {
|
||||
return new JavaBinCodec(resolver) {
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.apache.solr.client.solrj.SolrServerException;
|
|||
import org.apache.solr.client.solrj.StreamingResponseCallback;
|
||||
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
||||
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.common.SolrDocument;
|
||||
import org.apache.solr.common.SolrDocumentList;
|
||||
|
@ -99,11 +100,11 @@ public class EmbeddedSolrNoSerializeTest extends SolrTestCaseJ4 {
|
|||
return Collections.singleton(new ContentStreamBase.StringStream(input));
|
||||
}
|
||||
|
||||
// As of 7.2. But won't work until: https://issues.apache.org/jira/browse/SOLR-12142
|
||||
// @Override
|
||||
// public RequestWriter.ContentWriter getContentWriter(String expectedType) {
|
||||
// return new RequestWriter.StringPayloadContentWriter(input, "text/plain; charset=UTF8");
|
||||
// }
|
||||
// As of 7.2. But won't work until: https://issues.apache.org/jira/browse/SOLR-12142
|
||||
@Override
|
||||
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
|
||||
return new RequestWriter.StringPayloadContentWriter(input, "text/plain; charset=UTF8");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -15,15 +15,18 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
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.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
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
|
||||
|
@ -51,6 +54,23 @@ public class ContentStreamUpdateRequest extends AbstractUpdateRequest {
|
|||
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.
|
||||
* @param file The File to add.
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
package org.apache.solr.client.solrj.request.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
@ -25,7 +27,9 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
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.common.params.CommonParams;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.common.util.ContentStream;
|
||||
import org.apache.solr.common.util.ContentStreamBase;
|
||||
|
@ -706,12 +710,31 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
|
|||
protected abstract NamedList<Object> getRequestParameters();
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Collection<ContentStream> getContentStreams() throws IOException {
|
||||
CharArr json = new CharArr();
|
||||
new SchemaRequestJSONWriter(json).write(getRequestParameters());
|
||||
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
|
||||
protected SchemaResponse.UpdateResponse createResponse(SolrClient client) {
|
||||
return new SchemaResponse.UpdateResponse();
|
||||
|
|
Loading…
Reference in New Issue