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);
}
}
}
}
}
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();
}
}
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);
}
}
}
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);
}
Aggregations