Search in sources :

Example 6 with QueryResponseWriter

use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.

the class HttpSolrCall method call.

/**
   * This method processes the request.
   */
public Action call() throws IOException {
    MDCLoggingContext.reset();
    MDCLoggingContext.setNode(cores);
    if (cores == null) {
        sendError(503, "Server is shutting down or failed to initialize");
        return RETURN;
    }
    if (solrDispatchFilter.abortErrorMessage != null) {
        sendError(500, solrDispatchFilter.abortErrorMessage);
        return RETURN;
    }
    try {
        init();
        /* Authorize the request if
       1. Authorization is enabled, and
       2. The requested resource is not a known static file
        */
        if (cores.getAuthorizationPlugin() != null && shouldAuthorize()) {
            AuthorizationContext context = getAuthCtx();
            log.debug("AuthorizationContext : {}", context);
            AuthorizationResponse authResponse = cores.getAuthorizationPlugin().authorize(context);
            if (authResponse.statusCode == AuthorizationResponse.PROMPT.statusCode) {
                Map<String, String> headers = (Map) getReq().getAttribute(AuthenticationPlugin.class.getName());
                if (headers != null) {
                    for (Map.Entry<String, String> e : headers.entrySet()) response.setHeader(e.getKey(), e.getValue());
                }
                log.debug("USER_REQUIRED " + req.getHeader("Authorization") + " " + req.getUserPrincipal());
            }
            if (!(authResponse.statusCode == HttpStatus.SC_ACCEPTED) && !(authResponse.statusCode == HttpStatus.SC_OK)) {
                log.info("USER_REQUIRED auth header {} context : {} ", req.getHeader("Authorization"), context);
                sendError(authResponse.statusCode, "Unauthorized request, Response code: " + authResponse.statusCode);
                return RETURN;
            }
        }
        HttpServletResponse resp = response;
        switch(action) {
            case ADMIN:
                handleAdminRequest();
                return RETURN;
            case REMOTEQUERY:
                remoteQuery(coreUrl + path, resp);
                return RETURN;
            case PROCESS:
                final Method reqMethod = Method.getMethod(req.getMethod());
                HttpCacheHeaderUtil.setCacheControlHeader(config, resp, reqMethod);
                // if we fail cache validation, execute the query
                if (config.getHttpCachingConfig().isNever304() || !HttpCacheHeaderUtil.doCacheHeaderValidation(solrReq, req, reqMethod, resp)) {
                    SolrQueryResponse solrRsp = new SolrQueryResponse();
                    /* even for HEAD requests, we need to execute the handler to
               * ensure we don't get an error (and to make sure the correct
               * QueryResponseWriter is selected and we get the correct
               * Content-Type)
               */
                    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
                    execute(solrRsp);
                    HttpCacheHeaderUtil.checkHttpCachingVeto(solrRsp, resp, reqMethod);
                    Iterator<Map.Entry<String, String>> headers = solrRsp.httpHeaders();
                    while (headers.hasNext()) {
                        Map.Entry<String, String> entry = headers.next();
                        resp.addHeader(entry.getKey(), entry.getValue());
                    }
                    QueryResponseWriter responseWriter = getResponseWriter();
                    if (invalidStates != null)
                        solrReq.getContext().put(CloudSolrClient.STATE_VERSION, invalidStates);
                    writeResponse(solrRsp, responseWriter, reqMethod);
                }
                return RETURN;
            default:
                return action;
        }
    } catch (Throwable ex) {
        sendError(ex);
        // walk the the entire cause chain to search for an Error
        Throwable t = ex;
        while (t != null) {
            if (t instanceof Error) {
                if (t != ex) {
                    log.error("An Error was wrapped in another exception - please report complete stacktrace on SOLR-6161", ex);
                }
                throw (Error) t;
            }
            t = t.getCause();
        }
        return RETURN;
    } finally {
        MDCLoggingContext.clear();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthorizationContext(org.apache.solr.security.AuthorizationContext) Method(org.apache.solr.servlet.cache.Method) AuthorizationResponse(org.apache.solr.security.AuthorizationResponse) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) Map(java.util.Map) ValidatingJsonMap(org.apache.solr.common.util.ValidatingJsonMap) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap)

Example 7 with QueryResponseWriter

use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.

the class HttpSolrCall method sendError.

