commit
bdf5854610
|
@ -28,6 +28,7 @@ import java.util.List;
|
|||
import java.util.Queue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletException;
|
||||
|
@ -52,6 +53,7 @@ import org.eclipse.jetty.util.Callback;
|
|||
import org.eclipse.jetty.util.CountingCallback;
|
||||
import org.eclipse.jetty.util.IteratingCallback;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class AsyncMiddleManServlet extends AbstractProxyServlet
|
||||
{
|
||||
private static final String CLIENT_TRANSFORMER = AsyncMiddleManServlet.class.getName() + ".clientTransformer";
|
||||
|
@ -702,7 +704,7 @@ public class AsyncMiddleManServlet extends AbstractProxyServlet
|
|||
}
|
||||
}
|
||||
|
||||
if (!buffers.isEmpty())
|
||||
if (!buffers.isEmpty() || finished)
|
||||
{
|
||||
ByteBuffer result = gzip(buffers, finished);
|
||||
buffers.clear();
|
||||
|
|
|
@ -25,13 +25,16 @@ import java.net.URLEncoder;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
|
@ -57,7 +60,9 @@ import org.eclipse.jetty.server.Server;
|
|||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.eclipse.jetty.toolchain.test.IO;
|
||||
import org.eclipse.jetty.toolchain.test.TestTracker;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.Utf8StringBuilder;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
@ -67,6 +72,7 @@ import org.junit.Assert;
|
|||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class AsyncMiddleManServletTest
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(AsyncMiddleManServletTest.class);
|
||||
|
@ -283,6 +289,55 @@ public class AsyncMiddleManServletTest
|
|||
Assert.assertEquals(200, response.getStatus());
|
||||
Assert.assertArrayEquals(bytes, response.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransformGzippedHead() throws Exception
|
||||
{
|
||||
startServer(new HttpServlet()
|
||||
{
|
||||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
|
||||
|
||||
String sample = "<a href=\"http://webtide.com/\">Webtide</a>\n<a href=\"http://google.com\">Google</a>\n";
|
||||
byte[] bytes = sample.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
out.write(gzip(bytes));
|
||||
|
||||
// create a byte buffer larger enough to create 2 (or more) transforms.
|
||||
byte[] randomFiller = new byte[64*1024];
|
||||
/* fill with nonsense
|
||||
* Using random data to ensure compressed buffer size is large
|
||||
* enough to trigger at least 2 transform() events.
|
||||
*/
|
||||
new Random().nextBytes(randomFiller);
|
||||
|
||||
out.write(gzip(randomFiller));
|
||||
}
|
||||
});
|
||||
startProxy(new AsyncMiddleManServlet()
|
||||
{
|
||||
@Override
|
||||
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
|
||||
{
|
||||
return new GZIPContentTransformer(new HeadTransformer());
|
||||
}
|
||||
});
|
||||
startClient();
|
||||
|
||||
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort())
|
||||
.header(HttpHeader.CONTENT_ENCODING, "gzip")
|
||||
.timeout(5, TimeUnit.SECONDS)
|
||||
.send();
|
||||
|
||||
Assert.assertEquals(200, response.getStatus());
|
||||
|
||||
String expectedStr = "<a href=\"http://webtide.com/\">Webtide</a>";
|
||||
byte[] expected = expectedStr.getBytes(StandardCharsets.UTF_8);
|
||||
Assert.assertArrayEquals(expected, response.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManySequentialTransformations() throws Exception
|
||||
|
@ -379,7 +434,11 @@ public class AsyncMiddleManServletTest
|
|||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
Assert.assertEquals(-1, request.getInputStream().read());
|
||||
// decode input stream thru gzip
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
IO.copy(new GZIPInputStream(request.getInputStream()),bos);
|
||||
// ensure decompressed is 0 length
|
||||
Assert.assertEquals(0, bos.toByteArray().length);
|
||||
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
|
||||
response.getOutputStream().write(gzip(bytes));
|
||||
}
|
||||
|
@ -946,6 +1005,68 @@ public class AsyncMiddleManServletTest
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A transformer that discards all but the first line of text.
|
||||
*/
|
||||
private static class HeadTransformer implements AsyncMiddleManServlet.ContentTransformer
|
||||
{
|
||||
private StringBuilder head = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException
|
||||
{
|
||||
if (input.hasRemaining() && head != null)
|
||||
{
|
||||
int lnPos = findLineFeed(input);
|
||||
if (lnPos == -1)
|
||||
{
|
||||
// no linefeed found, copy it all
|
||||
copyHeadBytes(input,input.limit());
|
||||
}
|
||||
else
|
||||
{
|
||||
// found linefeed
|
||||
copyHeadBytes(input,lnPos);
|
||||
output.addAll(getHeadBytes());
|
||||
// mark head as sent
|
||||
head = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (finished && head != null)
|
||||
{
|
||||
output.addAll(getHeadBytes());
|
||||
}
|
||||
}
|
||||
|
||||
private void copyHeadBytes(ByteBuffer input, int pos)
|
||||
{
|
||||
ByteBuffer dup = input.duplicate();
|
||||
dup.limit(pos);
|
||||
String str = BufferUtil.toUTF8String(dup);
|
||||
head.append(str);
|
||||
}
|
||||
|
||||
private int findLineFeed(ByteBuffer input)
|
||||
{
|
||||
for (int i = input.position(); i < input.limit(); i++)
|
||||
{
|
||||
byte b = input.get(i);
|
||||
if ((b == (byte)'\n') || (b == (byte)'\r'))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private List<ByteBuffer> getHeadBytes()
|
||||
{
|
||||
ByteBuffer buf = BufferUtil.toBuffer(head.toString(),StandardCharsets.UTF_8);
|
||||
return Collections.singletonList(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private static class DiscardContentTransformer implements AsyncMiddleManServlet.ContentTransformer
|
||||
{
|
||||
|
|
|
@ -0,0 +1,280 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util.component;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Testing for LifeCycleListener events on nested components
|
||||
* during runtime.
|
||||
*/
|
||||
public class LifeCycleListenerNestedTest
|
||||
{
|
||||
public static class Foo extends ContainerLifeCycle
|
||||
{
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return Foo.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Bar extends ContainerLifeCycle
|
||||
{
|
||||
private final String id;
|
||||
|
||||
public Bar(String id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Bar other = (Bar)obj;
|
||||
if (id == null)
|
||||
{
|
||||
if (other.id != null)
|
||||
return false;
|
||||
}
|
||||
else if (!id.equals(other.id))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return Bar.class.getSimpleName() + "(" + id + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public static enum LifeCycleEvent
|
||||
{
|
||||
STARTING,
|
||||
STARTED,
|
||||
FAILURE,
|
||||
STOPPING,
|
||||
STOPPED
|
||||
}
|
||||
|
||||
public static class CapturingListener implements LifeCycle.Listener, Container.InheritedListener
|
||||
{
|
||||
private List<String> events = new ArrayList<>();
|
||||
|
||||
private void addEvent(Object obj, LifeCycleEvent event)
|
||||
{
|
||||
events.add(String.format("%s - %s",obj.toString(),event.name()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lifeCycleStarting(LifeCycle event)
|
||||
{
|
||||
addEvent(event,LifeCycleEvent.STARTING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lifeCycleStarted(LifeCycle event)
|
||||
{
|
||||
addEvent(event,LifeCycleEvent.STARTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lifeCycleFailure(LifeCycle event, Throwable cause)
|
||||
{
|
||||
addEvent(event,LifeCycleEvent.FAILURE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lifeCycleStopping(LifeCycle event)
|
||||
{
|
||||
addEvent(event,LifeCycleEvent.STOPPING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lifeCycleStopped(LifeCycle event)
|
||||
{
|
||||
addEvent(event,LifeCycleEvent.STOPPED);
|
||||
}
|
||||
|
||||
public List<String> getEvents()
|
||||
{
|
||||
return events;
|
||||
}
|
||||
|
||||
public void assertEvents(Matcher<Iterable<? super String>> matcher)
|
||||
{
|
||||
assertThat(events,matcher);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beanAdded(Container parent, Object child)
|
||||
{
|
||||
if(child instanceof ContainerLifeCycle)
|
||||
{
|
||||
((ContainerLifeCycle)child).addLifeCycleListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beanRemoved(Container parent, Object child)
|
||||
{
|
||||
if(child instanceof ContainerLifeCycle)
|
||||
{
|
||||
((ContainerLifeCycle)child).removeLifeCycleListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddBean_AddListener_Start() throws Exception
|
||||
{
|
||||
Foo foo = new Foo();
|
||||
Bar bara = new Bar("a");
|
||||
Bar barb = new Bar("b");
|
||||
foo.addBean(bara);
|
||||
foo.addBean(barb);
|
||||
|
||||
CapturingListener listener = new CapturingListener();
|
||||
foo.addLifeCycleListener(listener);
|
||||
foo.addEventListener(listener);
|
||||
|
||||
try
|
||||
{
|
||||
foo.start();
|
||||
|
||||
assertThat("Foo.started",foo.isStarted(),is(true));
|
||||
assertThat("Bar(a).started",bara.isStarted(),is(true));
|
||||
assertThat("Bar(b).started",barb.isStarted(),is(true));
|
||||
|
||||
listener.assertEvents(hasItem("Foo - STARTING"));
|
||||
listener.assertEvents(hasItem("Foo - STARTED"));
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTED"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTED"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
foo.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddListener_AddBean_Start() throws Exception
|
||||
{
|
||||
Foo foo = new Foo();
|
||||
|
||||
CapturingListener listener = new CapturingListener();
|
||||
foo.addLifeCycleListener(listener);
|
||||
foo.addEventListener(listener);
|
||||
|
||||
Bar bara = new Bar("a");
|
||||
Bar barb = new Bar("b");
|
||||
foo.addBean(bara);
|
||||
foo.addBean(barb);
|
||||
|
||||
try
|
||||
{
|
||||
foo.start();
|
||||
|
||||
assertThat("Foo.started",foo.isStarted(),is(true));
|
||||
assertThat("Bar(a).started",bara.isStarted(),is(true));
|
||||
assertThat("Bar(b).started",barb.isStarted(),is(true));
|
||||
|
||||
listener.assertEvents(hasItem("Foo - STARTING"));
|
||||
listener.assertEvents(hasItem("Foo - STARTED"));
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTED"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTED"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
foo.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddListener_Start_AddBean() throws Exception
|
||||
{
|
||||
Foo foo = new Foo();
|
||||
Bar bara = new Bar("a");
|
||||
Bar barb = new Bar("b");
|
||||
|
||||
CapturingListener listener = new CapturingListener();
|
||||
foo.addLifeCycleListener(listener);
|
||||
foo.addEventListener(listener);
|
||||
|
||||
try
|
||||
{
|
||||
foo.start();
|
||||
|
||||
listener.assertEvents(hasItem("Foo - STARTING"));
|
||||
listener.assertEvents(hasItem("Foo - STARTED"));
|
||||
|
||||
foo.addBean(bara);
|
||||
foo.addBean(barb);
|
||||
|
||||
bara.start();
|
||||
barb.start();
|
||||
|
||||
assertThat("Bar(a).started",bara.isStarted(),is(true));
|
||||
assertThat("Bar(b).started",barb.isStarted(),is(true));
|
||||
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(a) - STARTED"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTING"));
|
||||
listener.assertEvents(hasItem("Bar(b) - STARTED"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
barb.stop();
|
||||
bara.stop();
|
||||
foo.stop();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -71,7 +71,6 @@ public class LifeCycleListenerTest
|
|||
|
||||
// check that the lifecycle's state is started
|
||||
assertTrue("The lifecycle state is not started",lifecycle.isStarted());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -138,6 +137,7 @@ public class LifeCycleListenerTest
|
|||
lifecycle.stop();
|
||||
assertFalse("The stopping event occurred", listener.stopping);
|
||||
}
|
||||
|
||||
private static class TestLifeCycle extends AbstractLifeCycle
|
||||
{
|
||||
Exception cause;
|
||||
|
@ -168,11 +168,9 @@ public class LifeCycleListenerTest
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class TestListener extends AbstractLifeCycle.AbstractLifeCycleListener
|
||||
{
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private boolean failure = false;
|
||||
private boolean started = false;
|
||||
private boolean starting = false;
|
||||
|
|
Loading…
Reference in New Issue