Search in sources :

Example 26 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class HttpSolrCall method extractHandlerFromURLPath.

/**
   * Extract handler from the URL path if not set.
   * This returns true if the action is set.
   * 
   */
protected void extractHandlerFromURLPath(SolrRequestParsers parser) throws Exception {
    if (handler == null && path.length() > 1) {
        // don't match "" or "/" as valid path
        handler = core.getRequestHandler(path);
        if (handler == null) {
            // Handle /schema/* paths via Restlet
            if (path.equals("/schema") || path.startsWith("/schema/")) {
                solrReq = parser.parse(core, path, req);
                SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, new SolrQueryResponse()));
                if (path.equals(req.getServletPath())) {
                    // avoid endless loop - pass through to Restlet via webapp
                    action = PASSTHROUGH;
                    return;
                } else {
                    // forward rewritten URI (without path prefix and core/collection name) to Restlet
                    action = FORWARD;
                    return;
                }
            }
        }
        if (handler == null && parser.isHandleSelect()) {
            if ("/select".equals(path) || "/select/".equals(path)) {
                solrReq = parser.parse(core, path, req);
                invalidStates = checkStateIsValid(solrReq.getParams().get(CloudSolrClient.STATE_VERSION));
                String qt = solrReq.getParams().get(CommonParams.QT);
                handler = core.getRequestHandler(qt);
                if (handler == null) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
                }
                if (qt != null && qt.startsWith("/") && (handler instanceof ContentStreamHandlerBase)) {
                    //There was no restriction from Solr 1.4 thru 3.5 and it's not supported for update handlers.
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid Request Handler ('qt').  Do not use /select to access: " + qt);
                }
            }
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ContentStreamHandlerBase(org.apache.solr.handler.ContentStreamHandlerBase) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SolrException(org.apache.solr.common.SolrException)

Example 27 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class DirectSolrConnection method request.

