Search in sources :

Example 1 with ResponseParser

use of org.apache.solr.client.solrj.ResponseParser in project lucene-solr by apache.

the class ShowFileRequestHandlerTest method testGetRawFile.

public void testGetRawFile() throws SolrServerException, IOException {
    SolrClient client = getSolrClient();
    //assertQ(req("qt", "/admin/file")); TODO file bug that SolrJettyTestBase extends SolrTestCaseJ4
    QueryRequest request = new QueryRequest(params("file", "managed-schema"));
    request.setPath("/admin/file");
    final AtomicBoolean readFile = new AtomicBoolean();
    request.setResponseParser(new ResponseParser() {

        @Override
        public String getWriterType() {
            //unfortunately this gets put onto params wt=mock but it apparently has no effect
            return "mock";
        }

        @Override
        public NamedList<Object> processResponse(InputStream body, String encoding) {
            try {
                if (body.read() >= 0)
                    readFile.set(true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return null;
        }

        @Override
        public NamedList<Object> processResponse(Reader reader) {
            //TODO
            throw new UnsupportedOperationException("TODO unimplemented");
        }
    });
    //runs request
    client.request(request);
    //request.process(client); but we don't have a NamedList response
    assertTrue(readFile.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) ResponseParser(org.apache.solr.client.solrj.ResponseParser) SolrClient(org.apache.solr.client.solrj.SolrClient) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) Reader(java.io.Reader) IOException(java.io.IOException)

Example 2 with ResponseParser

use of org.apache.solr.client.solrj.ResponseParser in project lucene-solr by apache.

the class TestDelegationTokenResponse method delegationTokenResponse.

private void delegationTokenResponse(DelegationTokenRequest request, DelegationTokenResponse response, String responseBody) throws Exception {
    ResponseParser parser = request.getResponseParser();
    response.setResponse(parser.processResponse(IOUtils.toInputStream(responseBody, "UTF-8"), "UTF-8"));
}
Also used : ResponseParser(org.apache.solr.client.solrj.ResponseParser)

Example 3 with ResponseParser

use of org.apache.solr.client.solrj.ResponseParser in project lucene-solr by apache.

the class HttpSolrClientBuilderTest method testDefaultsToBinaryResponseParserWhenNoneProvided.

@Test
public void testDefaultsToBinaryResponseParserWhenNoneProvided() throws IOException {
    try (HttpSolrClient createdClient = new Builder(ANY_BASE_SOLR_URL).build()) {
        final ResponseParser usedParser = createdClient.getParser();
        assertTrue(usedParser instanceof BinaryResponseParser);
    }
}
Also used : ResponseParser(org.apache.solr.client.solrj.ResponseParser) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Test(org.junit.Test)

Example 4 with ResponseParser

use of org.apache.solr.client.solrj.ResponseParser 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)

Example 5 with ResponseParser

use of org.apache.solr.client.solrj.ResponseParser in project lucene-solr by apache.

the class HttpSolrClient method createMethod.

protected HttpRequestBase createMethod(final SolrRequest request, String collection) throws IOException, SolrServerException {
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = DEFAULT_PATH;
    }
    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = this.parser;
    }
    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    if (parser != null) {
        wparams.set(CommonParams.WT, parser.getWriterType());
        wparams.set(CommonParams.VERSION, parser.getVersion());
    }
    if (invariantParams != null) {
        wparams.add(invariantParams);
    }
    String basePath = baseUrl;
    if (collection != null)
        basePath += "/" + collection;
    if (request instanceof V2Request) {
        if (System.getProperty("solr.v2RealPath") == null) {
            basePath = baseUrl.replace("/solr", "/v2");
        } else {
            basePath = baseUrl + "/____v2";
        }
    }
    if (SolrRequest.METHOD.GET == request.getMethod()) {
        if (streams != null) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
        }
        return new HttpGet(basePath + path + wparams.toQueryString());
    }
    if (SolrRequest.METHOD.DELETE == request.getMethod()) {
        return new HttpDelete(basePath + path + wparams.toQueryString());
    }
    if (SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod()) {
        String url = basePath + path;
        boolean hasNullStreamName = false;
        if (streams != null) {
            for (ContentStream cs : streams) {
                if (cs.getName() == null) {
                    hasNullStreamName = true;
                    break;
                }
            }
        }
        boolean isMultipart = ((this.useMultiPartPost && SolrRequest.METHOD.POST == request.getMethod()) || (streams != null && streams.size() > 1)) && !hasNullStreamName;
        LinkedList<NameValuePair> postOrPutParams = new LinkedList<>();
        if (streams == null || isMultipart) {
            // send server list and request list as query string params
            ModifiableSolrParams queryParams = calculateQueryParams(this.queryParams, wparams);
            queryParams.add(calculateQueryParams(request.getQueryParams(), wparams));
            String fullQueryUrl = url + queryParams.toQueryString();
            HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
            if (!isMultipart) {
                postOrPut.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            }
            List<FormBodyPart> parts = new LinkedList<>();
            Iterator<String> iter = wparams.getParameterNamesIterator();
            while (iter.hasNext()) {
                String p = iter.next();
                String[] vals = wparams.getParams(p);
                if (vals != null) {
                    for (String v : vals) {
                        if (isMultipart) {
                            parts.add(new FormBodyPart(p, new StringBody(v, StandardCharsets.UTF_8)));
                        } else {
                            postOrPutParams.add(new BasicNameValuePair(p, v));
                        }
                    }
                }
            }
            // TODO: remove deprecated - first simple attempt failed, see {@link MultipartEntityBuilder}
            if (isMultipart && streams != null) {
                for (ContentStream content : streams) {
                    String contentType = content.getContentType();
                    if (contentType == null) {
                        // default
                        contentType = BinaryResponseParser.BINARY_CONTENT_TYPE;
                    }
                    String name = content.getName();
                    if (name == null) {
                        name = "";
                    }
                    parts.add(new FormBodyPart(name, new InputStreamBody(content.getStream(), contentType, content.getName())));
                }
            }
            if (parts.size() > 0) {
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
                for (FormBodyPart p : parts) {
                    entity.addPart(p);
                }
                postOrPut.setEntity(entity);
            } else {
                //not using multipart
                postOrPut.setEntity(new UrlEncodedFormEntity(postOrPutParams, StandardCharsets.UTF_8));
            }
            return postOrPut;
        } else // It is has one stream, it is the post body, put the params in the URL
        {
            String fullQueryUrl = url + wparams.toQueryString();
            HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
            // Single stream as body
            // Using a loop just to get the first one
            final ContentStream[] contentStream = new ContentStream[1];
            for (ContentStream content : streams) {
                contentStream[0] = content;
                break;
            }
            if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                Long size = contentStream[0].getSize();
                postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {

                    @Override
                    public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                    }

                    @Override
                    public boolean isRepeatable() {
                        return false;
                    }
                });
            } else {
                Long size = contentStream[0].getSize();
                postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {

                    @Override
                    public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                    }

                    @Override
                    public boolean isRepeatable() {
                        return false;
                    }
                });
            }
            return postOrPut;
        }
    }
    throw new SolrServerException("Unsupported method: " + request.getMethod());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ResponseParser(org.apache.solr.client.solrj.ResponseParser) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpPut(org.apache.http.client.methods.HttpPut) FormBodyPart(org.apache.http.entity.mime.FormBodyPart) ContentStream(org.apache.solr.common.util.ContentStream) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) SolrException(org.apache.solr.common.SolrException) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) V2Request(org.apache.solr.client.solrj.request.V2Request) LinkedList(java.util.LinkedList) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) StringBody(org.apache.http.entity.mime.content.StringBody) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

ResponseParser (org.apache.solr.client.solrj.ResponseParser)8 Test (org.junit.Test)3 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)2 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 NamedList (org.apache.solr.common.util.NamedList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Header (org.apache.http.Header)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1