Search in sources :

Example 16 with JavaBinCodec

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();
    }
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrCore(org.apache.solr.core.SolrCore) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SolrException(org.apache.solr.common.SolrException) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) IOException(java.io.IOException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Aggregations

JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)16 NamedList (org.apache.solr.common.util.NamedList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 SolrDocument (org.apache.solr.common.SolrDocument)6 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SolrException (org.apache.solr.common.SolrException)5 IOException (java.io.IOException)4 List (java.util.List)4 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SolrCore (org.apache.solr.core.SolrCore)3 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)3 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 FieldType (org.apache.solr.schema.FieldType)3