Example usage for org.apache.solr.response XMLResponseWriter write

List of usage examples for org.apache.solr.response XMLResponseWriter write

Introduction

In this page you can find the example usage for org.apache.solr.response XMLResponseWriter write.

Prototype

@Override
    public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException 

Source Link

Usage

From source file:com.doculibre.constellio.wicket.servlet.SolrServletEmulator.java

License:Open Source License

@SuppressWarnings("deprecation")
public void writeResponse(String solrQuery, RecordCollection collection, ConstellioUser user,
        HttpServletRequest request, HttpServletResponse response) {
    OutputStream outputStream;/*w w  w .  ja  v  a2 s  .c o m*/
    try {
        outputStream = response.getOutputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (user != null && !user.hasSearchPermission(collection)) {
        throw new RuntimeException("The user doesn't have search permission on the collection");
    } else if (user == null && collection.hasSearchPermission()) {
        throw new RuntimeException("The collection requires a user with search permission");
    } else {
        SimpleSearch simpleSearch = new SimpleSearch();
        simpleSearch.setCollectionName(collection.getName());

        if (solrQuery.contains("facet=constellio")) {
            solrQuery = solrQuery.replace("facet=constellio", "facet=on");
            solrQuery = addConstellioFacets(simpleSearch, solrQuery, collection, user);
            LOGGER.info("Using constellio facets, new query is " + solrQuery);
        }
        ModifiableSolrParams solrParams = new ModifiableSolrParams(
                SolrRequestParsers.parseQueryString(solrQuery));

        String queryType = solrParams.get("qt");
        if (queryType != null && (queryType.toLowerCase().equals("spellchecker")
                || queryType.toLowerCase().equals("spellcheck"))) {
            writeSpellCheckerResponse(simpleSearch, solrParams, collection, user, outputStream);

        } else {
            String cdf = solrParams.get("cdf");
            if (cdf != null && cdf.equals("true")) {
                SearchResultFieldServices searchResultFieldServices = ConstellioSpringUtils
                        .getSearchResultFieldServices();
                List<SearchResultField> fields = searchResultFieldServices.list();
                List<String> fieldNames = new ArrayList<String>();
                for (SearchResultField field : fields) {
                    if (field.getRecordCollection().equals(collection)) {
                        fieldNames.add(field.getIndexField().getName());
                    }
                }
                solrParams.add("fl", StringUtils.join(fieldNames.toArray(), ","));
                solrParams.remove("cdf");
            }
            try {
                SolrServer solrServer = SolrCoreContext.getSolrServer(collection);
                QueryResponse queryResponse = solrServer.query(solrParams);

                if (queryResponse.getStatus() == 0) {
                    String ipAddress = request.getRemoteAddr();
                    simpleSearch.setQuery(solrParams.get("q"));
                    StatsServices statsServices = ConstellioSpringUtils.getStatsServices();
                    statsServices.logSearch(simpleSearch, queryResponse, ipAddress);
                    XMLResponseWriter xmlWriter = new XMLResponseWriter();
                    SolrQueryResponse sResponse = new SolrQueryResponse();
                    sResponse.setAllValues(queryResponse.getResponse());

                    try (OutputStreamWriter out = new OutputStreamWriter(outputStream)) {
                        xmlWriter.write(out, new LocalSolrQueryRequest(null, solrParams), sResponse);
                        out.flush();
                    } catch (IOException e) {
                        throw new RuntimeException("Unable to convert Solr response into XML", e);
                    }
                }
            } catch (SolrException e) {
                // if (!e.logged)
                LOGGER.log(Level.SEVERE, SolrException.toStr(e), e);
                // try {
                // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes());
                // } catch (IOException e1) {
                // throw new RuntimeException(e1);
                // }
                // if (!e.logged) SolrException.log(log,e);
                sendErr(e.code(), SolrException.toStr(e), response, outputStream);
            } catch (Throwable e) {
                LOGGER.log(Level.SEVERE, SolrException.toStr(e), e);
                // try {
                // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes());
                // } catch (IOException e1) {
                // throw new RuntimeException(e1);
                // }
                // SolrException.log(log,e);
                sendErr(500, SolrException.toStr(e), response, outputStream);
            }

            // final SolrCore core =
            // SolrCoreContext.getCores().getCore(collection.getName());
            // // SolrServletRequest solrReq = new SolrServletRequest(core,
            // request);
            // SolrQueryRequest solrReq = new LocalSolrQueryRequest(core,
            // solrParams);
            // SolrQueryResponse solrRsp = new SolrQueryResponse();
            // try {
            //
            // SolrRequestHandler handler =
            // core.getRequestHandler(solrReq.getParams().get(CommonParams.QT));
            // if (handler==null) {
            // LOGGER.log(Level.WARNING, "Unknown Request Handler '" +
            // solrReq.getParams().get(CommonParams.QT)
            // + "' :" + solrReq);
            // // log.warn("Unknown Request Handler '" +
            // solrReq.getQueryType() +"' :" + solrReq);
            // throw new
            // SolrException(SolrException.ErrorCode.BAD_REQUEST,"Unknown Request Handler '"
            // + solrReq.getParams().get(CommonParams.QT) + "'");
            // }
            // core.execute(handler, solrReq, solrRsp );
            // if (solrRsp.getException() == null) {
            // String ipAddress = request.getRemoteAddr();
            //
            // simpleSearch.setQuery(solrParams.get("q"));
            // QueryResponse qrsp = new QueryResponse();
            // qrsp.setResponse(getParsedResponse(solrReq, solrRsp));
            //
            // StatsServices statsServices =
            // ConstellioSpringUtils.getStatsServices();
            // statsServices.logSearch(simpleSearch, qrsp, ipAddress);
            //
            // QueryResponseWriter responseWriter =
            // core.getQueryResponseWriter(solrReq);
            // // Now write it out
            // final String ct = responseWriter.getContentType(solrReq,
            // solrRsp);
            // // don't call setContentType on null
            // if (null != ct) response.setContentType(ct);
            // if (responseWriter instanceof BinaryQueryResponseWriter) {
            // BinaryQueryResponseWriter binWriter =
            // (BinaryQueryResponseWriter) responseWriter;
            // binWriter.write(outputStream, solrReq, solrRsp);
            // } else {
            // String charset =
            // ContentStreamBase.getCharsetFromContentType(ct);
            // Writer out = (charset == null ||
            // charset.equalsIgnoreCase("UTF-8"))
            // ? new OutputStreamWriter(outputStream, UTF8)
            // : new OutputStreamWriter(outputStream, charset);
            // out = new FastWriter(out);
            // responseWriter.write(out, solrReq, solrRsp);
            // out.flush();
            // }
            // } else {
            // Exception e = solrRsp.getException();
            // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e);
            // //
            // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes());
            // int rc=500;
            // if (e instanceof SolrException) {
            // rc=((SolrException)e).code();
            // }
            // sendErr(rc, SolrException.toStr(e), response, outputStream);
            // }
            // } catch (SolrException e) {
            // // if (!e.logged)
            // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e);
            // // try {
            // //
            // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes());
            // // } catch (IOException e1) {
            // // throw new RuntimeException(e1);
            // // }
            // // if (!e.logged) SolrException.log(log,e);
            // sendErr(e.code(), SolrException.toStr(e), response,
            // outputStream);
            // } catch (Throwable e) {
            // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e);
            // // try {
            // //
            // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes());
            // // } catch (IOException e1) {
            // // throw new RuntimeException(e1);
            // // }
            // // SolrException.log(log,e);
            // sendErr(500, SolrException.toStr(e), response, outputStream);
            // } finally {
            // // This releases the IndexReader associated with the request
            // solrReq.close();
            // }
        }
    }
}

From source file:org.outermedia.solrfusion.TestHelper.java

License:Open Source License

public static InputStream embeddedQueryToXmlInputStream(SolrParams request, QueryResponse response) {
    XMLResponseWriter xmlWriter = new XMLResponseWriter();
    Writer w = new StringWriter();
    SolrQueryResponse sResponse = new SolrQueryResponse();
    sResponse.setAllValues(response.getResponse());
    try {/*from w ww  .  j a  v a  2 s  .c  om*/
        xmlWriter.write(w, new LocalSolrQueryRequest(null, request), sResponse);
    } catch (IOException e) {
        throw new RuntimeException("Unable to convert Solr response into XML", e);
    }
    StringReader stringReader = new StringReader(w.toString());

    ReaderInputStream readerInputStream = new ReaderInputStream(stringReader);
    return readerInputStream;
}