protected void sendError(Throwable ex) throws IOException {
    Exception exp = null;
    SolrCore localCore = null;
    try {
        SolrQueryResponse solrResp = new SolrQueryResponse();
        if (ex instanceof Exception) {
            solrResp.setException((Exception) ex);
        } else {
            solrResp.setException(new RuntimeException(ex));
        }
        localCore = core;
        if (solrReq == null) {
            final SolrParams solrParams;
            if (req != null) {
                // use GET parameters if available:
                solrParams = SolrRequestParsers.parseQueryString(req.getQueryString());
            } else {
                // we have no params at all, use empty ones:
                solrParams = new MapSolrParams(Collections.<String, String>emptyMap());
            }
            solrReq = new SolrQueryRequestBase(core, solrParams) {
            };
        }
        QueryResponseWriter writer = core.getQueryResponseWriter(solrReq);
        writeResponse(solrResp, writer, Method.GET);
    } catch (Exception e) {
        // This error really does not matter
        exp = e;
    } finally {
        try {
            if (exp != null) {
                SimpleOrderedMap info = new SimpleOrderedMap();
                int code = ResponseUtils.getErrorInfo(ex, info, log);
                sendError(code, info.toString());
            }
        } finally {
            if (core == null && localCore != null) {
                localCore.close();
            }
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) EOFException(java.io.EOFException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with QueryResponseWriter

use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.

the class TestCrossCoreJoin method query.

public String query(SolrCore core, SolrQueryRequest req) throws Exception {
    String handler = "standard";
    if (req.getParams().get("qt") != null)
        handler = req.getParams().get("qt");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    core.execute(core.getRequestHandler(handler), req, rsp);
    if (rsp.getException() != null) {
        throw rsp.getException();
    }
    StringWriter sw = new StringWriter(32000);
    QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
    responseWriter.write(sw, req, rsp);
    req.close();
    SolrRequestInfo.clearRequestInfo();
    return sw.toString();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Example 9 with QueryResponseWriter

use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.

the class VelocityResponseWriterTest method testVelocityResponseWriterRegistered.

@Test
public void testVelocityResponseWriterRegistered() {
    QueryResponseWriter writer = h.getCore().getQueryResponseWriter("velocity");
    assertTrue("VrW registered check", writer instanceof VelocityResponseWriter);
}
Also used : VelocityResponseWriter(org.apache.solr.response.VelocityResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Test(org.junit.Test)

Example 10 with QueryResponseWriter

use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.

the class TestWriterPerf method doPerf.

void doPerf(String writerName, SolrQueryRequest req, int encIter, int decIter) throws Exception {
    SolrQueryResponse rsp = getResponse(req);
    QueryResponseWriter w = h.getCore().getQueryResponseWriter(writerName);
    ByteArrayOutputStream out = null;
    System.gc();
    RTimer timer = new RTimer();
    for (int i = 0; i < encIter; i++) {
        if (w instanceof BinaryQueryResponseWriter) {
            BinaryQueryResponseWriter binWriter = (BinaryQueryResponseWriter) w;
            out = new ByteArrayOutputStream();
            binWriter.write(out, req, rsp);
            out.close();
        } else {
            out = new ByteArrayOutputStream();
            // to be fair, from my previous tests, much of the performance will be sucked up
            // by java's UTF-8 encoding/decoding, not the actual writing
            Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            w.write(writer, req, rsp);
            writer.close();
        }
    }
    double encodeTime = timer.getTime();
    byte[] arr = out.toByteArray();
    timer = new RTimer();
    writerName = writerName.intern();
    for (int i = 0; i < decIter; i++) {
        ResponseParser rp = null;
        if (writerName == "xml") {
            rp = new XMLResponseParser();
        } else if (writerName == "javabin") {
            rp = new BinaryResponseParser();
        } else {
            break;
        }
        ByteArrayInputStream in = new ByteArrayInputStream(arr);
        rp.processResponse(in, "UTF-8");
    }
    double decodeTime = timer.getTime();
    log.info("writer " + writerName + ", size=" + out.size() + ", encodeRate=" + (encIter * 1000L / encodeTime) + ", decodeRate=" + (decIter * 1000L / decodeTime));
    req.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ResponseParser(org.apache.solr.client.solrj.ResponseParser) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RTimer(org.apache.solr.util.RTimer) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) OutputStreamWriter(java.io.OutputStreamWriter) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)15 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)11 StringWriter (java.io.StringWriter)6 SolrCore (org.apache.solr.core.SolrCore)5 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 MapSolrParams (org.apache.solr.common.params.MapSolrParams)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)2 SolrDocument (org.apache.solr.common.SolrDocument)2 SolrException (org.apache.solr.common.SolrException)2 ContentStream (org.apache.solr.common.util.ContentStream)2 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)2 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1