Search in sources :

Example 11 with ReadListener

use of javax.servlet.ReadListener 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 12 with ReadListener

use of javax.servlet.ReadListener in project solo by b3log.

the class MetaWeblogAPITestCase method metaWeblog.

/**
     * 手动构造rpc请求
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void metaWeblog() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/apis/metaweblog");
    when(request.getMethod()).thenReturn("POST");
    //        Date date = (Date) DateFormatUtils.ISO_DATETIME_FORMAT.parseObject("2004-05-03T17:30:08");
    Date date = DateUtils.parseDate("20040503T17:30:08", new String[] { "yyyyMMdd'T'HH:mm:ss", "yyyyMMdd'T'HH:mm:ss'Z'" });
    final class MockServletInputStream extends ServletInputStream {

        private ByteArrayInputStream stream;

        public MockServletInputStream(byte[] data) {
            stream = new ByteArrayInputStream(data);
        }

        public int read() throws IOException {
            return stream.read();
        }

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

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

        @Override
        public void setReadListener(ReadListener readListener) {
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>").append("<methodCall>").append("<methodName>metaWeblog.newPost</methodName>").append("<params>").append("<param>").append("<value><int>11</int></value>").append("</param>").append("<param>").append("<value><string>test@gmail.com</string></value>").append("</param>").append("<param>").append("<value><string>pass</string></value>").append("</param>").append("<param>").append("<value>").append("<struct>").append("<member>").append("<name>dateCreated</name>").append("<value><dateTime.iso8601>20040503T17:30:08</dateTime.iso8601></value>").append("</member>").append("<member>").append("<name>title</name>").append("<value><string>title</string></value>").append("</member>").append("<member>").append("<name>description</name>").append("<value><string>description</string></value>").append("</member>").append("</struct>").append("</value>").append("</param>").append("<param>").append("<value><boolean>1</boolean></value>").append("</param>").append("</params>").append("</methodCall>");
    when(request.getInputStream()).thenReturn(new MockServletInputStream(sb.toString().getBytes()));
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    System.out.println("xxxxxcontent:" + content);
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ReadListener(javax.servlet.ReadListener) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 13 with ReadListener

use of javax.servlet.ReadListener in project wicket by apache.

the class MockHttpServletRequest method getInputStream.

/**
 * Returns an input stream if there has been added some uploaded files. Use
 * {@link #addFile(String, File, String)} to add some uploaded files.
 *
 * @return The input stream
 * @throws IOException
 *             If an I/O related problem occurs
 */
@Override
public ServletInputStream getInputStream() throws IOException {
    byte[] request = buildRequest();
    // Ok lets make an input stream to return
    final ByteArrayInputStream bais = new ByteArrayInputStream(request);
    return new ServletInputStream() {

        private boolean isFinished = false;

        private boolean isReady = true;

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

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

        @Override
        public void setReadListener(ReadListener readListener) {
        }

        @Override
        public int read() {
            isFinished = true;
            isReady = false;
            return bais.read();
        }
    };
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadListener(javax.servlet.ReadListener)

Example 14 with ReadListener

use of javax.servlet.ReadListener in project teiid by teiid.

the class TestGzipMessageRequest method prepareRequest.

@Before
public void prepareRequest() throws IOException {
    streamBytes = new ByteArrayInputStream(TestGzipMessageResponse.TEST_STRING_IN_GZIP);
    stream = new ServletInputStream() {

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

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

        @Override
        public void setReadListener(ReadListener readListener) {
        }

        @Override
        public int read() throws IOException {
            return streamBytes.read();
        }
    };
    request = new GzipMessageRequest(mockRequest(stream));
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ReadListener(javax.servlet.ReadListener) Before(org.junit.Before)

Example 15 with ReadListener

use of javax.servlet.ReadListener in project shijie by tengxing.

the class XssHttpServletRequestWrapper method getInputStream.

public ServletInputStream getInputStream() throws IOException {
    if (!"application/json".equalsIgnoreCase(super.getHeader("Content-Type"))) {
        return super.getInputStream();
    } else {
        String json = IOUtils.toString(super.getInputStream(), "utf-8");
        if (StringUtils.isBlank(json)) {
            return super.getInputStream();
        } else {
            json = this.xssEncode(json);
            final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("utf-8"));
            return new ServletInputStream() {

                public boolean isFinished() {
                    return true;
                }

                public boolean isReady() {
                    return true;
                }

                public void setReadListener(ReadListener readListener) {
                }

                public int read() throws IOException {
                    return bis.read();
                }
            };
        }
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadListener(javax.servlet.ReadListener)

Aggregations

ReadListener (javax.servlet.ReadListener)27 ServletInputStream (javax.servlet.ServletInputStream)22 HttpServletRequest (javax.servlet.http.HttpServletRequest)19 IOException (java.io.IOException)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)18 ServletException (javax.servlet.ServletException)14 Test (org.junit.Test)14 AsyncContext (javax.servlet.AsyncContext)13 HttpServlet (javax.servlet.http.HttpServlet)12 InterruptedIOException (java.io.InterruptedIOException)11 UncheckedIOException (java.io.UncheckedIOException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)9 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ServletOutputStream (javax.servlet.ServletOutputStream)5 Response (org.eclipse.jetty.client.api.Response)4 Result (org.eclipse.jetty.client.api.Result)4 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)4