List of usage examples for javax.servlet ServletInputStream ServletInputStream
protected ServletInputStream()
From source file:de.micromata.genome.tpsb.httpmockup.MockHttpServletRequest.java
@Override public ServletInputStream getInputStream() throws IOException { if (requestData == null) { return null; }/*from www.ja v a 2s.co m*/ final ByteArrayInputStream bis = new ByteArrayInputStream(requestData); return new ServletInputStream() { @Override public int read() throws IOException { return bis.read(); } @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } }; }
From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java
private Map<String, String> testUpload(String path, final byte[] contents, String location, String profile, String version, boolean hasLocationHeader) throws Exception { final String old = System.getProperty("karaf.data"); System.setProperty("karaf.data", new File("target").getCanonicalPath()); FileUtils.deleteDirectory(new File("target/tmp")); Server server = new Server(0); server.setHandler(new AbstractHandler() { @Override//ww w. j av a2 s . c o m public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } }); server.start(); try { int localPort = server.getConnectors()[0].getLocalPort(); List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central"); RuntimeProperties props = new MockRuntimeProperties(); MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null); MavenUploadProxyServlet servlet = new MavenUploadProxyServlet(resolver, props, projectDeployer); HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class); EasyMock.expect(request.getPathInfo()).andReturn(path); EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() { private int i; @Override public int read() throws IOException { if (i >= contents.length) { return -1; } return (contents[i++] & 0xFF); } }); EasyMock.expect(request.getHeader("X-Location")).andReturn(location); EasyMock.expect(request.getParameter("profile")).andReturn(profile); EasyMock.expect(request.getParameter("version")).andReturn(version); final Map<String, String> headers = new HashMap<>(); HttpServletResponse rm = EasyMock.createMock(HttpServletResponse.class); HttpServletResponse response = new HttpServletResponseWrapper(rm) { @Override public void addHeader(String name, String value) { headers.put(name, value); } }; response.setStatus(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentLength(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentType((String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong()); EasyMock.expectLastCall().anyTimes(); response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); EasyMock.replay(request, rm); servlet.start(); servlet.doPut(request, response); EasyMock.verify(request, rm); Assert.assertEquals(hasLocationHeader, headers.containsKey("X-Location")); return headers; } finally { server.stop(); if (old != null) { System.setProperty("karaf.data", old); } } }
From source file:org.apache.click.servlet.MockRequest.java
/** * 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//from w w w . java2 s . c o m * @throws IOException If an I/O related problem occurs */ public ServletInputStream getInputStream() throws IOException { if (uploadedFiles != null && uploadedFiles.size() > 0) { byte[] request = buildRequest(); // Ok lets make an input stream to return final ByteArrayInputStream bais = new ByteArrayInputStream(request); return new ServletInputStream() { @Override public int read() { return bais.read(); } }; } else { return new ServletInputStream() { @Override public int read() { return -1; } }; } }
From source file:org.apache.cocoon.servletservice.util.ServletServiceRequest.java
public void setInputStream(final InputStream inputStream) { try {//from w w w.j a va 2 s.com this.contentLength = inputStream.available(); } catch (IOException e) { this.contentLength = -1; } this.content = new ServletInputStream() { public int read() throws IOException { return inputStream.read(); } }; }
From source file:org.apache.falcon.resource.AbstractTestBase.java
protected ServletInputStream getServletInputStream(final InputStream stream) { return new ServletInputStream() { @Override/*from ww w. j av a2 s .c o m*/ public int read() throws IOException { return stream.read(); } }; }
From source file:org.apache.falcon.resource.EntityManagerTest.java
/** * Converts a InputStream into ServletInputStream. * * @param resourceName resource name/*from w w w .ja v a 2 s. c o m*/ * @return ServletInputStream */ private ServletInputStream getServletInputStream(String resourceName) { final InputStream stream = this.getClass().getResourceAsStream(resourceName); return new ServletInputStream() { @Override public int read() throws IOException { return stream.read(); } }; }
From source file:org.apache.falcon.resource.proxy.ExtensionManagerProxy.java
private HttpServletRequest getEntityStream(Entity entity, EntityType type, HttpServletRequest request) throws IOException, JAXBException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); type.getMarshaller().marshal(entity, baos); final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray()); ServletInputStream servletInputStream = new ServletInputStream() { public int read() throws IOException { return byteArrayInputStream.read(); }/*from w ww . jav a2 s . c o m*/ }; return getBufferedRequest(new HttpServletRequestInputStreamWrapper(request, servletInputStream)); }
From source file:org.apache.falcon.resource.TestContext.java
public ServletInputStream getServletInputStream(final InputStream stream) { return new ServletInputStream() { @Override//from w w w . j ava 2 s. com public int read() throws IOException { return stream.read(); } }; }
From source file:org.apache.karaf.cave.server.maven.MavenProxyServletTest.java
private Map<String, String> testUpload(String path, final byte[] contents, String location, String profile, String version, boolean hasLocationHeader) throws Exception { final String old = System.getProperty("karaf.data"); System.setProperty("karaf.data", new File("target").getCanonicalPath()); FileUtils.deleteDirectory(new File("target/tmp")); Server server = new Server(0); server.setHandler(new AbstractHandler() { @Override/*ww w.j ava 2s . com*/ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } }); server.start(); try { int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort(); MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/maven2@id=central", "http", "localhost", localPort, "fuse", "fuse", null); CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null); HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class); EasyMock.expect(request.getPathInfo()).andReturn(path); EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() { private int i; @Override public int read() throws IOException { if (i >= contents.length) { return -1; } return (contents[i++] & 0xFF); } @Override public boolean isFinished() { // TODO Auto-generated method stub return false; } @Override public boolean isReady() { // TODO Auto-generated method stub return true; } @Override public void setReadListener(ReadListener readListener) { // TODO Auto-generated method stub } }); EasyMock.expect(request.getHeader("X-Location")).andReturn(location); final Map<String, String> headers = new HashMap<>(); HttpServletResponse rm = EasyMock.createMock(HttpServletResponse.class); HttpServletResponse response = new HttpServletResponseWrapper(rm) { @Override public void addHeader(String name, String value) { headers.put(name, value); } }; response.setStatus(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentLength(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentType((String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong()); EasyMock.expectLastCall().anyTimes(); response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); EasyMock.replay(request, rm); servlet.init(); servlet.doPut(request, response); EasyMock.verify(request, rm); Assert.assertEquals(hasLocationHeader, headers.containsKey("X-Location")); return headers; } finally { server.stop(); if (old != null) { System.setProperty("karaf.data", old); } } }
From source file:org.apache.karaf.services.mavenproxy.internal.MavenProxyServletTest.java
private Map<String, String> testUpload(String path, final byte[] contents, String location, String profile, String version, boolean hasLocationHeader) throws Exception { final String old = System.getProperty("karaf.data"); System.setProperty("karaf.data", new File("target").getCanonicalPath()); FileUtils.deleteDirectory(new File("target/tmp")); Server server = new Server(0); server.setHandler(new AbstractHandler() { @Override/* www . ja v a 2 s. c o m*/ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } }); server.start(); try { int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort(); MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/maven2@id=central", "http", "localhost", localPort, "fuse", "fuse", null); MavenProxyServlet servlet = new MavenProxyServlet(resolver, 5, null, null, null); HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class); EasyMock.expect(request.getPathInfo()).andReturn(path); EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() { private int i; @Override public int read() throws IOException { if (i >= contents.length) { return -1; } return (contents[i++] & 0xFF); } }); EasyMock.expect(request.getHeader("X-Location")).andReturn(location); final Map<String, String> headers = new HashMap<>(); HttpServletResponse rm = EasyMock.createMock(HttpServletResponse.class); HttpServletResponse response = new HttpServletResponseWrapper(rm) { @Override public void addHeader(String name, String value) { headers.put(name, value); } }; response.setStatus(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentLength(EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); response.setContentType((String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong()); EasyMock.expectLastCall().anyTimes(); response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject()); EasyMock.expectLastCall().anyTimes(); EasyMock.replay(request, rm); servlet.init(); servlet.doPut(request, response); EasyMock.verify(request, rm); Assert.assertEquals(hasLocationHeader, headers.containsKey("X-Location")); return headers; } finally { server.stop(); if (old != null) { System.setProperty("karaf.data", old); } } }