use of javax.servlet.WriteListener in project sling by apache.
the class ClientAbortExceptionTest method setup.
@Before
public void setup() throws IOException {
context = new Mockery();
final ServletOutputStream sos = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
throw new IOException("Always fails, on purpose");
}
@Override
public void flush() throws IOException {
throw new IOException("Always fails, on purpose");
}
@Override
public void close() throws IOException {
throw new IOException("Always fails, on purpose");
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
final HttpServletResponse raw = context.mock(HttpServletResponse.class);
context.checking(new Expectations() {
{
allowing(raw).getOutputStream();
will(returnValue(sos));
}
});
r = new RequestLoggerResponse(raw);
}
use of javax.servlet.WriteListener in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testDownloadMetadata.
@Test(timeout = 30000)
public void testDownloadMetadata() 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
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String result = null;
if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + " <groupId>org.apache.camel</groupId>\n" + " <artifactId>camel-core</artifactId>\n" + " <versioning>\n" + " <latest>2.14.0</latest>\n" + " <release>2.14.0</release>\n" + " <versions>\n" + " <version>1.6.1</version>\n" + " <version>1.6.2</version>\n" + " <version>1.6.3</version>\n" + " <version>1.6.4</version>\n" + " <version>2.0-M2</version>\n" + " <version>2.0-M3</version>\n" + " <version>2.0.0</version>\n" + " <version>2.1.0</version>\n" + " <version>2.2.0</version>\n" + " <version>2.3.0</version>\n" + " <version>2.4.0</version>\n" + " <version>2.5.0</version>\n" + " <version>2.6.0</version>\n" + " <version>2.7.0</version>\n" + " <version>2.7.1</version>\n" + " <version>2.7.2</version>\n" + " <version>2.7.3</version>\n" + " <version>2.7.4</version>\n" + " <version>2.7.5</version>\n" + " <version>2.8.0</version>\n" + " <version>2.8.1</version>\n" + " <version>2.8.2</version>\n" + " <version>2.8.3</version>\n" + " <version>2.8.4</version>\n" + " <version>2.8.5</version>\n" + " <version>2.8.6</version>\n" + " <version>2.9.0-RC1</version>\n" + " <version>2.9.0</version>\n" + " <version>2.9.1</version>\n" + " <version>2.9.2</version>\n" + " <version>2.9.3</version>\n" + " <version>2.9.4</version>\n" + " <version>2.9.5</version>\n" + " <version>2.9.6</version>\n" + " <version>2.9.7</version>\n" + " <version>2.9.8</version>\n" + " <version>2.10.0</version>\n" + " <version>2.10.1</version>\n" + " <version>2.10.2</version>\n" + " <version>2.10.3</version>\n" + " <version>2.10.4</version>\n" + " <version>2.10.5</version>\n" + " <version>2.10.6</version>\n" + " <version>2.10.7</version>\n" + " <version>2.11.0</version>\n" + " <version>2.11.1</version>\n" + " <version>2.11.2</version>\n" + " <version>2.11.3</version>\n" + " <version>2.11.4</version>\n" + " <version>2.12.0</version>\n" + " <version>2.12.1</version>\n" + " <version>2.12.2</version>\n" + " <version>2.12.3</version>\n" + " <version>2.12.4</version>\n" + " <version>2.13.0</version>\n" + " <version>2.13.1</version>\n" + " <version>2.13.2</version>\n" + " <version>2.14.0</version>\n" + " </versions>\n" + " <lastUpdated>20140918132816</lastUpdated>\n" + " </versioning>\n" + "</metadata>\n" + "\n";
} else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n" + " <groupId>org.apache.camel</groupId>\n" + " <artifactId>camel-core</artifactId>\n" + " <versioning>\n" + " <latest>2.14.0.redhat-620034</latest>\n" + " <release>2.14.0.redhat-620034</release>\n" + " <versions>\n" + " <version>2.10.0.redhat-60074</version>\n" + " <version>2.12.0.redhat-610312</version>\n" + " <version>2.12.0.redhat-610328</version>\n" + " <version>2.12.0.redhat-610355</version>\n" + " <version>2.12.0.redhat-610378</version>\n" + " <version>2.12.0.redhat-610396</version>\n" + " <version>2.12.0.redhat-610399</version>\n" + " <version>2.12.0.redhat-610401</version>\n" + " <version>2.12.0.redhat-610402</version>\n" + " <version>2.12.0.redhat-611403</version>\n" + " <version>2.12.0.redhat-611405</version>\n" + " <version>2.12.0.redhat-611406</version>\n" + " <version>2.12.0.redhat-611408</version>\n" + " <version>2.12.0.redhat-611409</version>\n" + " <version>2.12.0.redhat-611410</version>\n" + " <version>2.12.0.redhat-611411</version>\n" + " <version>2.12.0.redhat-611412</version>\n" + " <version>2.14.0.redhat-620031</version>\n" + " <version>2.14.0.redhat-620033</version>\n" + " <version>2.14.0.redhat-620034</version>\n" + " </versions>\n" + " <lastUpdated>20141019130841</lastUpdated>\n" + " </versioning>\n" + "</metadata>\n" + "\n";
}
if (result == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
baseRequest.setHandled(true);
response.getOutputStream().close();
} else {
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getOutputStream().write(result.getBytes());
response.getOutputStream().close();
}
}
});
server.start();
try {
int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
List<String> remoteRepos = Arrays.asList("http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2");
RuntimeProperties props = new MockRuntimeProperties();
// TODO: local repo should point to target/tmp
MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
AsyncContext context = EasyMock.createMock(AsyncContext.class);
EasyMock.makeThreadSafe(context, true);
final Map<String, Object> attributes = new HashMap<>();
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}
};
EasyMock.makeThreadSafe(request, true);
EasyMock.expect(request.getMethod()).andReturn("GET");
EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.makeThreadSafe(response, true);
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();
response.flushBuffer();
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchDispatch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchDispatch.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.start();
servlet.doGet(requestWrapper, response);
latchDispatch.await();
EasyMock.verify(request, response, context);
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}).anyTimes();
response.flushBuffer();
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchComplete = new CountDownLatch(1);
context.complete();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchComplete.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
EasyMock.verify(request, response, context);
org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader().read(new ByteArrayInputStream(baos.toByteArray()), false);
assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));
EasyMock.verify(request, response, context);
} finally {
server.stop();
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of javax.servlet.WriteListener in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testDownload.
private void testDownload(Handler serverHandler) 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(serverHandler);
server.start();
try {
int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
RuntimeProperties props = new MockRuntimeProperties();
// TODO: local repo should point to target/tmp
MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
AsyncContext context = EasyMock.createMock(AsyncContext.class);
EasyMock.makeThreadSafe(context, true);
final Map<String, Object> attributes = new HashMap<>();
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}
};
EasyMock.makeThreadSafe(request, true);
EasyMock.expect(request.getMethod()).andReturn("GET");
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.makeThreadSafe(response, true);
response.setStatus(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentLength(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentType((String) EasyMock.anyObject());
EasyMock.expectLastCall();
response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
EasyMock.expectLastCall();
response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchDispatch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchDispatch.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.start();
servlet.doGet(requestWrapper, response);
latchDispatch.await();
EasyMock.verify(request, response, context);
//
// Subsequent call
//
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ServletOutputStream outputStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
while (true) {
long size = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".size");
long pos = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".position");
ByteBuffer buffer = (ByteBuffer) attributes.get(ByteBuffer.class.getName());
if (pos + buffer.position() >= size) {
break;
}
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latch.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latch.await();
EasyMock.verify(request, response, context);
}
//
// Last calls
//
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latchComplete = new CountDownLatch(1);
context.complete();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchComplete.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
response.flushBuffer();
EasyMock.expectLastCall();
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latchComplete.await();
EasyMock.verify(request, response, context);
Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());
} finally {
server.stop();
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of javax.servlet.WriteListener in project incubator-servicecomb-java-chassis by apache.
the class TestStandardHttpServletResponseEx method flushBuffer.
@Test
public void flushBuffer() throws IOException {
Buffer buffer = Buffer.buffer();
ServletOutputStream output = new ServletOutputStream() {
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
buffer.appendByte((byte) b);
}
};
response = new MockUp<HttpServletResponse>() {
@Mock
ServletOutputStream getOutputStream() {
return output;
}
}.getMockInstance();
responseEx = new StandardHttpServletResponseEx(response);
// no body
responseEx.flushBuffer();
Assert.assertEquals(0, buffer.length());
Buffer body = Buffer.buffer().appendString("body");
responseEx.setBodyBuffer(body);
responseEx.flushBuffer();
Assert.assertEquals("body", buffer.toString());
}
use of javax.servlet.WriteListener in project knox by apache.
the class RMHaDispatchTest method testConnectivityFailure.
@Test
public void testConnectivityFailure() throws Exception {
String serviceName = "RESOURCEMANAGER";
HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
descriptor.addServiceConfig(HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null));
HaProvider provider = new DefaultHaProvider(descriptor);
URI uri1 = new URI("http://unreachable-host");
URI uri2 = new URI("http://reachable-host");
ArrayList<String> urlList = new ArrayList<>();
urlList.add(uri1.toString());
urlList.add(uri2.toString());
provider.addHaService(serviceName, urlList);
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(provider).anyTimes();
BasicHttpParams params = new BasicHttpParams();
HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class);
EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes();
EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)).once();
HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() {
@Override
public ServletOutputStream answer() throws Throwable {
return new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
throw new IOException("unreachable-host");
}
@Override
public void setWriteListener(WriteListener arg0) {
}
@Override
public boolean isReady() {
return false;
}
};
}
}).once();
EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
RMHaDispatch dispatch = new RMHaDispatch();
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
dispatch.setHttpClient(client);
dispatch.setHaProvider(provider);
dispatch.init();
long startTime = System.currentTimeMillis();
try {
dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
} catch (IOException e) {
// this is expected after the failover limit is reached
}
long elapsedTime = System.currentTimeMillis() - startTime;
Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
// test to make sure the sleep took place
Assert.assertTrue(elapsedTime > 1000);
}
Aggregations