Example usage for javax.servlet ServletInputStream ServletInputStream

List of usage examples for javax.servlet ServletInputStream ServletInputStream

Introduction

In this page you can find the example usage for javax.servlet ServletInputStream ServletInputStream.

Prototype


protected ServletInputStream() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:org.apache.shindig.common.testing.FakeHttpServletRequest.java

/**
 * Get the body of the request (i.e. the POST data) as a binary stream. As per
 * Java docs, this OR getReader() may be called, but not both (attempting that
 * will result in an IllegalStateException)
 *
 *//*  w w w  .j  a  v  a2s . c  o  m*/
public ServletInputStream getInputStream() {
    if (getReaderCalled) {
        throw new IllegalStateException("getInputStream() called after getReader()");
    }
    getInputStreamCalled = true; // so that getReader() can no longer be called

    final InputStream in = new ByteArrayInputStream(postData);
    return new ServletInputStream() {
        @Override
        public int read() throws IOException {
            return in.read();
        }
    };
}

From source file:org.apache.shindig.gadgets.servlet.ServletUtilTest.java

@Test
public void testFromHttpServletRequest() throws Exception {
    HttpServletRequest original = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(original.getScheme()).andReturn("https");
    EasyMock.expect(original.getServerName()).andReturn("www.example.org");
    EasyMock.expect(original.getServerPort()).andReturn(444);
    EasyMock.expect(original.getRequestURI()).andReturn("/path/foo");
    EasyMock.expect(original.getQueryString()).andReturn("one=two&three=four");
    Vector<String> headerNames = new Vector<String>();
    headerNames.add("Header1");
    headerNames.add("Header2");
    EasyMock.expect(original.getHeaderNames()).andReturn(headerNames.elements());
    EasyMock.expect(original.getHeaders("Header1")).andReturn(makeEnumeration("HVal1", "HVal3"));
    EasyMock.expect(original.getHeaders("Header2")).andReturn(makeEnumeration("HVal2", "HVal4"));
    EasyMock.expect(original.getMethod()).andReturn("post");
    final ByteArrayInputStream bais = new ByteArrayInputStream("post body".getBytes());
    ServletInputStream sis = new ServletInputStream() {
        @Override/*  w  ww. jav  a  2 s  .c  o m*/
        public int read() throws IOException {
            return bais.read();
        }
    };
    EasyMock.expect(original.getInputStream()).andReturn(sis);
    EasyMock.expect(original.getRemoteAddr()).andReturn("1.2.3.4");

    EasyMock.replay(original);
    HttpRequest request = ServletUtil.fromHttpServletRequest(original);
    EasyMock.verify(original);

    assertEquals(Uri.parse("https://www.example.org:444/path/foo?one=two&three=four"), request.getUri());
    assertEquals(3, request.getHeaders().size());
    assertEquals("HVal1", request.getHeaders("Header1").get(0));
    assertEquals("HVal3", request.getHeaders("Header1").get(1));
    assertEquals("HVal2", request.getHeaders("Header2").get(0));
    assertEquals("HVal4", request.getHeaders("Header2").get(1));
    assertEquals("post", request.getMethod());
    assertEquals("post body", request.getPostBodyAsString());
    assertEquals("1.2.3.4", request.getParam(ServletUtil.REMOTE_ADDR_KEY));
}

From source file:org.apache.sling.servlethelpers.MockSlingHttpServletRequest.java

@Override
public ServletInputStream getInputStream() {
    if (content == null) {
        return null;
    }//  w w w.j ava  2  s .  com
    return new ServletInputStream() {
        private final InputStream is = new ByteArrayInputStream(content);

        @Override
        public int read() throws IOException {
            return is.read();
        }

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

        @Override
        public boolean isFinished() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setReadListener(ReadListener readListener) {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.apache.solr.servlet.SolrRequestParserTest.java

License:asdf

@Test
public void testParameterIncompatibilityException1() throws Exception {
    HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
    expect(request.getQueryString()).andReturn(null).anyTimes();
    // we emulate Jetty that returns empty stream when parameters were parsed before:
    expect(request.getInputStream()).andReturn(new ServletInputStream() {
        @Override//from   www  .j ava  2 s.c  o  m
        public int read() {
            return -1;
        }

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

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

        @Override
        public void setReadListener(ReadListener readListener) {

        }
    });
    replay(request);

    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());
    }
}

From source file:org.apache.wicket.protocol.http.mock.MockHttpServletRequest.java

/**
 * Returns an input stream if there has been added some uploaded files. Use
 * {@link #addFile(String, File, String)} to add some uploaded files.
 * //ww w .j  a v  a  2s.c  om
 * @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();
        }
    };
}

From source file:org.atomserver.server.servlet.BlockingFilter.java

private ServletRequest wrapServletRequest(ServletRequest servletRequest) throws IOException {
    final ServletInputStream originalInputStream = servletRequest.getInputStream();
    final int maxBytes = settings.getMaxContentLength();
    final ServletInputStream inputStream = new ServletInputStream() {
        int bytesRead = 0;

        public int read() throws IOException {
            if (bytesRead++ > maxBytes) {
                throw new TooMuchDataException("Content length exceeds the maximum length allowed.");
            }/*from  w  ww .  j a  v a 2  s .c o  m*/
            return originalInputStream.read();
        }
    };
    // wrap the original request, returning the wrapped input stream instead
    return new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {
        public ServletInputStream getInputStream() throws IOException {
            return inputStream;
        }
    };
}

From source file:org.biokoframework.http.RequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);

    ServletInputStream inputStream = new ServletInputStream() {
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }//from  ww w  .  j a v  a  2  s  .  c o m

        @Override
        public boolean isFinished() {
            return byteArrayInputStream.available() > 0;
        }

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

        @Override
        public void setReadListener(ReadListener readListener) {
            // TODO Auto-generated method stub

        }
    };

    return inputStream;
}

From source file:org.candlepin.common.filter.TeeHttpServletRequest.java

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
    ServletInputStream servletInputStream = new ServletInputStream() {
        @Override/*from   w  w  w  .  j  a  va2  s .  c o m*/
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }
    };
    return servletInputStream;
}

From source file:org.candlepin.servlet.filter.logging.LoggingRequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
    ServletInputStream servletInputStream = new ServletInputStream() {
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }//www  .j  ava  2 s .  com
    };
    return servletInputStream;
}

From source file:org.deegree.securityproxy.filter.RequestBodyWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    return new ServletInputStream() {

        private final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);

        @Override/*  w w w . j  a  v a2 s.  c o  m*/
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }

        @Override
        public void close() throws IOException {
            super.close();
            byteArrayInputStream.close();
        }
    };
}