use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class EmbeddedSolrServer method request.
// TODO-- this implementation sends the response to XML and then parses it.
// It *should* be able to convert the response directly into a named list.
@Override
public NamedList<Object> request(SolrRequest request, String coreName) throws SolrServerException, IOException {
String path = request.getPath();
if (path == null || !path.startsWith("/")) {
path = "/select";
}
SolrRequestHandler handler = coreContainer.getRequestHandler(path);
if (handler != null) {
try {
SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), request.getContentStreams());
SolrQueryResponse resp = new SolrQueryResponse();
handler.handleRequest(req, resp);
checkForExceptions(resp);
return BinaryResponseWriter.getParsedResponse(req, resp);
} catch (IOException | SolrException iox) {
throw iox;
} catch (Exception ex) {
throw new SolrServerException(ex);
}
}
if (coreName == null)
coreName = this.coreName;
// Check for cores action
SolrQueryRequest req = null;
try (SolrCore core = coreContainer.getCore(coreName)) {
if (core == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No such core: " + coreName);
}
SolrParams params = request.getParams();
if (params == null) {
params = new ModifiableSolrParams();
}
// Extract the handler from the path or params
handler = core.getRequestHandler(path);
if (handler == null) {
if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
String qt = params.get(CommonParams.QT);
handler = core.getRequestHandler(qt);
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
}
}
}
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
}
req = _parser.buildRequestFrom(core, params, request.getContentStreams());
req.getContext().put(PATH, path);
req.getContext().put("httpMethod", request.getMethod().name());
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(handler, req, rsp);
checkForExceptions(rsp);
// Check if this should stream results
if (request.getStreamingResponseCallback() != null) {
try {
final StreamingResponseCallback callback = request.getStreamingResponseCallback();
BinaryResponseWriter.Resolver resolver = new BinaryResponseWriter.Resolver(req, rsp.getReturnFields()) {
@Override
public void writeResults(ResultContext ctx, JavaBinCodec codec) throws IOException {
// write an empty list...
SolrDocumentList docs = new SolrDocumentList();
docs.setNumFound(ctx.getDocList().matches());
docs.setStart(ctx.getDocList().offset());
docs.setMaxScore(ctx.getDocList().maxScore());
codec.writeSolrDocumentList(docs);
// This will transform
writeResultsBody(ctx, codec);
}
};
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
createJavaBinCodec(callback, resolver).setWritableDocFields(resolver).marshal(rsp.getValues(), out);
try (InputStream in = out.toInputStream()) {
return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in);
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
// Now write it out
NamedList<Object> normalized = BinaryResponseWriter.getParsedResponse(req, rsp);
return normalized;
} catch (IOException | SolrException iox) {
throw iox;
} catch (Exception ex) {
throw new SolrServerException(ex);
} finally {
if (req != null)
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
Aggregations