@Test
  public void testNaNInf() throws IOException {
    SolrQueryRequest req = req("dummy");
    SolrQueryResponse rsp = new SolrQueryResponse();
    QueryResponseWriter w = new PythonResponseWriter();

    StringWriter buf = new StringWriter();
    rsp.add("data1", Float.NaN);
    rsp.add("data2", Double.NEGATIVE_INFINITY);
    rsp.add("data3", Float.POSITIVE_INFINITY);
    w.write(buf, req, rsp);
    assertEquals(
        buf.toString(), "{'data1':float('NaN'),'data2':-float('Inf'),'data3':float('Inf')}");

    w = new RubyResponseWriter();
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals(buf.toString(), "{'data1'=>(0.0/0.0),'data2'=>-(1.0/0.0),'data3'=>(1.0/0.0)}");

    w = new JSONResponseWriter();
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals(
        buf.toString(), "{\"data1\":\"NaN\",\"data2\":\"-Infinity\",\"data3\":\"Infinity\"}");
    req.close();
  }
  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();
    long start = System.currentTimeMillis();
    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, "UTF-8");
        w.write(writer, req, rsp);
        writer.close();
      }
    }

    long encodeTime = Math.max(System.currentTimeMillis() - start, 1);

    byte[] arr = out.toByteArray();

    start = System.currentTimeMillis();
    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");
    }

    long decodeTime = Math.max(System.currentTimeMillis() - start, 1);

    log.info(
        "writer "
            + writerName
            + ", size="
            + out.size()
            + ", encodeRate="
            + (encodeTime == 1 ? "N/A" : "" + (encIter * 1000L / encodeTime))
            + ", decodeRate="
            + (decodeTime == 1 ? "N/A" : "" + (decIter * 1000L / decodeTime)));

    req.close();
    SolrRequestInfo.clearRequestInfo();
  }
  private void writeResponse(
      SolrQueryResponse solrRsp, QueryResponseWriter responseWriter, Method reqMethod)
      throws IOException {
    try {
      Object invalidStates = solrReq.getContext().get(CloudSolrClient.STATE_VERSION);
      // This is the last item added to the response and the client would expect it that way.
      // If that assumption is changed , it would fail. This is done to avoid an O(n) scan on
      // the response for each request
      if (invalidStates != null) solrRsp.add(CloudSolrClient.STATE_VERSION, invalidStates);
      // Now write it out
      final String ct = responseWriter.getContentType(solrReq, solrRsp);
      // don't call setContentType on null
      if (null != ct) response.setContentType(ct);

      if (solrRsp.getException() != null) {
        NamedList info = new SimpleOrderedMap();
        int code = ResponseUtils.getErrorInfo(solrRsp.getException(), info, log);
        solrRsp.add("error", info);
        response.setStatus(code);
      }

      if (Method.HEAD != reqMethod) {
        OutputStream out =
            new CloseShieldOutputStream(
                response.getOutputStream()); // Prevent close of container streams, see SOLR-8933
        QueryResponseWriterUtil.writeQueryResponse(out, responseWriter, solrReq, solrRsp, ct);
      }
      // else http HEAD request, nothing to write out, waited this long just to get ContentType
    } catch (EOFException e) {
      log.info("Unable to write response, client closed connection or we are shutting down", e);
    }
  }
Exemple #4
0
  protected void writeResponse(
      SolrQueryResponse solrRsp, QueryResponseWriter responseWriter, SolrQueryRequest solrReq)
      throws IOException {
    Object invalidStates = solrReq.getContext().get(CloudSolrClient.STATE_VERSION);
    // This is the last item added to the applyResult and the client would expect it that way.
    // If that assumption is changed , it would fail. This is done to avoid an O(n) scan on
    // the applyResult for each request
    if (invalidStates != null) solrRsp.add(CloudSolrClient.STATE_VERSION, invalidStates);
    // Now write it out
    final String ct = responseWriter.getContentType(solrReq, solrRsp);
    // don't call setContentType on null
    if (null != ct) {
      responseSetter.setContentType(ct);
    }

    if (solrRsp.getException() != null) {
      NamedList info = new SimpleOrderedMap();
      int code = ResponseUtils.getErrorInfo(solrRsp.getException(), info, logger);
      // solrRsp.add("error", info);
      // use protocol response exception instead of set 'error' response return to client,
      responseSetter.setSolrResponseException(code, info);
    }

    QueryResponseWriterUtil.writeQueryResponse(
        responseSetter.getResponseOutputStream(), responseWriter, solrReq, solrRsp, ct);

    // fire QueryResponse write Complete
    responseSetter.writeQueryResponseComplete(solrRsp);
  }
 /** Writes the response to the search query to the HTTP response's output stream. */
 private void writeResponse(
     SolrQuery query,
     HttpServletRequest httpRequest,
     HttpServletResponse httpResponse,
     QueryResponse queryResponse)
     throws IOException {
   SolrCore core = SearchActivator.getInstance().getSolrCore();
   // this seems to be the only way to obtain the JSON response representation
   SolrQueryRequest solrRequest = new LocalSolrQueryRequest(core, query.toNamedList());
   SolrQueryResponse solrResponse = new SolrQueryResponse();
   // bash the query in the response to remove user info
   NamedList<Object> params =
       (NamedList<Object>) queryResponse.getHeader().get("params"); // $NON-NLS-1$
   params.remove(CommonParams.Q);
   params.add(CommonParams.Q, httpRequest.getParameter(CommonParams.Q));
   NamedList<Object> values = queryResponse.getResponse();
   String contextPath = httpRequest.getContextPath();
   if (contextPath.length() > 0) setSearchResultContext(values, contextPath);
   solrResponse.setAllValues(values);
   QueryResponseWriter writer = core.getQueryResponseWriter("json"); // $NON-NLS-1$
   writer.write(httpResponse.getWriter(), solrRequest, solrResponse);
 }