Search in sources :

Example 1 with FormDataRequestParser

use of org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser in project lucene-solr by apache.

the class SolrRequestParserTest method testParameterIncompatibilityException2.

@Test
public void testParameterIncompatibilityException2() throws Exception {
    HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
    when(request.getMethod()).thenReturn("POST");
    // we emulate Tomcat that throws IllegalStateException when parameters were parsed before:
    when(request.getInputStream()).thenThrow(new IllegalStateException());
    FormDataRequestParser formdata = new FormDataRequestParser(2048);
    try {
        formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
        fail("should throw SolrException");
    } catch (SolrException solre) {
        assertTrue(solre.getMessage().startsWith("Solr requires that request parameters"));
        assertEquals(500, solre.code());
    }
    verify(request).getInputStream();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 2 with FormDataRequestParser

use of org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser in project lucene-solr by apache.

the class SolrRequestParserTest method testParameterIncompatibilityException1.

@Test
public void testParameterIncompatibilityException1() throws Exception {
    HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
    // we emulate Jetty that returns empty stream when parameters were parsed before:
    when(request.getInputStream()).thenReturn(new ServletInputStream() {

        @Override
        public int read() {
            return -1;
        }

        @Override
        public boolean isFinished() {
            return true;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }
    });
    FormDataRequestParser formdata = new FormDataRequestParser(2048);
    try {
        formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
        fail("should throw SolrException");
    } catch (SolrException solre) {
        assertTrue(solre.getMessage().startsWith("Solr requires that request parameters"));
        assertEquals(500, solre.code());
    }
    verify(request).getInputStream();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) ServletInputStream(javax.servlet.ServletInputStream) ReadListener(javax.servlet.ReadListener) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 3 with FormDataRequestParser

use of org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser in project lucene-solr by apache.

the class SolrRequestParserTest method testStandardFormdataUploadLimit.

@Test
public void testStandardFormdataUploadLimit() throws Exception {
    final int limitKBytes = 128;
    final StringBuilder large = new StringBuilder("q=hello");
    // grow exponentially to reach 128 KB limit:
    while (large.length() <= limitKBytes * 1024) {
        large.append('&').append(large);
    }
    HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", -1);
    when(request.getMethod()).thenReturn("POST");
    when(request.getInputStream()).thenReturn(new ByteServletInputStream(large.toString().getBytes(StandardCharsets.US_ASCII)));
    FormDataRequestParser formdata = new FormDataRequestParser(limitKBytes);
    try {
        formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
        fail("should throw SolrException");
    } catch (SolrException solre) {
        assertTrue(solre.getMessage().contains("upload limit"));
        assertEquals(400, solre.code());
    }
    verify(request).getInputStream();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 4 with FormDataRequestParser

use of org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser 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 5 with FormDataRequestParser

use of org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser 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)

Aggregations

HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ContentStream (org.apache.solr.common.util.ContentStream)5 FormDataRequestParser (org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser)5 Test (org.junit.Test)5 SolrException (org.apache.solr.common.SolrException)3 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)2 SolrParams (org.apache.solr.common.params.SolrParams)2 MultipartRequestParser (org.apache.solr.servlet.SolrRequestParsers.MultipartRequestParser)2 RawRequestParser (org.apache.solr.servlet.SolrRequestParsers.RawRequestParser)2 StandardRequestParser (org.apache.solr.servlet.SolrRequestParsers.StandardRequestParser)2 ReadListener (javax.servlet.ReadListener)1 ServletInputStream (javax.servlet.ServletInputStream)1