public String request(SolrRequestHandler handler, SolrParams params, String body) throws Exception {
    if (params == null)
        params = new MapSolrParams(new HashMap<String, String>());
    // Make a stream for the 'body' content
    List<ContentStream> streams = new ArrayList<>(1);
    if (body != null && body.length() > 0) {
        streams.add(new ContentStreamBase.StringStream(body));
    }
    SolrQueryRequest req = null;
    try {
        req = parser.buildRequestFrom(core, params, streams);
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        core.execute(handler, req, rsp);
        if (rsp.getException() != null) {
            throw rsp.getException();
        }
        // Now write it out
        QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
        StringWriter out = new StringWriter();
        responseWriter.write(out, req, rsp);
        return out.toString();
    } finally {
        if (req != null) {
            req.close();
        }
        SolrRequestInfo.clearRequestInfo();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 28 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class BaseSolrResource method doInit.

/**
   * Pulls the SolrQueryRequest constructed in SolrDispatchFilter
   * from the SolrRequestInfo thread local, then gets the SolrCore
   * and IndexSchema and sets up the response.
   * writer.
   * <p>
   * If an error occurs during initialization, setExisting(false) is
   * called and an error status code and message is set; in this case,
   * Restlet will not continue servicing the request (by calling the
   * method annotated to associate it with GET, etc., but rather will
   * send an error response.
   */
@Override
public void doInit() throws ResourceException {
    super.doInit();
    // Turn off content negotiation for now
    setNegotiated(false);
    if (isExisting()) {
        try {
            SolrRequestInfo solrRequestInfo = SolrRequestInfo.getRequestInfo();
            if (null == solrRequestInfo) {
                final String message = "No handler or core found in " + getRequest().getOriginalRef().getPath();
                doError(Status.CLIENT_ERROR_BAD_REQUEST, message);
                setExisting(false);
            } else {
                solrRequest = solrRequestInfo.getReq();
                if (null == solrRequest) {
                    final String message = "No handler or core found in " + getRequest().getOriginalRef().getPath();
                    doError(Status.CLIENT_ERROR_BAD_REQUEST, message);
                    setExisting(false);
                } else {
                    solrResponse = solrRequestInfo.getRsp();
                    solrCore = solrRequest.getCore();
                    schema = solrRequest.getSchema();
                    String responseWriterName = solrRequest.getParams().get(CommonParams.WT);
                    if (null == responseWriterName) {
                        // Default to json writer
                        responseWriterName = JSON;
                    }
                    String indent = solrRequest.getParams().get("indent");
                    if (null == indent || !("off".equals(indent) || "false".equals(indent))) {
                        // indent by default
                        ModifiableSolrParams newParams = new ModifiableSolrParams(solrRequest.getParams());
                        newParams.remove(indent);
                        newParams.add("indent", "on");
                        solrRequest.setParams(newParams);
                    }
                    responseWriter = solrCore.getQueryResponseWriter(responseWriterName);
                    contentType = responseWriter.getContentType(solrRequest, solrResponse);
                    final String path = getRequest().getRootRef().getPath();
                    if (!RestManager.SCHEMA_BASE_PATH.equals(path)) {
                        // don't set webapp property on the request when context and core/collection are excluded 
                        final int cutoffPoint = path.indexOf("/", 1);
                        final String firstPathElement = -1 == cutoffPoint ? path : path.substring(0, cutoffPoint);
                        // Context path
                        solrRequest.getContext().put("webapp", firstPathElement);
                    }
                    SolrCore.preDecorateResponse(solrRequest, solrResponse);
                    // client application can set a timeout for update requests
                    Object updateTimeoutSecsParam = getSolrRequest().getParams().get(UPDATE_TIMEOUT_SECS);
                    if (updateTimeoutSecsParam != null)
                        updateTimeoutSecs = (updateTimeoutSecsParam instanceof Number) ? ((Number) updateTimeoutSecsParam).intValue() : Integer.parseInt(updateTimeoutSecsParam.toString());
                }
            }
        } catch (Throwable t) {
            if (t instanceof OutOfMemoryError) {
                throw (OutOfMemoryError) t;
            }
            setExisting(false);
            throw new ResourceException(t);
        }
    }
}
Also used : SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ResourceException(org.restlet.resource.ResourceException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 29 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class DateRangeField method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String startStr, String endStr, boolean minInclusive, boolean maxInclusive) {
    if (parser == null) {
        //null when invoked by SimpleFacets.  But getQueryFromSpatialArgs expects to get localParams.
        final SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
        parser = new QParser("", null, requestInfo.getReq().getParams(), requestInfo.getReq()) {

            @Override
            public Query parse() throws SyntaxError {
                throw new IllegalStateException();
            }
        };
    }
    Calendar startCal;
    if (startStr == null) {
        startCal = tree.newCal();
    } else {
        startCal = parseCalendar(startStr);
        if (!minInclusive) {
            startCal.add(Calendar.MILLISECOND, 1);
        }
    }
    Calendar endCal;
    if (endStr == null) {
        endCal = tree.newCal();
    } else {
        endCal = parseCalendar(endStr);
        if (!maxInclusive) {
            endCal.add(Calendar.MILLISECOND, -1);
        }
    }
    Shape shape = tree.toRangeShape(tree.toShape(startCal), tree.toShape(endCal));
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
    return getQueryFromSpatialArgs(parser, field, spatialArgs);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) NRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.NRShape) Shape(org.locationtech.spatial4j.shape.Shape) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) Query(org.apache.lucene.search.Query) SyntaxError(org.apache.solr.search.SyntaxError) Calendar(java.util.Calendar) QParser(org.apache.solr.search.QParser) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Aggregations

SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)29 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)22 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)16 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)9 SolrCore (org.apache.solr.core.SolrCore)9 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)8 Map (java.util.Map)5 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)5 Query (org.apache.lucene.search.Query)4 SolrException (org.apache.solr.common.SolrException)4 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)4 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 HashMap (java.util.HashMap)3 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)3 NamedList (org.apache.solr.common.util.NamedList)3 BinaryResponseWriter (org.apache.solr.response.BinaryResponseWriter)3 ResultContext (org.apache.solr.response.ResultContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2