Search in sources :

Example 26 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SolrRequestParserTest method testStandardParseParamsAndFillStreamsISO88591.

@Test
public void testStandardParseParamsAndFillStreamsISO88591() throws Exception {
    final String getParams = "qt=%FC&dup=foo&ie=iso-8859-1&dup=%FC", postParams = "qt2=%FC&q=hello&d%75p=bar";
    final byte[] postBytes = postParams.getBytes(StandardCharsets.US_ASCII);
    final String contentType = "application/x-www-form-urlencoded; charset=iso-8859-1";
    // Set up the expected behavior
    HttpServletRequest request = getMock("/solr/select", contentType, postBytes.length);
    when(request.getMethod()).thenReturn("POST");
    when(request.getQueryString()).thenReturn(getParams);
    when(request.getInputStream()).thenReturn(new ByteServletInputStream(postBytes));
    MultipartRequestParser multipart = new MultipartRequestParser(2048);
    RawRequestParser raw = new RawRequestParser();
    FormDataRequestParser formdata = new FormDataRequestParser(2048);
    StandardRequestParser standard = new StandardRequestParser(multipart, raw, formdata);
    SolrParams p = standard.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
    assertEquals("contentType: " + contentType, "hello", p.get("q"));
    assertEquals("contentType: " + contentType, "ü", p.get("qt"));
    assertEquals("contentType: " + contentType, "ü", p.get("qt2"));
    assertArrayEquals("contentType: " + contentType, new String[] { "foo", "ü", "bar" }, p.getParams("dup"));
    verify(request).getInputStream();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) RawRequestParser(org.apache.solr.servlet.SolrRequestParsers.RawRequestParser) SolrParams(org.apache.solr.common.params.SolrParams) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) MultipartRequestParser(org.apache.solr.servlet.SolrRequestParsers.MultipartRequestParser) StandardRequestParser(org.apache.solr.servlet.SolrRequestParsers.StandardRequestParser) Test(org.junit.Test)

Example 27 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SolrRequestParserTest method testStreamFile.

@Test
public void testStreamFile() throws Exception {
    File file = getFile("README");
    byte[] bytes = FileUtils.readFileToByteArray(file);
    SolrCore core = h.getCore();
    Map<String, String[]> args = new HashMap<>();
    args.put(CommonParams.STREAM_FILE, new String[] { file.getAbsolutePath() });
    // Make sure it got a single stream in and out ok
    List<ContentStream> streams = new ArrayList<>();
    try (SolrQueryRequest req = parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams)) {
        assertEquals(1, streams.size());
        try (InputStream in = streams.get(0).getStream()) {
            assertArrayEquals(bytes, IOUtils.toByteArray(in));
        }
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) BufferedInputStream(java.io.BufferedInputStream) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 28 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SolrRequestParserTest method testStandardParseParamsAndFillStreams.

@Test
public void testStandardParseParamsAndFillStreams() throws Exception {
    final String getParams = "qt=%C3%BC&dup=foo", postParams = "q=hello&d%75p=bar";
    final byte[] postBytes = postParams.getBytes(StandardCharsets.US_ASCII);
    // Set up the expected behavior
    final String[] ct = new String[] { "application/x-www-form-urlencoded", "Application/x-www-form-urlencoded", "application/x-www-form-urlencoded; charset=utf-8", "application/x-www-form-urlencoded;" };
    for (String contentType : ct) {
        HttpServletRequest request = getMock("/solr/select", contentType, postBytes.length);
        when(request.getMethod()).thenReturn("POST");
        when(request.getQueryString()).thenReturn(getParams);
        when(request.getInputStream()).thenReturn(new ByteServletInputStream(postBytes));
        MultipartRequestParser multipart = new MultipartRequestParser(2048);
        RawRequestParser raw = new RawRequestParser();
        FormDataRequestParser formdata = new FormDataRequestParser(2048);
        StandardRequestParser standard = new StandardRequestParser(multipart, raw, formdata);
        SolrParams p = standard.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
        assertEquals("contentType: " + contentType, "hello", p.get("q"));
        assertEquals("contentType: " + contentType, "ü", p.get("qt"));
        assertArrayEquals("contentType: " + contentType, new String[] { "foo", "bar" }, p.getParams("dup"));
        verify(request).getInputStream();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) RawRequestParser(org.apache.solr.servlet.SolrRequestParsers.RawRequestParser) SolrParams(org.apache.solr.common.params.SolrParams) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) MultipartRequestParser(org.apache.solr.servlet.SolrRequestParsers.MultipartRequestParser) StandardRequestParser(org.apache.solr.servlet.SolrRequestParsers.StandardRequestParser) Test(org.junit.Test)

Example 29 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class BinaryRequestWriter method getContentStreams.

@Override
public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException {
    if (req instanceof UpdateRequest) {
        UpdateRequest updateRequest = (UpdateRequest) req;
        if (isNull(updateRequest.getDocuments()) && isNull(updateRequest.getDeleteByIdMap()) && isNull(updateRequest.getDeleteQuery()) && (updateRequest.getDocIterator() == null)) {
            return null;
        }
        List<ContentStream> l = new ArrayList<>();
        l.add(new LazyContentStream(updateRequest));
        return l;
    } else {
        return super.getContentStreams(req);
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ArrayList(java.util.ArrayList)

Example 30 with ContentStream

use of org.apache.solr.common.util.ContentStream 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

ContentStream (org.apache.solr.common.util.ContentStream)45 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)15 SolrException (org.apache.solr.common.SolrException)13 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)12 SolrParams (org.apache.solr.common.params.SolrParams)11 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)10 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)8 HashMap (java.util.HashMap)7 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 SolrCore (org.apache.solr.core.SolrCore)7 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)7 IOException (java.io.IOException)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 NamedList (org.apache.solr.common.util.NamedList)6 Reader (java.io.Reader)5 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 FormDataRequestParser (org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser)5